我尝试使用node.js - nodemailer模块发送电子邮件,我的整个代码看起来像
var http=require("http");
var nodemailer=require("nodemailer");
http.createServer(function(req,res){
res.writeHead(200, {"Content-Type": "text/plain"});
var transporter = nodemailer.createTransport('SMTP',{
service: 'gmail',
auth: {
user: 'myemail@gmail.com', // my mail
pass: 'mypassword'
}
});
var mailOptions = {
from: 'Fred Foo ✔ <foo@blurdybloop.com>', // sender address
to: 'myemail@gmail.com', // the same mail = want to send it to myself
subject: 'Hello ✔', // Subject line
text: 'Hello world ✔', // plaintext body
html: '<b>Hello world ✔</b>' // html body
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
res.end("hllo");
}).listen(4000)
但是当我运行它时,它会抛出“登录”错误,altought我的名字/传递是正确的,整个错误看起来像这样
{ [AuthError: Invalid login - 534-5.7.14 <https://accounts.google.com/ContinueSi
gnIn?sarp=1&scc=1&plt=AKgnsbtWV
534-5.7.14 FHw6FqlXkg3nIQW7AtsO7o0r-4EtD7eTtnrgHeXxeULYqUtHjrnTB9R647mlR7GY9Z4lS
z
534-5.7.14 WuzYYnNwNBjIyrjNzHk-o9sIV_vIsbjgF5HsFPtJl5S0dSf3eyNCX_E8tQ_Z6s4Z1mUe5
i
534-5.7.14 p8Y5YagzdN5RSkjpB1yTu3TfWHSTLisczIZphhLfhlz-v86tAB8MX-4jRqA51hfJYy0ko
F
534-5.7.14 qytMeIbs1kUZEJzPIabmjRi87DIk> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 cx1sm17733852wib.0 - gs
mtp]
name: 'AuthError',
data: '534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt
=AKgnsbtWV\r\n534-5.7.14 FHw6FqlXkg3nIQW7AtsO7o0r-4EtD7eTtnrgHeXxeULYqUtHjrnTB9R
647mlR7GY9Z4lSz\r\534-5.7.14 WuzYYnNwNBjIyrjNzHk-o9sIV_vIsbjgF5HsFPtJl5S0dSf3ey
NCX_E8tQ_Z6s4Z1mUe5i\r\n534-5.7.14 p8Y5YagzdN5RSkjpB1yTu3TfWHSTLisczIZphhLfhlz-v
86tAB8MX-4jRqA51hfJYy0koF\r\n534-5.7.14 qytMeIbs1kUZEJzPIabmjRi87DIk> Please log
in via your web browser and\r\n534-5.7.14 then try again.\r\n534-5.7.14 Learn
more at\r\n534 5.7.14 https://support.google.com/mail/answer/78754 cx1sm1773385
2wib.0 - gsmtp',
stage: 'auth' }
我必须在我的gmail中设置一些东西才能允许它从中发送电子邮件或我该如何解决它
答案 0 :(得分:24)
尝试安装nodemailer-smtp-transport,然后在createTransport函数中使用它。
var smtpTransport = require('nodemailer-smtp-transport');
var transport = nodemailer.createTransport(smtpTransport({
service: 'gmail',
auth: {
user: 'myemail@gmail.com', // my mail
pass: 'mypassword'
}
}));
并尝试在您的Google帐户中启用此功能: https://www.google.com/settings/security/lesssecureapps
我真的认为这应该是你的身份验证问题
答案 1 :(得分:2)
在使用非安全端口后,它可以与我合作
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 587,
secure: false, // secure:true for port 465, secure:false for port 587
auth: {
user: 'xxxx@gmail.com',
pass: 'xxxx'
}
});