1 安装nodemailer插件
npm install nodemailer --save
2 获得自己邮箱IMAP/SMTP/POP3服务授权码
前往自己的邮箱,找到设置,找到如上三种服务,如果是默认关闭,点击开启,通过手机验证码验证后会获取到一段授权码,保存好,等会儿要用
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b8a1d55b5bb67834bfc0602ea83cfda2.png)
3 引入nodemailer模块使用
"use strict"
const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport({
host: "smtp.163.com",
port: 465,
secure: true,
auth: {
user: "manengji@163.com",
pass: "**************"
}
});
let mailObj = {
from: '"Jack.Ma" <manengji@163.com>',
to: "manengji@qq.com,manengji@qq.com",
subject: "这是代码发送的邮件",
text: "hello world",
html: "<b>Hello world?</b>"
}
console.log('sending...')
transporter.sendMail(mailObj,(err,data)=>{
if(err) throw err
console.log('send mail success!')
});
以上