NodeMailer使用QQ邮箱来发送邮件

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);

});
    原文作者:进击的前端
    原文地址: https://www.jianshu.com/p/4cfc0e0868d9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞