NodeMailer是一个用Node写的代理发送邮件的一个库。
它默认的配置是用gmail,但是因为某些原因我们可以换成别的。
首先安装库,nodemailer,nodemailer-smtp-transport,nodemailer-wellknown
cnpm install nodemailer nodemailer-smtp-transport nodemailer-wellknown --save
然后配置你的邮箱和密码config.auth,然后填充mailOptions里的内容,然后执行index.js,然后邮件就发送成功了。
index.js
var nodemailer = require("nodemailer");
var smtpTransport = require('nodemailer-smtp-transport');
var wellknown = require("nodemailer-wellknown");
var config = wellknown("QQ");
config.auth = {
user:'@qqcom',
pass:''
}
var transporter = nodemailer.createTransport(smtpTransport(config));
var mailOptions = {
from:"name<@qq.com>",
to:"mail@126.com",
subject:"test",
text:"text plain",
html:"<div>div content</div>"
};
// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});