node.js发送邮件

nodemailer模块的特点:

  • 使用Unicode编码
  • 支持Windows系统,不需要安装依赖
  • 支持纯文本和HTML格式
  • 支持发送附件(包括大型附件)
  • 在HTML中嵌入图片
  • 支持SSL/STARTTLS安全协议
  • 不同的传输方法,可以使用内置也可以使用外部插件的形式
  • 提供自定义插件支持(比如增加DKIM签名,使用markdown代替HTML等等)
  • 支持XOAUTH2登录验证(以及关于更新的令牌反馈)

安装使用

npm install nodemailer --save   

github:https://github.com/nodemailer/nodemailer

var nodemailer = require('nodemailer');
var transporter = nodemailer.createTransport({
    //https://github.com/andris9/nodemailer-wellknown#supported-services 支持列表
    service: 'qq',
    port: 465, // SMTP 端口
    secureConnection: true, // 使用 SSL
    auth: {
        user: '768065158@qq.com',
        //这里密码不是qq密码,是你设置的smtp密码
        pass: '*****'
    }
});

// NB! No need to recreate the transporter object. You can use
// the same transporter object for all e-mails

// setup e-mail data with unicode symbols
var mailOptions = {
    from: 'xxx@qq.com', // 发件地址
    to: 'xxx@qq.com', // 收件列表
    subject: 'Hello sir', // 标题
    //text和html两者只支持一种
    text: 'Hello world ?', // 标题
    html: '<b>Hello world ?</b>' // html 内容
};

// 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.cnblogs.com/cosyer/p/6676650.html