首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用nodemailer发送电子邮件被困在Ubuntu 16.04上

使用nodemailer发送电子邮件被困在Ubuntu 16.04上
EN

Stack Overflow用户
提问于 2018-10-03 14:55:05
回答 1查看 575关注 0票数 0

因此,我们使用包nodemailer发送电子邮件使用Node.js API。它在Windows环境下运行良好,使用smtp.gmail.com。一旦我们移动节点API到Ubuntu16.04,发送电子邮件停止工作。API的其余部分运行良好。在我们的gmail帐户上,我们启用了两个:

  1. https://accounts.google.com/b/0/DisplayUnlockCaptcha
  2. https://myaccount.google.com/lesssecureapps

代码:

代码语言:javascript
复制
var transporter = nodemailer.createTransport({ 
    //tried other combinations aswell
    service: 'gmail', 
    host: 'smtp.gmail.com', 
    //tried port 465 and secure:true
    port: 587, 
    secure: false, 
    auth: { 
        user: 'sender@gmail.com', 
        pass: 'password' 
    } 
});


var mailOptions = {
    from: 'sender@gmail.com',
    to: 'reciever@gmail.com',
    subject: 'Welcome',
    html: "Hello": 
};`

我们尝试了其他邮件提供商,如outlook、zoho、yandex、...The每次都会超时。Ubuntu上的防火墙是禁用的,安装也是新鲜的。

溶液

问题在于服务提供商,Ubuntu所在的地方。

EN

回答 1

Stack Overflow用户

发布于 2018-10-03 15:14:54

这只是建议通过Google的API绕过网络问题。

您需要通过Google控制台的API管理器生成OAUTH客户端ID,然后选择"Web应用程序“;一旦有了授权代码,就可以使用以下代码:

代码语言:javascript
复制
<pre>
  var nodemailer = require("nodemailer");
  sails.log.debug("try to send mail");
  var smtpTransport = nodemailer.createTransport("SMTP", {
      service: "Gmail",
      auth: {
        XOAuth2: {
          user: "xxx@gmail.com", // Your gmail address.
          clientId: "YOUR_CLIENT_ID",
          clientSecret: "YOUR_CLIENT_SECRET",
          refreshToken: "REFRESH_TOKEN_YOU_JUST_FOUND"
        }
      }
    });

  var mailOptions = {
      from: "xxx@gmail.com", // sender address
      to: RECEIVER_EMAIL", // list of receivers
      subject: "A_SUBJECT", // Subject line
      // text: ", // plaintext body
      html: htmlBody // html body
    };
  // send mail
    smtpTransport.sendMail(mailOptions, function(error, info) {
      if (error) {
        sails.log.debug(error);
        return res.notOk({
          status: "error",
          msg: "Email sending failed"
        })
      } else {
        console.log("Message %s sent: %s", info.messageId, info.response);
        return res.ok({
          status: "ok",
          msg: "Email sent"
        })
      }
      smtpTransport.close();
    });
</pre>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52629917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档