使用带有 Gmail 密码的 nodemailer

Use nodemailer with Gmail password

提问人:LAHCEN AGLAGAL 提问时间:11/16/2023 最后编辑:VLAZLAHCEN AGLAGAL 更新时间:11/17/2023 访问量:35

问:

嗨,大家好,我在问我是否可以将 nodemailer 与我的 gmail 密码一起使用,而不是应用程序密码。 因为每当我尝试这样做时,我都会收到此错误:

Error in sending email: Error: Invalid login: 534-5.7.9 Application-specific password required. Learn more at

534 5.7.9 https://support.google.com/mail/?p=InvalidSecondFactor H5-20020A170906718500B009DDF38056F8SM8150481ejk.118 - GSMTP

节点 .js 身份验证 安全性 节点邮件

评论


答:

0赞 eekinci 11/17/2023 #1

索引.js

const transporter = nodemailer.createTransport({
  service: process.env.MAIL_SERVICE,
  auth: {
    user: process.env.MAIL_USERNAME,
    pass: process.env.MAIL_PASSWORD,
  },
});

const mailOptions = {
  from: process.env.MAIL_USERNAME,
  to: process.env.MAIL_USERNAME,
  subject: req.body.subject,
  text: req.body.text,
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    console.log(error);
  } else {
    console.log(info);
  }
});

environment.env 环境

MAIL_SERVICE='gmail'
MAIL_USERNAME='s[email protected]'
MAIL_PASSWORD='[email protected]'