不接受Node.js nodemailer用户名和密码

时间:2019-01-16 14:56:05

标签: node.js nodemailer

我正在尝试使用nodejs和nodemailer发送邮件,但是它对我不起作用。我有以下代码:

const config = require('../../config');
const http = require('http');
const nodemailer = require('nodemailer');

module.exports = {

    sendMail: function (params) {
        return new Promise((resolve, reject) => {

            var transporter = nodemailer.createTransport({
                //service: 'gmail', //I tried with this too
                host: 'smtp.gmail.com',
                port: 465,
                secure: true,
                auth: {
                    user: params.fromMail,
                    pass: params.fromPass
                }
            });

            var mailOptions = {
                from: params.fromMail,
                to: params.toMailList,
                subject: params.subject,
                html: params.mailHtmlContent,
            };

            transporter.sendMail(mailOptions, function(err, info){
                if (err) {
                    reject(err);
                } else {
                    resolve(info.response);
                }
            });


        });
    }

};

我收到此错误:

  

{错误:无效的登录名:535-5.7.8不接受用户名和密码。   在535 5.7.8了解更多   https://support.google.com/mail/?p=BadCredentials p6sm97493129wrx.50-   gsmtp       在SMTPConnection._formatError(C:\ Users \ Carlos \ Desktop \ Proyectos \ nodeServer \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:774:19)       在SMTPConnection._actionAUTHComplete(C:\ Users \ Carlos \ Desktop \ Proyectos \ nodeServer \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:1509:34)       在SMTPConnection._responseActions.push.str(C:\ Users \ Carlos \ Desktop \ Proyectos \ nodeServer \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:547:26)       在SMTPConnection._processResponse(C:\ Users \ Carlos \ Desktop \ Proyectos \ nodeServer \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:933:20)       在SMTPConnection._onData(C:\ Users \ Carlos \ Desktop \ Proyectos \ nodeServer \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:739:14)       在TLSSocket._socket.on.chunk(C:\ Users \ Carlos \ Desktop \ Proyectos \ nodeServer \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:691:47)       在TLSSocket.emit(events.js:182:13)       在addChunk(_stream_visible.js:283:12)       在可读AddChunk(_stream_visible.js:264:11)       在TLSSocket.Readable.push(_stream_visible.js:219:10)代码处:'EAUTH',响应:'535-5.7.8用户名和密码不被接受。   在\ n535 5.7.8了解更多信息   https://support.google.com/mail/?p=BadCredentials p6sm97493129wrx.50-   gsmtp”,responseCode:535,命令:“ AUTH PLAIN”}

我已更改了我的gmail帐户中有关“安全性较差的应用”的通讯参数。 我也尝试了另一个非Gmail帐户。 我尝试过:

service: 'gmail',

与连接有关:

host: 'smtp.gmail.com',
port: 465,
secure: true,

我在stackoverflow中阅读了所有“类似问题”,但我不知道我是否需要其他东西或是否做得不好。 (当然,我已经修改了两个帐户的用户名和密码,都可以)。

我在做什么不好?

2 个答案:

答案 0 :(得分:1)

var nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  service: 'gmail',//smtp.gmail.com  //in place of service use host...
  secure: false,//true
  port: 25,//465
  auth: {
    user: 'example@gmail.com',
    pass: 'password'
  }, tls: {
    rejectUnauthorized: false
  }
});

transporter.sendEMail = function (mailRequest) {
  return new Promise(function (resolve, reject) {
    transporter.sendMail(mailRequest, (error, info) => {
      if (error) {
        reject(error);
      } else {
        resolve("The message was sent!");
      }
    });
  });
}

module.exports = transporter;

用法

//import transporter JS file
const mail = require('../utils/mail');
let htmlContent = `
                <h1><strong>Contact Form</strong></h1>
                <p>Hi,</p>
                <p>${name} contacted with the following Details</p>
                <br/>
                <p>Email: ${email}</p>
                <p>Phone: ${phone}</p>
                <p>Company Name: ${companyName}</p>
                <p>Message: ${message}</p>
                `
    let mailOptions = {
        from: "Example <example@gmail.com>",
        to: "me@gmail.com",
        subject: "Mail Test",
        text: "",
        html: htmlContent
    }

mail.sendMail(mailOptions)
    .then(function (email) {
        res.status(200).json({ success: true, msg: 'Mail sent' });
    }).catch(function (exception) {
        res.status(200).json({ success: false, msg: exception });
    });

注意:

  1. 在使用gmail发送电子邮件之前,您必须允许不安全的应用访问gmail,您可以通过转到gmail设置进行操作 https://myaccount.google.com/lesssecureapps?pli=1

  2. 还启用验证码 https://accounts.google.com/DisplayUnlockCaptcha

答案 1 :(得分:0)

接受的答案对我不起作用。这四个步骤做到了:

  • 使用管理员帐户登录到您的Google管理控制台。

  • 从管理控制台仪表板中,依次转到“安全性”和“基本设置”(您可能必须单击底部的“更多控件”。)

  • 在“安全性较低的应用程序”下,选择“转到安全性较弱的应用程序的设置”。

  • 在子窗口中,选择“允许所有用户访问不太安全的应用程序”单选按钮。

相关问题