我是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 用户名和密码作为凭据。
请帮助我。
原因之一可能是 Gmail 的“现代安全标准”保护。
检查您的 Gmail 收件箱中是否有任何主题为“Google 帐户:登录尝试被阻止”的新邮件
如果是,请打开邮件并点击链接 https://www.google.com/settings/security/lesssecureapps
将“访问不太安全的应用程序”设置为“打开”。再试一次,现在应该可以工作了。
特别是 2 个问题:或者您没有启用 不太安全的应用程序 https://myaccount.google.com/lesssecureapps 或者您没有启用 显示解锁验证码 https://accounts.google .com/DisplayUnlockCaptcha,您需要同时打开它们。
您需要启用应用程序安全性:
|*|如果您使用 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',
您是否仔细检查过您的登录凭据?另外,您是否仔细检查了您的“发件人”地址以匹配您的电子邮件?
三周前,我使用 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文章链接。
将此添加为单独的答案,因为接受的答案不再有效。
自 2022 年 5 月 30 日起,Google 不再支持安全性较低的应用。有一个有关此问题的 Nodemailer GitHub 问题 (https://github.com/nodemailer/nodemailer/issues/1424),其中包含更新的步骤:
...,
auth: {
user: '[email protected]',
pass: 'your_new_app_password',
},
...
如果您有工作区用户帐户,您可能需要作为管理员:
启用 2FA
为电子邮件创建应用程序密码
将该密码(16 个字符)复制到 Nodemailer auth 中的 pass 参数中。
当我按照以下步骤进行节点邮件程序时,工作正常。