无法通过nodejs中的nodemailer发送电子邮件

Unable to send email via nodemailer in nodejs

我使用以下配置通过 Nodemailer 发送电子邮件但无法发送并抛出错误:getaddrinfo ENOTFOUND ssl://smtp.googlemail.com ssl://smtp.googlemail.com:587 at GetAddrInfoReqWrap.onlookup

配置:

host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
    user: 'xxx@gmail.com',
    pass: 'xxxx'
}

谁能给我一个解决方案,尽快解决?

注意:确保您在以下 link 中允许较低的安全性:

https://myaccount.google.com/security?pli=1#connectedapps

然后像这样配置 nodemailer

var smtpTransport = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: 'user@gmail.com',
        pass: 'pass'
    }
});

尝试添加值为 gmailservice 参数。

var smtpTransport = nodemailer.createTransport({
    service: 'gmail',
    port: 8000,
    secure: false, // use SSL
    auth: {
        user: 'user@gmail.com',
        pass: 'pass'
    }
});

参考How to send mail using nodemailer

你需要做两件事: 1. 添加您的服务提供商名称

 let transporter = nodemailer.createTransport({
    service: 'gmail',
    port: 25,
    secure: false,
    auth: {
           user: 'your_mail_address@gmail.com',
           pass: 'pass'
         }
    });
  1. 将安全性较低的应用程序的访问设置设置为启用。

参考这些链接:

  1. https://www.w3schools.com/nodejs/nodejs_email.
  2. https://devanswers.co/allow-less-secure-apps-access-gmail-account/
This works for me. 

var transporter = nodemailer.createTransport({
   host: 'smtp.gmail.com',
   port: 465,
   secure: true,  
   service: 'Gmail',
   auth: {
    user: 'user@gmail.com',
    pass: '***'
   }
});

var mailOptions = {
  from: SOURCE_EMAIL,
  to: to,
  subject: 'send mail',
  text: 'Your text is here'
};
var status = transporter.sendMail(mailOptions, function(error, info){
   console.log('info...',info);
   if (error) {
      console.log(error);       
   } else {
      console.log('Email sent: ' + info.response);      
  }
});