NodeMailer是一個用Node寫的代理發(fā)送郵件的一個庫。
它默認的配置是用gmail,但是因為某些原因我們可以換成別的腊瑟。
首先安裝庫,nodemailer,nodemailer-smtp-transport,nodemailer-wellknown
cnpm install nodemailer nodemailer-smtp-transport nodemailer-wellknown --save
然后配置你的郵箱和密碼config.auth块蚌,然后填充mailOptions里的內(nèi)容闰非,然后執(zhí)行index.js,然后郵件就發(fā)送成功了峭范。
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);
});