gpt4 book ai didi

node.js - Gmail 阻止 Nodemailer 发送邮件

转载 作者:行者123 更新时间:2023-12-03 12:14:37 24 4
gpt4 key购买 nike

我正在部署我的 Node.js 应用程序。但是,我在发送注册电子邮件时遇到了问题。

const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: GMAIL_USER,
pass: GMAIL_PASS,
},
});
......
......
transporter.sendMail({
to: newUser.email,
subject: 'Confirm Email',
html: `Please click this email to confirm your email: <a href="${url}">${url}</a>`
});

当我尝试在本地主机上运行它时,这非常有效,但是一旦我将文件上传到我的服务器并尝试它,谷歌就会阻止登录尝试,我收到一封电子邮件说
Someone just used your password to try to sign in to your account. Google blocked them, but you should check what happened.

每次,我都会点击“这就是我”按钮,但任何 future 的尝试仍然会被阻止。

我启用了“不太安全的应用程序”。有没有办法将要从我的 gmail 发送的 IP 列入白名单?或者一种让这个工作正常的方法?

最佳答案

您有两个选择。要么将访问不太安全的应用程序设置为 已启用 或者您从 Google OAuth2.0 获取 accessToken 和 refreshToken 并在您的 nodemailer 中使用它们配置

  • 对于选项一去https://www.google.com/settings/security/lesssecureapps
  • 对于选项二,请转至 https://console.developers.google.com/apis/credentials

  • 如果您选择选项二,您的传输配置将如下所示:
    auth: {
    type: 'OAuth2',
    user: 'user@example.com',
    accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
    }

    关于node.js - Gmail 阻止 Nodemailer 发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60050129/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com