npm i --save nodemailer
var nodemailer = require("nodemailer");
app.get("/sendEmail",function(req,res){
var mailTrans = nodemailer.createTransport({
service:\'Gmail\',
auth:{
user:\'xxx@gmail.com\',
pass:xxx
},
port:587,
secure: false,
tls:{
rejectUnauthorized:false
}
});
mailTrans.sendMail({
from:\'xx@163.com\',
to:\'xxx@163.com\',
subject:\'my subject is here\',
text:\'thank your for your letter !\'
},function(err){
if(err) console.log(\'unabled to send email\'+err);
});
res.json({code:0})
})
question:
Reference:
https://stackoverflow.com/questions/31473292/etimedout-connect-error-using-nodemailer-in-nodejs-openshift-application
http://masashi-k.blogspot.com/2013/06/sending-mail-with-gmail-using-xoauth2.html
https://nodemailer.com/about/
update again .haha
app.get("/sendEmail",function(req,res){
nodemailer.createTestAccount((err, account) => {
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
host: "smtp.163.com",
secureConnection: true,
port:465,
auth: {
user: \'xxx@163.com\', // generated ethereal user
pass: \'\' // generated ethereal password(授权码)
}
});
// setup email data with unicode symbols
let mailOptions = {
from: \'xxx@163.com\', // sender address(注意和上面的要一致)
to: \'xxx@qq.com\', // list of receivers
subject: \'Hello ✔\', // Subject line
text: \'Hello world?\', // plain text body
html: `<div>
<h2 style="font-size:30px;color:#53ff53;">haha</h2>
<a href="http://www.cyany.com">1345648</a>`, // html body
attachments:[
{
filename:\'text.txt\',
content:\'hello,world\'
},
{
filename:\'text.txt\',
path:\'text.txt\'
}
]
};
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log(\'Server is ready to take our messages\');
}
});
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log(\'Message sent: %s\', info.messageId);
// Preview only available when sending through an Ethereal account
console.log(\'Preview URL: %s\', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
res.json({code:0})
})