Nodemailer 与 Gmail | 出现错误:错误:无效登录:535-5.7.8 用户名和密码不被接受

Jak*_*son 2 gmail smtp node.js nodemailer

我正在尝试使用带有 Gmail 帐户的 Nodemailer 在我的应用程序中设置电子邮件验证。

我的问题是它报告错误,指出我的用户名和密码尚未被接受:

There was an error: 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 a3sm2007362wri.89 - gsmtp
Run Code Online (Sandbox Code Playgroud)
const transporter = nodemailer.createTransport({
  host: "smtp.gmail.com",
  service: "Gmail",
  port: 465,
  secure: true,
  auth: {
    user: process.env.USER,
    pass: process.env.PASS,
  },
});

transporter.sendMail(
  {
    from: process.env.USER,
    to: email,
    subject: subject,
    text: text,
  },
  (error) => {
    if (error) {
      return console.log("There was an error: " + error);
    }
    console.log("Email sent successfully");
  }
);
Run Code Online (Sandbox Code Playgroud)

我可以确认 和process.env.USERprocess.env.PASS正确的,并且我还允许不太安全的应用程序

在此输入图像描述

有什么我遗漏/不理解的吗?

dev*_*per 8

“从 2022 年 5 月 30 日起,\xe2\x80\x8b\xe2\x80\x8bGoogle 不再支持使用要求您仅使用用户名和密码登录 Google 帐户的第三方应用或设备。”\ n https://support.google.com/accounts/answer/6010255?hl=en

\n

解决方案

\n
    \n
  1. 转到您的 Google 帐户
  2. \n
  3. 查找安全菜单
  4. \n
  5. 启用两步验证
  6. \n
  7. 之后您将在安全页面中看到“应用程序密码”选项。(如果没有在 Google 帐户搜索框中搜索。
  8. \n
  9. 从“选择应用程序”下拉列表中选择邮件
  10. \n
  11. 从“设备”下拉列表中选择设备,然后单击“生成”按钮。
  12. \n
  13. 复制密码并在需要时在您的应用程序中使用它。
  14. \n
\n