×

使用NodeJS发送邮件

96
静候那一米阳光
2017.08.06 12:42* 字数 5

模块

nodemailer

npm install nodemailer --save

实例

'use strict';
const nodemailer = require('nodemailer');
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    host: 'smtp-mail.outlook.com',
    port: 587,
    secure: false, // secure:true for port 465, secure:false for port 587
    auth: {
        user: 'boxuerixin@hotmail.com',
        pass: 'xxx'
    }
});
// 个人邮箱,总是被当作垃圾处理。。。
// let transporter = nodemailer.createTransport({
//     host: 'smtp.mxhichina.com',
//     port: 465,
//     secure: true, // secure:true for port 465, secure:false for port 587
//     auth: {
//         user: 'zhangdanyang@zhangdanyang.com',
//         pass: 'xxx'
//     }
// });

// setup email data with unicode symbols
let mailOptions = {
    from: '"张丹阳" <boxuerixin@hotmail.com>', // sender address
    to: 'boxuerixin@qq.com', // list of receivers
    subject: '来自zhangdanyang.com的邮件', // Subject line
    text: '貌似没啥用', // plain text body
    html: '<b>正文</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
    console.log("send")
    if (error) {
        return console.log(error);
    }
    console.log('Message %s sent: %s', info.messageId, info.response);
});
NodeJS
Web note ad 1