1 安裝nodemailer插件
npm install nodemailer --save
2 獲得自己郵箱IMAP/SMTP/POP3服務授權碼
前往自己的郵箱,找到設定,找到如上三種服務,如果是預設關閉,點選開啟,通過手機驗證碼驗證後會擷取到一段授權碼,儲存好,等會兒要用
3 引入nodemailer子產品使用
"use strict"
const nodemailer = require("nodemailer");
// 建立一個發送郵件對象執行個體,配置基礎資訊
//host,port等資訊,不同郵箱的配置内容可以在node_modules/lib/well_know/services.json中找到
let transporter = nodemailer.createTransport({
host: "smtp.163.com",
port: 465,
secure: true, // true for 465, false for other ports //如果端口号是465為true,反之false
auth: {
user: "[email protected]", // 發送方的郵箱位址
pass: "**************" // 此處填寫你的郵箱授權碼
}
});
// send mail with defined transport object
let mailObj = {
from: '"Jack.Ma" <[email protected]>', // 發送者,必須與上面的發送方資訊一緻,否則發送失敗
to: "[email protected],[email protected]", // 多個發送用逗号隔開
subject: "這是代碼發送的郵件", // 主題
text: "hello world", // 内容
html: "<b>Hello world?</b>" // html body
}
console.log('sending...')
//調用發送方法
transporter.sendMail(mailObj,(err,data)=>{
if(err) throw err
console.log('send mail success!')
});
以上