官网地址:https://nodemailer.com/about/
项目中添加插件nodemailer:
npm install nodemailer
发送邮件例子:
"use strict";
const nodemailer = require("nodemailer");
// async..await is not allowed in global scope, must use a wrapper
async function main() {
// Generate test SMTP service account from ethereal.email
// Only needed if you don't have a real mail account for testing
let testAccount = await nodemailer.createTestAccount();
// create reusable transporter object using the default SMTP transport
// // 创建发送邮件的对象
let transporter = nodemailer.createTransport({
host: "smtp.qq.com",
port: 456,
secure: true, // true for 465, false for other ports
auth: {
user: '你的邮箱', // generated ethereal user 发送方QQ邮箱
pass: '你的 mtp 验证码', // generated ethereal password
},
});
// send mail with defined transport object
let info = await transporter.sendMail({
from: '"Fred Foo " <foo@example.com>', // sender address
to: "bar@example.com, baz@example.com", // list of receivers
subject: "Hello ✔", // Subject line
text: "Hello world?", // plain text body
html: "<b>Hello world?</b>", // html body
});
console.log("Message sent: %s", info.messageId);
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@example.com>
// Preview only available when sending through an Ethereal account
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
}
main().catch(console.error);
注解:
- 上面的 mtp 验证码,(以qq邮箱为例)在 “设置”》“账户”》 里面找
2.其它邮箱 host 在 services.json
相对路径:\node_modules\nodemailer\lib\well-known\services.json
截图一些:
3.报错 “no function … ”
有没有安装 node.js, nodemail
4.在运行文件时报Error: self signed certificate in certificate chain 错误
解决办法,运行时候先把本电脑的防护先关了(防火墙,360,金山毒霸等)