使用nodemailer发邮件

最近在学node,就想着node能不能像后台那样发送邮件,结果找到了nodemailer这个发邮件的插件,下列代码使用qq邮箱发邮件

代码:

'use strict';

const nodemailer = require('nodemailer');

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

let mailOptions = {
  from: '[email protected]', // 发送者邮箱
  to: '[email protected]', // 收信者邮箱
  subject: 'aaaa', // 邮件主题
  // 发送text或者html格式
  // text: 'Hello world?', // plain text body
  html: '<b>我在用node给你发邮件哦</b>' // 信的内容
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error);
  }
  console.log('Message sent: %s', info.messageId);
});

使用这个发邮件很方便,几乎自己配置一下就好了

至于那个 smtp授权码:打开设置-账户-拉到下面 开启IMAP/SMTP服务。因为是qq邮箱所以开启这个服务,好像如果是126邮箱就要开启POP3/SMTP服务。具体没有实验。

文章参考:https://www.cnblogs.com/chenjg/p/nodemailer.html

猜你喜欢

转载自blog.csdn.net/qq_36802917/article/details/83377090