Nodejs nodemailer 用户名和密码不被接受

Nodejs nodemailer Username and Password not accepted

我正在尝试使用 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);
                }
            });


        });
    }

};

我收到这个错误:

{ Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8 https://support.google.com/mail/?p=BadCredentials p6sm97493129wrx.50 - gsmtp at SMTPConnection._formatError (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:774:19) at SMTPConnection._actionAUTHComplete (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:1509:34) at SMTPConnection._responseActions.push.str (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:547:26) at SMTPConnection._processResponse (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:933:20) at SMTPConnection._onData (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:739:14) at TLSSocket._socket.on.chunk (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:691:47) at TLSSocket.emit (events.js:182:13) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:264:11) at TLSSocket.Readable.push (_stream_readable.js:219:10) code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/?p=BadCredentials p6sm97493129wrx.50 - gsmtp', responseCode: 535, command: 'AUTH PLAIN' }

我已经更改了我的 gmail 帐户上关于 "less secure apps" 的通讯参数。 我也尝试使用另一个非 gmail 帐户。 我试过这个:

service: 'gmail',

这用于连接:

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

我在 Whosebug 中阅读了所有 "Similar Questions",但我不知道我是否还需要其他任何东西,或者我是否做错了什么。 (当然两个账号的用户名和密码我都查过了,没问题)。

我做错了什么?

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

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

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

  • 在管理控制台仪表板中,转到“安全”,然后转到“基本设置”(您可能需要单击底部的“更多控件”)。

  • 在安全性较低的应用下,select转到安全性较低的应用的设置。

  • 在子窗口中,select 允许所有用户访问安全性较低的应用单选按钮。