一、安裝 nodemailer
復制代碼 代碼如下:
npm install nodemailer --save
復制代碼 代碼如下:
var nodemailer = require("nodemailer");
// 開啟一個 SMTP 連接池
var smtpTransport = nodemailer.createTransport("SMTP",{
host: "smtp.qq.com", // 主機
secureConnection: true, // 使用 SSL
port: 465, // SMTP 端口
auth: {
user: "xxxxxxxx@qq.com", // 賬號
pass: "xxxxxxxx" // 密碼
}
});
// 設置郵件內(nèi)容
var mailOptions = {
from: "Fred Foo <xxxxxxxx@qq.com>", // 發(fā)件地址
to: "2838890xx@qq.com, minimixx@126.com", // 收件列表
subject: "Hello world", // 標題
html: "<b>thanks a for visiting!</b> 世界,你好!" // html 內(nèi)容
}
// 發(fā)送郵件
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // 如果沒用,關閉連接池
});
復制代碼 代碼如下:
{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
name: 'AuthError',
data: '454 Authentication failed, please open smtp flag first!',
stage: 'auth' }
錯誤原因: 賬號未設置該服務
解決方案: QQ郵箱 -> 設置 -> 帳戶 -> 開啟服務:POP3/SMTP服務
復制代碼 代碼如下:
{ [SenderError: Mail from command failed - 501 mail from address must be same as authorization user]
name: 'SenderError',
data: '501 mail from address must be same as authorization user',
stage: 'mail' }
新聞熱點
疑難解答
圖片精選