使用 Nodemailer 提交电子邮件时出现 NodeJs 错误
NodeJs Error When Submitting Email using Nodemailer
我已经按照这些步骤设置了 nodemailer
1) 允许访问 gmail 中安全性较低的应用程序
2) 在app.js
中写入如下内容
app.post('/reachus/send',function(req,res){
var transporter=nodemailer.createTransport({
service:'Gmail',
auth: {
user:'my@gmail.com',
pass:'***'
}
});
var mailOptions={
from:'Naveen DK <my@gmail.com>',
to:'my@gmail.com',
subject:'Email Sent from your website',
text:'You have a submission with the following details.. Name: '+req.body.name +'Email: '+req.body.email+' Message: '+ req.body.message,
html: '<p>You have a submission with the following details..</p><ul><li> Name :'+req.body.name + ' </li><li>Email: '+req.body.email +'</li><li>Message ' + req.body.message +'</li></ul>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
res.redirect('/');
} else{
console.log('Message Sent ' + info.response);
res.redirect('/');
}
});
});
3) 单击“提交电子邮件”后出现以下错误
{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
code: 'ECONNECTION',
errno: 'ECONNRESET',
syscall: 'read',
command: 'CONN
}
请查找以下 2 个视频了解更多详情
1 https://www.dropbox.com/s/nc1zvivlfpabj6h/HowMyCodeLooksLike.wmv?dl=0
2 https://www.dropbox.com/s/tfsqu6ir90s682h/ErrorOnceSubmissionDone.wmv?dl=0
提前致谢
纳文
使用下面的代码从 nodemailer 发送电子邮件,在函数内部传递你的参数,你将得到你的结果。
var AppConfig = {
'sendEmailID': 'useremail',
'sendEmailFromName': 'senderemail',
'sendEmailPassword': 'password'
}
function SendEmail(toEmail, Subject, html) {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "username",
pass: AppConfig.sendEmailPassword
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: AppConfig.sendEmailFromName, // sender address
to: toEmail, // list of receivers
subject: Subject, // Subject line
html: html // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log("ERROR----" + error);
}
console.log('Message sent: ' + info.response);
});
}
根据@chetan mekha 的回复,我更改了我的代码如下:
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
但是出现了另一个错误:{ [错误:证书链中的自签名证书]代码:'ECONNECTION',命令:'CONN'}但是添加了行
rejectUnauthorized: false under ciphers 使它起作用了!所以对我有用的最终代码片段看起来像这样..
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});
我已经按照这些步骤设置了 nodemailer 1) 允许访问 gmail 中安全性较低的应用程序 2) 在app.js
中写入如下内容app.post('/reachus/send',function(req,res){
var transporter=nodemailer.createTransport({
service:'Gmail',
auth: {
user:'my@gmail.com',
pass:'***'
}
});
var mailOptions={
from:'Naveen DK <my@gmail.com>',
to:'my@gmail.com',
subject:'Email Sent from your website',
text:'You have a submission with the following details.. Name: '+req.body.name +'Email: '+req.body.email+' Message: '+ req.body.message,
html: '<p>You have a submission with the following details..</p><ul><li> Name :'+req.body.name + ' </li><li>Email: '+req.body.email +'</li><li>Message ' + req.body.message +'</li></ul>'
};
transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
res.redirect('/');
} else{
console.log('Message Sent ' + info.response);
res.redirect('/');
}
});
});
3) 单击“提交电子邮件”后出现以下错误
{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
code: 'ECONNECTION',
errno: 'ECONNRESET',
syscall: 'read',
command: 'CONN
}
请查找以下 2 个视频了解更多详情
1 https://www.dropbox.com/s/nc1zvivlfpabj6h/HowMyCodeLooksLike.wmv?dl=0
2 https://www.dropbox.com/s/tfsqu6ir90s682h/ErrorOnceSubmissionDone.wmv?dl=0
提前致谢
纳文
使用下面的代码从 nodemailer 发送电子邮件,在函数内部传递你的参数,你将得到你的结果。
var AppConfig = {
'sendEmailID': 'useremail',
'sendEmailFromName': 'senderemail',
'sendEmailPassword': 'password'
}
function SendEmail(toEmail, Subject, html) {
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "username",
pass: AppConfig.sendEmailPassword
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
// setup e-mail data with unicode symbols
var mailOptions = {
from: AppConfig.sendEmailFromName, // sender address
to: toEmail, // list of receivers
subject: Subject, // Subject line
html: html // html body
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
return console.log("ERROR----" + error);
}
console.log('Message sent: ' + info.response);
});
}
根据@chetan mekha 的回复,我更改了我的代码如下:
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3'
}
});
但是出现了另一个错误:{ [错误:证书链中的自签名证书]代码:'ECONNECTION',命令:'CONN'}但是添加了行 rejectUnauthorized: false under ciphers 使它起作用了!所以对我有用的最终代码片段看起来像这样..
var smtpTransport=nodemailer.createTransport({
host: 'smtp.gmail.com',
port: '587',
auth: {
user: "**",
pass: "**"
},
secureConnection: 'false',
tls: {
ciphers: 'SSLv3',
rejectUnauthorized: false
}
});