我想从我的NodeJs应用程序发送电子邮件。
const nodemailer = require('nodemailer');
const senderMail = "myemail@yahoo.com";
const emailTransporter = nodemailer.createTransport({
service: 'yahoo',
auth: {
user: senderMail,
pass: 'mypassword'
}
});
function getMailReceivers(mailReceivers) { // convert the string array to one string
var receivers = "";
for (var i = 0; i < mailReceivers.length; i++) {
receivers += mailReceivers[i];
if (i < mailReceivers.length - 1)
receivers += ", ";
}
return receivers;
}
function getMailOptions(mailReceivers, subj, content) { // set the options and return them
return {
from: senderMail,
to: getMailReceivers(mailReceivers),
subject: subj,
html: content
};
}
module.exports = {
sendHtmlMail: function(mailReceivers, subject, html) { // send an email
emailTransporter.sendMail(getMailOptions(mailReceivers, subject, html), function(error, info) {
if (error) {
throw error;
} else {
console.log(info.response);
}
});
}
};
所以我使用正确登录。我多次测试了它。我仍然收到此错误消息
Error: Invalid login: 535 5.7.0 (#MBR1212) Incorrect username or password.
at SMTPConnection._formatError (...\nodemailer\lib\smtp-connection\index.js:591:19)
at SMTPConnection._actionAUTHComplete (...\nodemailer\lib\smtp-connection\index.js:1320:34)
at SMTPConnection._responseActions.push.str (...\nodemailer\lib\smtp-connection\index.js:356:26)
at SMTPConnection._processResponse (...\nodemailer\lib\smtp-connection\index.js:747:20)
at SMTPConnection._onData (...\nodemailer\lib\smtp-connection\index.js:543:14)
at TLSSocket._socket.on.chunk (...\nodemailer\lib\smtp-connection\index.js:495:47)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
这里已经有一个问题,但它还没有答案
nodemailer and yahoo failing to send email
我使用此代码打电话给我的邮件
mailer.sendHtmlMail(["email1", "email2", "email3"], "Test Email", "<p>Success!</p>");
答案 0 :(得分:1)
我最近启用了yahoo实现,因此我想尝试回答您的问题并分享我的实现。您缺少emailTransporter对象中的一些字段。
首先,您缺少 host 字段。对于yahoo,该字段应为 smtp.mail.yahoo.com
Yahoo要求 port 是465或587。465对我有用。
您的服务字段正确无误。
最后,您需要一个安全字段。将其设置为 false 。
我不确定添加for循环会如何影响此效果,但是您应该先尝试仅发送一封电子邮件以确保其正常工作。
如果需要,可以添加调试器或记录器选项。我推荐记录器,它确实对我有所帮助。
因此,我将像这样更改您的emailTransporter对象:
const senderMail = "myemail@yahoo.com";
const emailTransporter = nodemailer.createTransport({
host: 'smtp.mail.yahoo.com',
port: 465,
service:'yahoo',
secure: false,
auth: {
user: senderMail,
pass: 'mypassword'
},
debug: false,
logger: true . <---highly recommend this one here
});
侧面注1:确保在yahoo帐户中将安全选项更改为“安全性较低的应用程序”。
旁注2:在此有效的同时,雅虎花了几乎 7-10分钟来发送一封电子邮件!对于开发(或真正的生产)而言不是很好,我改用了Gmail。我希望这有帮助。