我已经编写了使用 nodemailer node.js rest api 发送 gmail 的代码。这对我的本地系统服务器工作正常,但是当我在 windows-2000 服务器上部署相同的代码时,我无法发送 gmail 并收到内部服务器错误。
这是我正在使用的代码片段-
var nodemailer = require('nodemailer');
var stringify = require('json-stringify');
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port:587,
secure: false,
auth: {
user: '[email protected]',
pass: 'user@123'
}
});
// setup e-mail data with unicode symbols
module.exports={
create:function(req,res){
var params=req.allParams();
var order_details=JSON.stringify(params);
//console.log("order-->"+order_details);
var mailOptions = {
from: '[email protected]', // sender address
to: '[email protected]', // list of receivers
subject: "g-mail message ", // Subject line
html: order_details // html body
}
console.log("mailOptions"+JSON.stringify(mailOptions));
smtpTransport.sendMail(mailOptions, function(err, result){
console.log(result);
if(err){
return res.status(500).json("Error");
//return res.json("System Error")
}else{
return res.status(200).json("e-mail sent to customer");
}
smtpTransport.close();
});
}
}
当我从本地系统服务器调用 api 时,这是我对 conole.log(result)--> 的响应
{ accepted: [ '[email protected]' ],
rejected: [],
envelopeTime: 1094,
messageTime: 1124,
messageSize: 300,
response: '250 2.0.0 OK 1539452574 189-v6sm7035924pfe.121 - gsmtp',
envelope:
{ from: '[email protected]',
to: [ '[email protected]' ] },
messageId: '<[email protected]>' }
但是当我试图从我的 windows-2000 服务器调用 api 时,对于 line console.log(result)--> 我得到“未定义”。
所以我的问题是我是从 gmail.smtp 服务器收到此响应还是从系统无法访问 gmail.smtp 服务器。我试图从我的 windws-2000 服务器 ping gmail.smtp 服务器,但丢包率为 0%。所以可以从我的服务器访问 gmail.smtp?
我还尝试了端口 465,587,25 和 secure-true, false 选项。
对于错误日志,我得到了这个-
{ Error: self signed certificate in certificate chain at TLSSocket.
<anonymous> (_tls_wrap.js:1105:38) at emitNone (events.js:106:13)
at TLSSocket.emit (events.js:208:7) at TLSSocket._finishInit (_tls_wrap.js:639:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38) code: 'ECONNECTION', command: 'CONN' }}
感谢您在此问题上的宝贵意见。
var nodemailer = require('nodemailer');
var stringify = require('json-stringify');
// add this line before nodemailer.createTransport()
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port:587,
secure: false,
auth: {
user: '[email protected]',
pass: 'user@123'
}
});
现在它将起作用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句