在服务器代码上发送电子邮件时,Nodemailer突然出现以下错误。但是,当作为独立文件运行时,它可以工作!不知道为什么会出错 - 我在stackoverflow中检查了很多类似的问题 - 没有人帮忙。
我的结尾没有代码更改,如下所示
{ Error: connect ECONNREFUSED 127.0.0.1:587
at Object._errnoException (util.js:1022:11)
at _exceptionWithHostPort (util.js:1044:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
code: 'ECONNECTION',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 587,
command: 'CONN' }
以下是示例代码
let mailer = {
from: 'Company',
options: {
service: 'gmail',
auth: {
user: 'example@gmail.com',
pass: 'secret'
}
}
}
let email = nodemailer.createTransport(mailer.options);
let mailOptions = {};
mailOptions['to'] = 'example@gmail.com';
mailOptions['from'] = mailer.from;
mailOptions['subject'] = 'basic';
mailOptions['html'] = 'hello world';
email.sendMail(mailOptions, err => {
if (!err) {
console.log('sent email succesfully');
} else {
console.log('error sending email', err);
}
cbk(err, null);
});