我已经在本地(http://localhost:8080
)设置了一个基本的 NodeJS 服务器(使用 nodemailer 模块),这样我就可以测试服务器是否真的可以发送电子邮件。
如果我正确理解了 SMTP 选项(如果我错了,请纠正我),我可以尝试从我的服务器向某人的电子邮件帐户发送电子邮件直接,或 我可以发送电子邮件,仍然使用 Node.js,但通过实际的电子邮件帐户(在本例中是我的个人 Gmail 帐户),即使用 SMTP。此选项要求我通过 NodeJS 远程登录该帐户。
所以在下面的服务器中,我实际上是在尝试使用 NodeJs 将电子邮件从我的个人电子邮件帐户发送到我的个人电子邮件帐户。
这是我的简单服务器:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport("SMTP", {
service: 'Gmail',
auth: {
user: '*my personal Gmail address*',
pass: '*my personal Gmail password*'
}
});
var http = require('http');
var httpServer = http.createServer(function (request, response)
{
transporter.sendMail({
from: '*my personal Gmail address*',
to: '*my personal Gmail address*',
subject: 'hello world!',
text: 'hello world!'
});
}).listen(8080);
但是,它不起作用。我收到了 Google 发来的电子邮件:
Google Account: sign-in attempt blocked If this was you You can switch to an app made by Google such as Gmail to access your account (recommended) or change your settings at https://www.google.com/settings/security/lesssecureapps so that your account is no longer protected by modern security standards.
我在 nodemailer GitHub 页面上找不到上述问题的解决方案。有没有人有解决方案/建议?
谢谢! :-)
最佳答案
答案在谷歌的消息中。
将访问不太安全的应用程序设置为启用
对于第二部分的问题,并针对
I'm actually simply following the steps from the nodemailer github page so there are no errors in my code
我会推荐你到 nodemailer 的 github 页面,以及这段代码:
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'gmail.user@gmail.com',
pass: 'userpass'
}
});
它与您的代码略有不同,事实上您有:nodemailer.createTransport("SMTP"
.
删除 SMTP 参数,它可以工作(刚刚测试过)。另外,为什么将它封装在 http 服务器中?以下作品:
var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'xxx',
pass: 'xxx'
}
});
console.log('created');
transporter.sendMail({
from: 'xxx@gmail.com',
to: 'xxx@gmail.com',
subject: 'hello world!',
text: 'hello world!'
});
关于node.js - 使用 nodemailer 通过 Node.js 发送电子邮件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196467/