【问题标题】:node.js nodemailer gmail errornode.js nodemailer gmail错误
【发布时间】:2015-09-07 23:31:19
【问题描述】:

我正在尝试使用 node.js - nodemailer 模块发送电子邮件,我的整个代码看起来像

var http=require("http");
var nodemailer=require("nodemailer");

http.createServer(function(req,res){
     res.writeHead(200, {"Content-Type": "text/plain"});
     var transporter = nodemailer.createTransport('SMTP',{
    service: 'gmail',
    auth: {
        user: 'myemail@gmail.com', // my mail
        pass: 'mypassword'
    }
});
var mailOptions = {
    from: 'Fred Foo ✔ <foo@blurdybloop.com>', // sender address 
    to: 'myemail@gmail.com', // the same mail = want to send it to myself
    subject: 'Hello ✔', // Subject line 
    text: 'Hello world ✔', // plaintext body 
    html: '<b>Hello world ✔</b>' // html body 
};
    transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);

});
    res.end("hllo");
}).listen(4000)

但是当我运行它时,它会抛出“登录”错误,虽然我的名字/密码是正确的,整个错误看起来像这样

{ [AuthError: Invalid login - 534-5.7.14 <https://accounts.google.com/ContinueSi
gnIn?sarp=1&scc=1&plt=AKgnsbtWV
534-5.7.14 FHw6FqlXkg3nIQW7AtsO7o0r-4EtD7eTtnrgHeXxeULYqUtHjrnTB9R647mlR7GY9Z4lS
z
534-5.7.14 WuzYYnNwNBjIyrjNzHk-o9sIV_vIsbjgF5HsFPtJl5S0dSf3eyNCX_E8tQ_Z6s4Z1mUe5
i
534-5.7.14 p8Y5YagzdN5RSkjpB1yTu3TfWHSTLisczIZphhLfhlz-v86tAB8MX-4jRqA51hfJYy0ko
F
534-5.7.14 qytMeIbs1kUZEJzPIabmjRi87DIk> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 cx1sm17733852wib.0 - gs
mtp]
  name: 'AuthError',
  data: '534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt
=AKgnsbtWV\r\n534-5.7.14 FHw6FqlXkg3nIQW7AtsO7o0r-4EtD7eTtnrgHeXxeULYqUtHjrnTB9R
647mlR7GY9Z4lSz\r\534-5.7.14 WuzYYnNwNBjIyrjNzHk-o9sIV_vIsbjgF5HsFPtJl5S0dSf3ey
NCX_E8tQ_Z6s4Z1mUe5i\r\n534-5.7.14 p8Y5YagzdN5RSkjpB1yTu3TfWHSTLisczIZphhLfhlz-v
86tAB8MX-4jRqA51hfJYy0koF\r\n534-5.7.14 qytMeIbs1kUZEJzPIabmjRi87DIk> Please log
 in via your web browser and\r\n534-5.7.14 then try again.\r\n534-5.7.14  Learn
more at\r\n534 5.7.14  https://support.google.com/mail/answer/78754 cx1sm1773385
2wib.0 - gsmtp',
  stage: 'auth' }

我必须在我的 gmail acc 中设置什么以允许它从那里发送电子邮件,或者我该如何解决它

【问题讨论】:

    标签: javascript node.js gmail nodemailer


    【解决方案1】:

    尝试安装 nodemailer-smtp-transport,然后在你的 createTransport 函数中使用它。

    var smtpTransport = require('nodemailer-smtp-transport');
    
    var transport = nodemailer.createTransport(smtpTransport({
        service: 'gmail',
        auth: {
            user: 'myemail@gmail.com', // my mail
            pass: 'mypassword'
        }
    }));
    

    并尝试在您的谷歌帐户中启用此功能: https://www.google.com/settings/security/lesssecureapps

    我真的认为这应该是你的身份验证问题

    【讨论】:

    • 我最近遇到了这个问题。我阅读了github.com/nodemailer/nodemailer#using-gmail 中的文档,它显示了您必须采取的一些额外步骤才能访问不太安全的应用程序。这对我来说就像一个魅力!
    【解决方案2】:

    使用非安全端口后它对我有用

    var transporter = nodemailer.createTransport({
      host: 'smtp.gmail.com',
      port: 587,
      secure: false, // secure:true for port 465, secure:false for port 587
      auth: {
        user: 'xxxx@gmail.com',
        pass: 'xxxx'
      }
    });
    

    【讨论】:

    • @Johnyb 确保允许访问不太安全的应用程序,形成您的谷歌帐户安全
    猜你喜欢
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    • 2019-12-24
    • 2018-02-28
    • 1970-01-01
    • 1970-01-01
    • 2020-06-22
    • 2020-02-10
    相关资源
    最近更新 更多