我必须使用expressjs和nodejs在网站上的“提交”按钮上发送电子邮件,但由于在控制台上出现以下错误,因此无法在邮寄电话上发送电子邮件。我正在使用nodemailer在此处发送电子邮件。
错误: -
[Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
以下是代码:-
var nodemailer = require('nodemailer');
var ctrl={
post:function(req,res)
{
console.log('If this function returns promise it would be handled accordingly.'+req.body.Name);
// create reusable transporter object using SMTP transport
var transporter = nodemailer.createTransport({
service: 'smtp.gmail.com',
auth: {
user: '[email protected]',
pass: 'password'
},
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: 'Hello', // Subject line
text: 'Hello world', // plaintext body
html: '<b>Hello world</b>', // html body
replyTo:req.body.Email
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
console.log(error);
}else{
console.log('Message sent: ' + info.response);
}
});
return {Name:req.body.Name}; //req.body form post
//return {Name:req.query.Name}; //req.query url querystring
},
put:function(req,res){
return {};
}
}
module.exports = ctrl;
最佳答案
您应该使用XOAuth2令牌连接到Gmail。不用担心,Nodemailer已经知道这一点:
var smtpTransport = nodemailer.createTransport('SMTP', {
service: 'Gmail',
auth: {
XOAuth2: {
user: smtpConfig.user,
clientId: smtpConfig.client_id,
clientSecret: smtpConfig.client_secret,
refreshToken: smtpConfig.refresh_token,
accessToken: smtpConfig.access_token,
timeout: smtpConfig.access_timeout - Date.now()
}
}
};
检查此link以获得nodemailer SMTP。这将有助于澄清
关于node.js - 无法使用ExpressJS和Node.js发送电子邮件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27601594/