为什么我在 Node 邮件程序上收到“登录错误”?

问题描述 投票:0回答:0

我尝试使用 Node mailer 发送电子邮件,但一直收到无效登录错误。 我想在用户注册时发送电子邮件。

我启用了双因素验证并为此创建了一个应用程序。我已经获得了这个应用程序的 16 位密码。 Created an app

这是我得到的错误

535 5.7.8  https://support.google.com/mail/?p=BadCredentials i10-20020a05640200ca00b0050bd9d3ddf3sm4737563edu.42 - gsmtp
project/node_modules/nodemailer/lib/smtp-connection/index.js:193:44)
    at TLSSocket.emit (node:events:512:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10) {
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n' +
    '535 5.7.8  https://support.google.com/mail/?p=BadCredentials i10-20020a05640200ca00b0050bd9d3ddf3sm4737563edu.42 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN'
}

这是我的代码。

const nodemailer = require("nodemailer");
const sender = process.env.EMAIL_NAME;
const passsword = process.env.EMAIL_PASSWORD;

exports.confrimEmail = (name, recieverEmail) => {
  const transporter = nodemailer.createTransport({
    service: "gmail",
    host: "smtp.gmail.com",
    auth: {
      user: sender,
      pass: passsword,
    },
  });

  const mailOptions = {
    from: sender,
    to: recieverEmail,
    subject: "Confirm Your Email Address for AASTU-GDSC Inventory API",
    text: `Dear ${name}, \n
           Thank you for signing up for the AASTU-GDSC Inventory API!\n
           We are excited to have you join our community and
           can't wait to see what you will create with our API.
           To complete your registration, we need to confirm your email address.\nPlease click on the link below to verify your email address:\n
           http://localhost:3001/api/users/confrimed
           `,
  };

  transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log(error);
    } else {
      console.log(mailOptions.from);
      console.log(mailOptions.to);
      console.log(mailOptions.text);
      console.log("Email sent:" + info.response);
    }
  });
};
node.js nodemailer
© www.soinside.com 2019 - 2024. All rights reserved.