Nodemailer和Gmail

时间:2014-11-03 13:55:05

标签: node.js gmail nodemailer

我想使用Nodemailer和GMAIL发送电子邮件,但它不起作用......

编辑#1:

代码:



var transporter = nodemailer.createTransport(smtpPool({
  service: 'gmail',
  auth: {
    user: '***@gmail.com',
    pass: '***'
  },
  maxConnections: 5,
  maxMessages: 10
}));

var mailOptions = {
  from: mail, // sender address
  to: 'infoprintwithlove@gmail.com',
  subject: subject,
  text: message,
  html: message
};

transporter.sendMail(mailOptions, function(error, info){
  if(error){
    console.log(error);
  }else{
    request.flash('success', 'Votre message à bien été envoyé.');
    response.redirect('/contacter-printwithlove');
  }
});




错误:

{ [Error: Invalid login]
     code: 'EAUTH',

密码似乎不正确,但在Gmail网络界面上我的密码效果很好......

编辑#2:

enter image description here

1 个答案:

答案 0 :(得分:2)

您需要传递服务=' gmail'和用户/密码:

var transporter = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: 'myemail@gmail.com',
    pass: <your password>
  }
});

// Or using SMTP Pool if you need to send a large amount of emails
var smtpPool = require('nodemailer-smtp-pool');
var transporter = nodemailer.createTransport(smtpPool({
  service: 'gmail',
  auth: {
    user: 'myemail@gmail.com',
    pass: <your password>
  },
  maxConnections: 5,
  maxMessages: 10
}));

编辑:

您可能需要&#34;允许访问您的Google帐户&#34;。使用浏览器登录您的Gmail,然后转到此链接https://accounts.google.com/DisplayUnlockCaptcha将其解锁。您可能需要使用刚刚登录的相同浏览器,以便google知道您要授予哪个帐户访问权限。点击&#34;允许&#34;按钮,您应该看到消息:&#34;帐户访问权限已启用。请尝试从新设备或应用程序再次登录您的Google帐户。&#34;