NodeMailer 登录无效

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

我是node.js 编程新手。我正在使用nodemailer 模块发送电子邮件。

const nodemailer = require ('nodemailer'),
credentials=require('./credentials.js');
var mailTransport=nodemailer.createTransport({
    service:'Gmail',
    auth: {
        user : credentials.gmail.user,
        pass : credentials.gmail.password,
    }
});
function sendMail(mail_id){
    mailTransport.sendMail({
        from: ' "my name" <[email protected]>',
        to : mail_id,   //[email protected]
        subject : 'Hello',
        text: "Hello How do u do ?",
    },function(err,info){
        if(err){
            console.log('Unable to send the mail :'+err.message);
        }
        else{
            console.log('Message response : '+info.response);
        }
    });
}
exports.sendMail=sendMail;

这是我向不同用户发送电子邮件的程序。但我收到 Invalid Login 。我不知道为什么会这样。我是 Node.js 和服务器端脚本的新手。
我正在使用我的 Gmail 用户名和密码作为凭据。
请帮助我。

javascript node.js smtp server-side nodemailer
8个回答
40
投票

原因之一可能是 Gmail 的“现代安全标准”保护。

检查您的 Gmail 收件箱中是否有任何主题为“Google 帐户:登录尝试被阻止”的新邮件

如果是,请打开邮件并点击链接 https://www.google.com/settings/security/lesssecureapps

将“访问不太安全的应用程序”设置为“打开”。再试一次,现在应该可以工作了。


16
投票

特别是 2 个问题:或者您没有启用 不太安全的应用程序 https://myaccount.google.com/lesssecureapps 或者您没有启用 显示解锁验证码 https://accounts.google .com/DisplayUnlockCaptcha,您需要同时打开它们。


15
投票

您需要启用应用程序安全性:

|*|如果您使用 Gmail,

Use :

    service: 'gmail',

Goto : 

    https://myaccount.google.com/lesssecureapps

Enable : 

    Allow less secure apps: ON

|*|如果您使用雅虎,

Use :

    service: 'yahoo',

Goto : 

    https://login.yahoo.com/account/security

Enable : 

    Allow apps that use less secure sign in

|*|如果您使用 Live 或 Hotmail,则无需启用任何功能。

Use :

    service: 'hotmail',

9
投票

您是否仔细检查过您的登录凭据?另外,您是否仔细检查了您的“发件人”地址以匹配您的电子邮件?

三周前,我使用 Nodemailer 进行了一些测试,并使用 github 页面上给出的 gmail 示例进行了一些测试,它的效果非常好:

https://github.com/andris9/Nodemailer

无效登录表示凭据输入错误/错误。


6
投票

就我而言, 仅打开不太安全的应用程序是不够的:https://myaccount.google.com/lesssecureapps

我还必须启用显示解锁验证码:https://accounts.google.com/DisplayUnlockCaptcha

这解决了我的问题,我能够使用 nodemail 和 gmail 发送电子邮件。

这件事实际上不是随机的,nodemailer 社区网站本身表示,如果打开不太安全的应用程序无法单独工作,则执行第二步以启用验证码。
https://community.nodemailer.com/using-gmail/

上图取自我分享的nodemailer文章链接。


2
投票

将此添加为单独的答案,因为接受的答案不再有效。

自 2022 年 5 月 30 日起,Google 不再支持安全性较低的应用。有一个有关此问题的 Nodemailer GitHub 问题 (https://github.com/nodemailer/nodemailer/issues/1424),其中包含更新的步骤:

...,
  auth: {
    user: '[email protected]', 
    pass: 'your_new_app_password', 
  },
...

0
投票

如果您有工作区用户帐户,您可能需要作为管理员:

  1. 创建用户组
  2. 将用户添加到该组
  3. 进入用户组设置,在安全下启用不安全的应用程序,这样其他组的电子邮件就不会受到影响
  4. 如果这还不够(在前三个步骤几个月后一切都很好之后,这对我有用),请转到 myaccount.google.com 并使用特定用户电子邮件和密码登录,再次转到安全并打开 less安全的应用程序。

0
投票
  1. 前往 https://myaccount.google.com/security

  2. 启用 2FA

  3. 为电子邮件创建应用程序密码

  4. 将该密码(16 个字符)复制到 Nodemailer auth 中的 pass 参数中。

当我按照以下步骤进行节点邮件程序时,工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.