[英]How do I send email from nodemailer in nodejs using gmail?
我遵循了文檔,但谷歌表示該應用程序的安全性低於其安全級別。 並且不再允許訪問此類應用程序的選項。
const nodemailer = require('nodemailer');
let mailTransporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'xyz@gmail.com',
pass: '*************'
}
});
let mailDetails = {
from: 'xyz@gmail.com',
to: 'abc@gmail.com',
subject: 'Test mail',
text: 'Node.js testing mail for GeeksforGeeks'
};
mailTransporter.sendMail(mailDetails, function(err, data) {
if(err) {
console.log('Error Occurs');
} else {
console.log('Email sent successfully');
}
});
在撰寫本文時,谷歌不再支持不太安全的應用程序。 而且您不能使用您的谷歌帳戶密碼。
您將不得不生成一個新的應用密碼。
應用密碼僅在開啟兩步驗證的情況下才有效。 按照以下步驟獲取應用密碼
const client = nodemailer.createTransport({
service: "Gmail",
auth: {
user: "username@gmail.com",
pass: "Google-App-Password-Without-Spaces"
}
});
client.sendMail(
{
from: "sender",
to: "recipient",
subject: "Sending it from Heroku",
text: "Hey, I'm being sent from the cloud"
}
)
不推薦使用不太安全的應用程序 util 14/06/2022
您需要在您的 google 帳戶中啟用兩步驗證https://myaccount.google.com/signinoptions/two-step-verification?rapt=AEjHL4Nm5j8lzlmlfGjIPZ3JQPadURur-daRW6csSgARqTeML2jsYhw3cctrxLoOZXEWpIivj6eXEcaFt_EfQct4VY40OwxOg
所以谷歌會發送一個應用密碼,你可以用 nodemailer 登錄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.