链接:https://nodemailer.com/about/

安装

  1. cnpm install nodemailer --save

使用

把下面的代码封装成函数,调用就直接发送了

  1. // 引入NodeMailer,发送邮件
  2. const nodemailer = require("nodemailer");
  3. // 发送配置
  4. let transporter = nodemailer.createTransport({
  5. host: "smtp.qq.com", // 发件服务器的主机名
  6. port: 465, //发件服务器的主机端口
  7. secure: true, // true 的话端口就是465, false的话用其他端口
  8. auth: {
  9. user: "xxxxx@qq.com", // 这里填你的邮箱
  10. pass: "你的授权码", // 这里填你的对应邮箱的授权码
  11. },
  12. });
  13. // 发送内容,from 一定要和上面auth.user 一致
  14. let info = await transporter.sendMail({
  15. from: '<xxxxx@qq.com>', // 发送方,一定要和
  16. to: "yyyyy@qq.com", // 收件方
  17. subject: "主题", // Subject line
  18. text: "内容", // plain text body
  19. });