使用nodemailer发邮件

xiaoxiao2025-06-03  60

最近在学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: 'xxxx@qq.com', //自己的邮箱 // 这里密码不是qq密码,是你设置的smtp授权码 pass: '', } }); let mailOptions = { from: 'xxxx@qq.com', // 发送者邮箱 to: 'xxxx@qq.com', // 收信者邮箱 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

转载请注明原文地址: https://www.6miu.com/read-5031208.html

最新回复(0)