Nodemailer不能使用gmail发送邮件

时间:2021-07-23 18:12:20

I am trying to send email through nodemailer but is not able to send email and showing following error.

我试图通过nodemailer发送电子邮件,但不能发送邮件和显示以下错误。

{ [Error: Invalid login]
  code: 'EAUTH',
  response: '534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsTr\n534-5.7.14 2r0PE8hXz57GMK9aydA3GouUGQr1Pc2wBywEf6i09kKVOv9Qv5y9Csy_lL-PIgUO6ML8uA\n534-5.7.14 ZsEiEkpAU0N5aiQBZdI1urKo3XfCiiS2MhjiUZaBgpn88sFXR-sBeA5ydMc_Md1F5wDcbX\n534-5.7.14 7ynrcVC-h0H_-e_ptvGhA3ywiHOSoDZAxzrindvEMNkXmCilbo9J7ITdlXFKwQjITaIkei\n534-5.7.14 Tk8QMKEY22ldNjE78Lh2ekheLd5M> Please log in via your web browser and\n534-5.7.14 then try again.\n534-5.7.14  Learn more at\n534 5.7.14  https://support.google.com/mail/answer/78754 21sm9309692qgj.21 - gsmtp',
  responseCode: 534 }

I am using exact code as shown in nodemailer documentation.

我正在使用nodemailer文档中所示的精确代码。

this is server side code which i am using.

这是我正在使用的服务器端代码。

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
       user: 'username',
       pass: 'password'
    }
});

app.post('/sendfeedbackmail', function(req, res) {

    var mailOptions = {
        from: 'Feedback<username@gmail.com>', // sender address 
        to: 'user1@gmail.com', // receiver 
        subject: 'Subject', // Subject line 
        text: mailData, // plaintext body 
        html: mailData // html body 
    };

   // send mail with defined transport object 
   transporter.sendMail(mailOptions, function(error, info) {
       if (error) {
          console.log(error);
          res.send({success:false});
       }else{
           res.send({
              success: true
           })
       }
   });
})

This was working fine few days ago and still working fine for other email client then gmail hence i think the issue may be related to any security setting of gmail account.

这几天前还可以正常工作,对于其他邮件客户,gmail也可以正常工作,所以我认为这个问题可能与gmail账户的任何安全设置有关。

2 个解决方案

#1


3  

UPDATE

Check http://masashi-k.blogspot.com.br/2013/06/sending-mail-with-gmail-using-xoauth2.html to register your application.

查看http://masashi-k.blogspot.com.br/2013/06/sending-mail-with- gmail-using-xo2 .html来注册应用程序。


You will need something like this to authenticate on Gmail:

您需要类似这样的东西来验证Gmail:

var xoauth2 = require('xoauth2');
var nodemailer = require('nodemailer');
var smtp = require('nodemailer-smtp-transport');

var generator = xoauth2.createXOAuth2Generator({
  user: '..',
  clientId: '...',
  clientSecret: '...',
  refreshToken: '...',
  accessToken: ''
});
generator.on('token', function(token){
  console.info('new token', token.accessToken);
  // maybe you want to store this token
});

var transporter_google = nodemailer.createTransport(smtp({
  name: '...',
  host: '...',
  port: 587,
  secure: false,
  ignoreTLS: false,
  tls: { rejectUnauthorized: true },
  debug: false,
  auth: { xoauth2: generator }
}));

See https://github.com/andris9/nodemailer-smtp-transport#authentication.

看到https://github.com/andris9/nodemailer-smtp-transport身份验证。

#2


0  

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
       user: 'username',
       pass: 'password'
    }
});

Instead of username = enter your valid email id and password = your password

而不是用户名=输入您的有效电子邮件id和密码=您的密码

again an alert shows to your Gmail from google like

再一次从谷歌向Gmail显示一个警告。

(Google will continue to block sign-in attempts from the app you're using because it has known security problems or is out of date. You can continue to use this app by allowing access to less secure apps, but this may leave your account vulnerable. )

(谷歌将继续阻止你正在使用的应用程序的登录尝试,因为它已经知道安全问题或过时了。你可以继续使用这个应用程序,允许访问不太安全的应用程序,但这可能会让你的账户变得脆弱。

you need to allow the access from other mailer

您需要允许来自其他邮件的访问。

then it generates the response

然后它生成响应

#1


3  

UPDATE

Check http://masashi-k.blogspot.com.br/2013/06/sending-mail-with-gmail-using-xoauth2.html to register your application.

查看http://masashi-k.blogspot.com.br/2013/06/sending-mail-with- gmail-using-xo2 .html来注册应用程序。


You will need something like this to authenticate on Gmail:

您需要类似这样的东西来验证Gmail:

var xoauth2 = require('xoauth2');
var nodemailer = require('nodemailer');
var smtp = require('nodemailer-smtp-transport');

var generator = xoauth2.createXOAuth2Generator({
  user: '..',
  clientId: '...',
  clientSecret: '...',
  refreshToken: '...',
  accessToken: ''
});
generator.on('token', function(token){
  console.info('new token', token.accessToken);
  // maybe you want to store this token
});

var transporter_google = nodemailer.createTransport(smtp({
  name: '...',
  host: '...',
  port: 587,
  secure: false,
  ignoreTLS: false,
  tls: { rejectUnauthorized: true },
  debug: false,
  auth: { xoauth2: generator }
}));

See https://github.com/andris9/nodemailer-smtp-transport#authentication.

看到https://github.com/andris9/nodemailer-smtp-transport身份验证。

#2


0  

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
       user: 'username',
       pass: 'password'
    }
});

Instead of username = enter your valid email id and password = your password

而不是用户名=输入您的有效电子邮件id和密码=您的密码

again an alert shows to your Gmail from google like

再一次从谷歌向Gmail显示一个警告。

(Google will continue to block sign-in attempts from the app you're using because it has known security problems or is out of date. You can continue to use this app by allowing access to less secure apps, but this may leave your account vulnerable. )

(谷歌将继续阻止你正在使用的应用程序的登录尝试,因为它已经知道安全问题或过时了。你可以继续使用这个应用程序,允许访问不太安全的应用程序,但这可能会让你的账户变得脆弱。

you need to allow the access from other mailer

您需要允许来自其他邮件的访问。

then it generates the response

然后它生成响应