一、安裝 nodemailer
// 開啟一個 SMTP 連接池
var smtpTransport = nodemailer.createTransport("SMTP",{
host: "smtp.qq.com", // 主機
secureConnection: true, // 使用 SSL
port: 465, // SMTP 端口
auth: {
user: "xxxxxxxx@qq.com", // 賬號
pass: "xxxxxxxx" // 密碼
}
});
// 設置郵件內容
var mailOptions = {
from: "Fred Foo <xxxxxxxx@qq.com>", // 發件地址
to: "2838890xx@qq.com, minimixx@126.com", // 收件列表
subject: "Hello world", // 標題
html: "<b>thanks a for visiting!</b> 世界,你好!" // html 內容
}
// 發送郵件
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // 如果沒用,關閉連接池
});
錯誤原因: 賬號未設置該服務
解決方案: QQ郵箱 -> 設置 -> 帳戶 -> 開啟服務:POP3/SMTP服務
新聞熱點
疑難解答