哥伦布

nodemailer实现发送邮件功能

在nodejs项目中,遇到要向某一个邮箱发送邮件时可使用nodemailer,可支持QQ,163,126,Yahoo等等

安装:npm install nodemailer --S

复制代码
/*
 * @Description: 描述
 * @Version: 1.0
 * @Autor: Nanke_南柯
 * @Date: 2021-10-13 22:52:36
 * @LastEditors: Nanke_南柯
 * @LastEditTime: 2021-10-13 22:57:09
 */
const nodemailer = require('nodemailer');
 
// 开启一个 SMTP 连接池
let transporter = nodemailer.createTransport({
    host: 'smtp.qq.com',
    secureConnection: true, // use SSL
    port: 465,
    secure: true, // secure:true for port 465, secure:false for port 587
    auth: {
        user: 'xxxxxxxxx@qq.com',
        pass: 'xxxxxxxxxx' // QQ邮箱需要使用授权码
    }
});
 
// 设置邮件内容(谁发送什么给谁)
let mailOptions = {
    from: 'xxxxxxxxx@qq.com', // 发件人
    to: 'xxxxxxxxx@qq.com', // 收件人
    subject: '测试发送邮件', // 主题
    // text: '这是一封来自 Node.js 的测试邮件', // plain text body
    html: '<b>这是一封来自 南柯大帅哥 的测试邮件</b>', // html body
    // 下面是发送附件,不需要就注释掉
    // attachments: [{
    //         filename: 'test.md',
    //         path: './test.md'
    //     },
    //     {
    //         filename: 'content',
    //         content: '发送内容'
    //     }
    // ]
};
 
// 使用先前创建的传输器的 sendMail 方法传递消息对象
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log(`Message: ${info.messageId}`);
    console.log(`sent: ${info.response}`);
});
复制代码

 

posted @   南柯Dream丶  阅读(35)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示