node使用nodemailer发送邮件

安装模块

npm install nodemailer  

代码

const nodemailer = require('nodemailer');
// 查找到有关QQ邮箱的相关信息在 /node_modules/nodemailer/lib/well-known/services.json
//  "QQ": {
//         "domains": ["qq.com"],
//         "host": "smtp.qq.com",
//         "port": 465,
//         "secure": true
//     },
let transporter = nodemailer.createTransport({
  host: "smtp.qq.com", // 你可以通过services.json 文件中来获取
  port: 465, // 发邮箱的端口号
  secure:true, //自否自定义端口
  auth: { // 权限认证
    user: 'xxxx@qq.com',
    pass: 'ckiibimilpipoh', //这是邮箱的授权码在邮箱后台生成的 
  }
})
 
let mailOptions = {
  from: 'xxxxx@qq.com', // 发邮件的账号
  to: 'yyyy@qq.com', // 收邮件的账号
  subject: 'NODEMAILER', // 标题
  text:'你发送的内容',
}
 
transporter.sendMail(mailOptions, (err, info) => {
  if (!err) {
    console.log('邮件已经发生完成')
  } else {
    console.log('发送失败',err)
  }
})

如何配置QQ邮箱后台

1.进入QQ邮箱后台,点击左上角的设置

2.点击账号,找到 IMAP/SMTP服务(什么是IMAP,它又是如何设置?) 点击开启

3.用手机发送短信,确认开启

4.获取授权信息

关于出现错误

发送失败 Error: Message failed: 550 Mail content denied. 
1.检查接收账号/发送账号是否正确,我第一次就写错账号了。

nodemailer 官网地址

https://nodemailer.com/about/
posted @ 2023-02-18 10:38  南风晚来晚相识  阅读(153)  评论(0)    收藏  举报