繁体   English   中英

NodeMailer 登录无效

[英]NodeMailer Invalid Login

我是 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" <myname@gmail.com>',
        to : mail_id,   //user@gmail.com
        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 用户名和密码作为凭据。
请帮我。

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

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

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

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

你需要为应用程序启用安全性:

|*| 如果你使用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',

特别是 2 个问题:或者您没有启用不太安全的应用程序https://myaccount.google.com/lesssecureapps或者您没有启用Display Unlock Captcha https://accounts.google.com/DisplayUnlockCaptcha ,您需要打开它们两个。

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

3 周前,我使用 nodemailer 进行了一些测试,使用了 github 页面上给出的 gmail 示例,它的工作原理非常棒:

https://github.com/andris9/Nodemailer

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

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

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

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

这个东西不是随机的,实际上 nodemailer 社区网站本身说要执行第二步来启用验证码,如果打开不太安全的应用程序不能单独工作。
https://community.nodemailer.com/using-gmail/

在此处输入图片说明

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

如果您有工作区用户帐户,您可能需要以管理员身份执行以下操作:

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

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

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

...,
  auth: {
    user: 'yourmail@gmail.com', 
    pass: 'your_new_app_password', 
  },
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM