node.js - 错误: Invalid login: 535 Authentication failed: Bad username/password with node mailer

标签 node.js sendgrid nodemailer

我收到“错误:无效登录:535 身份验证失败:用户名/密码错误” 我有正确的用户名和密码,但仍然收到此错误。可能是什么问题.. enter image description here

  const sendForgotPasswordEmail = (user) => {
    if (!user) { return; }
    const token = user.passwordResetToken;
    const transporter = nodemailer.createTransport({
      service: 'SendGrid',
      auth: {
        user: process.env.SENDGRID_USER,
        pass: process.env.SENDGRID_PASSWORD
      }
    });
    const mailOptions = {
      to: user.email,
      from: 'mail.mail@com',
      subject: 'Reset your password',
      text: `You are receiving this email because you (or someone else) have requested the reset of the password for your account.\n\n
        Please click on the following link, or paste this into your browser to complete the process:\n\n
        http://${req.headers.host}/reset/${token}\n\n
        If you did not request this, please ignore this email and your password will remain unchanged.\n`
    };
    return transporter.sendMail(mailOptions)
      .then(() => {
        req.flash('info', { msg: `An e-mail has been sent to ${user.email} with further instructions.` });
      });
  };

  createRandomToken
    .then(setRandomToken)
    .then(sendForgotPasswordEmail)
    .then(() => res.redirect('/forgot'))
    .catch(next);

最佳答案

虽然您没有具体说明,但从您在 Github 上的交叉发布和错误的屏幕截图来看,您的问题更具体地说是您在使用 Hackathon Starter 时遇到用户名/密码错误(使用SendGrid设置);并且其底层项目使用了nodemailer。

我已经使用 SendGrid 和有效的用户名/密码测试了 Hackathon Starter,它按预期工作,因此这不是 Hackathon Starter 或 Nodemailer 的问题。因此,在您的具体情况下,根据您发布的错误,SendGrid 正在响应,他们收到的用户名和密码与系统中的有效用户名密码不对应。我建议:

  1. 登录 SendGrid 网站并重新验证用户名和密码。请注意,SendGrid 提供的 API key 不是用户名和密码;而是用户名和密码。它们的用途与使用 SendGrid 进行 SMTP 邮件身份验证的目的不同。您需要使用 SendGrid 中帐户详细信息下的用户名/密码。
  2. 检查您的环境变量,以验证它们是否使用您的 SendGrid“用户名”和“密码”正确设置。请注意,您很可能不需要在用户名和密码周围添加任何单引号或双引号(至少这对我来说是这样的)。您可以在代码中临时添加如下调试代码,以检查环境变量是否设置正确。

    console.log“SENDGRID_USER env 变量设置为:”,process.env.SENDGRID_USER console.log“SENDGRID_PASSWORD env 变量设置为:”,process.env.SENDGRID_PASSWORD

请确保在验证后删除此调试代码,因为将密码以明文形式进入控制台日志可能会成为安全问题。

顺便说一句,在您的 mailOptions 中,您可能需要将 from: 字段更改为与您项目的电子邮件地址/别名之一相对应的有效电子邮件地址。

关于node.js - 错误: Invalid login: 535 Authentication failed: Bad username/password with node mailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514799/

相关文章:

node.js - Mongoose 嵌套架构 CastError

javascript - 如何直接在Sequelize中更改预加载的结果

node.js - Mongoose/Node 如何直接在EJS文件中访问数据库

apache - 修复由于子域上的 HSTS 而导致的断开链接跟踪?

ruby-on-rails - 在 Hartl 的教程中添加联系表单

node.js - -bash ng 命令未找到 Mojave

javascript - 通过 firebase 函数替换在发送网格中不起作用

node.js - 带 Mailgun 的 Nodemailer

javascript - 错误: No recipients defined when trying to use nodemailer but recipient listed

node.js - 错误 : write EPIPE at errnoException (net. js:770:11)