因此,我们使用包nodemailer发送电子邮件使用Node.js API。它在Windows环境下运行良好,使用smtp.gmail.com。一旦我们移动节点API到Ubuntu16.04,发送电子邮件停止工作。API的其余部分运行良好。在我们的gmail帐户上,我们启用了两个:
代码:
var transporter = nodemailer.createTransport({
//tried other combinations aswell
service: 'gmail',
host: 'smtp.gmail.com',
//tried port 465 and secure:true
port: 587,
secure: false,
auth: {
user: 'sender@gmail.com',
pass: 'password'
}
});
var mailOptions = {
from: 'sender@gmail.com',
to: 'reciever@gmail.com',
subject: 'Welcome',
html: "Hello":
};`我们尝试了其他邮件提供商,如outlook、zoho、yandex、...The每次都会超时。Ubuntu上的防火墙是禁用的,安装也是新鲜的。
溶液
问题在于服务提供商,Ubuntu所在的地方。
发布于 2018-10-03 15:14:54
这只是建议通过Google的API绕过网络问题。
您需要通过Google控制台的API管理器生成OAUTH客户端ID,然后选择"Web应用程序“;一旦有了授权代码,就可以使用以下代码:
<pre>
var nodemailer = require("nodemailer");
sails.log.debug("try to send mail");
var smtpTransport = nodemailer.createTransport("SMTP", {
service: "Gmail",
auth: {
XOAuth2: {
user: "xxx@gmail.com", // Your gmail address.
clientId: "YOUR_CLIENT_ID",
clientSecret: "YOUR_CLIENT_SECRET",
refreshToken: "REFRESH_TOKEN_YOU_JUST_FOUND"
}
}
});
var mailOptions = {
from: "xxx@gmail.com", // sender address
to: RECEIVER_EMAIL", // list of receivers
subject: "A_SUBJECT", // Subject line
// text: ", // plaintext body
html: htmlBody // html body
};
// send mail
smtpTransport.sendMail(mailOptions, function(error, info) {
if (error) {
sails.log.debug(error);
return res.notOk({
status: "error",
msg: "Email sending failed"
})
} else {
console.log("Message %s sent: %s", info.messageId, info.response);
return res.ok({
status: "ok",
msg: "Email sent"
})
}
smtpTransport.close();
});
</pre>https://stackoverflow.com/questions/52629917
复制相似问题