node.js - Azure 移动服务上的 NodeMailer 无法正常工作

标签 node.js azure azure-mobile-services nodemailer

我正在尝试使用nodemailer发送邮件。该脚本在本地计算机上运行,​​但我无法将nodemailer包含在azure移动服务中。在我的 package.json 中添加了 'nodemailer' : "*"但仍然无法包含它。

日志说

类型错误:无法读取未定义的属性“原型(prototype)”

我注释掉了完整的逻辑,但错误仍然存​​在。终于评论出来了 var nodemailer = require('nodemailer');

错误消失了。

最佳答案

要解决此问题,您需要安装旧版本的 nodemailer,以便它可以在 Azure 移动服务上运行。我在 Azure 的 package.json 中添加了 Nodemailer 0.7.1 版本,然后进行了必要的代码更改,它对我有用。

支持 0.7.1 所需的代码更改非常小,以下是文档中的完整代码:

var nodemailer = require("nodemailer");

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a3a9a5ada8eab1b7a1b684a3a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    from: "Fred Foo ✔ <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfa9a0a08fada3babdabb6ada3a0a0bfe1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>>", // sender address
    to: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94f6f5e6d4f6f8e1e6f0edf6f8fbfbe4baf7fbf9" rel="noreferrer noopener nofollow">[email protected]</a>, <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1173706b51737d64637568737d7e7e613f727e7c" rel="noreferrer noopener nofollow">[email protected]</a>", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}

// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    // if you don't want to use this transport object anymore, uncomment following line
    //smtpTransport.close(); // shut down the connection pool, no more messages
});

Nodemailer 0.7.1 documentation

关于node.js - Azure 移动服务上的 NodeMailer 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26753793/

相关文章:

c# - 无法使用模拟器上传文件, Controller 有问题

asp.net - 无法为 Azure 云服务设置通配符 SSL

javascript - Azure 移动服务推送磁贴通知和清除磁贴属性

android - 开发移动应用程序时将文件上传到 CDN 的最佳实践?

node.js - 在 Heroku 上运行 Node.js 应用程序时出错

sql-server - Sequelize Query,多对多关系问题

node.js - 快速等待数据库连接

node.js - 覆盖特定模型的 Mongoose 查询

android - 适用于 Android 的 Azure ASP.NET 后端问题

azure - 如何将动态参数传递给Azure Function