qq邮箱,先去qq邮箱设置中获取到授权码
var nodemailer = require("nodemailer");
var transporter = nodemailer.createTransport({
service: "qq",
auth: {
user: "XXX@qq.com", //这里填自己的 qq号
pass: "cqfib", //授权码,通过QQ邮箱获取
},
});
var mailOptions = {
from: "xx@qq.com", // 发送者 asdfghj
to: ["xx", "xx@163.com"], // 接受者,可以同时发送多个,以逗号隔开
subject: "nodemailer3.1.8邮件发送", // 标题
text: "这是我发的一封邮件,如果你开心,我也开心^_^", // 文本
// html: `<h2>nodemailer基本使用:</h2><h3>
// <a href="//www.greatytc.com/u/27f54f428a14" rel="external nofollow" >
// //www.greatytc.com/u/27f54f428a14</a></h3>`,
attachments: [
{
filename: "package.json",
path: "./package.json",
},
{
filename: "content",
content: "发送内容",
},
{
filename: "index.html",
path: "./index.html",
},
],
};
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
return;
}
console.log(info)
console.log("发送成功");
res.send("发送成功");
});
自己搭建的邮箱服务, 要数据用户名和密码, 指定服务ip和端口,开启ssl
var nodemailer = require("nodemailer");
const auth = process.env.auth
var transporter = nodemailer.createTransport({
host: 'mapi.xxx.com',
port: 587,
secureConnection:true,
auth
});
var mailOptions = {
from: "xxx", // 发送者 asdfghj
to: ["xxx", "xxx@163.com"], // 接受者,可以同时发送多个,以逗号隔开
subject: "nodemailer3.1.8邮件发送", // 标题
text: "这是我发的一封邮件,如果你开心,我也开心^_^", // 文本
// html: `<h2>nodemailer基本使用:</h2><h3>
// <a href="//www.greatytc.com/u/27f54f428a14" rel="external nofollow" >
// //www.greatytc.com/u/27f54f428a14</a></h3>`,
attachments: [
{
filename: "package.json",
path: "./package.json",
},
{
filename: "content",
content: "发送内容",
},
{
filename: "index.html",
path: "./index.html",
},
],
};
transporter.sendMail(mailOptions, function (err, info) {
if (err) {
console.log(err);
return;
}
console.log(info)
console.log("发送成功");
});