Skip to content

node发送邮件库nodemailer简单使用

npm地址

安装

shell
npm install nodemailer --save

示例

js

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    // host: 'smtp.qq.com',
    service: 'qq', // 使用了内置传输发送邮件 查看支持列表:https://nodemailer.com/smtp/well-known/
    port: 465, // SMTP 端口
    secureConnection: true, // 使用了 SSL
    auth: {
        user: 'xxxxxx@qq.com',
        // 这里密码不是qq密码,是你设置的smtp授权码
        pass: 'xxxxxx',
    }
});

let mailOptions = {
    from: '"Shang" <xxxxx@qq.com>', // sender address
    to: 'xxxxxxxx@163.com', // list of receivers
    subject: 'Hello', // Subject line
    // 发送text或者html格式
    // text: 'Hello world?', // plain text body
    html: '<b>Hello world?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message sent: %s', info.messageId);
    // Message sent: <04ec7731-cc68-1ef6-303c-61b0f796b78f@qq.com>
});
/src/technology/dateblog/2025/04/20250420-node%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6%E5%BA%93nodemailer%E7%AE%80%E5%8D%95%E4%BD%BF%E7%94%A8.html