簡體   English   中英

如何使用 gmail 從 nodejs 中的 nodemailer 發送電子郵件?

[英]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');
    }
});

在撰寫本文時,谷歌不再支持不太安全的應用程序。 而且您不能使用您的谷歌帳戶密碼。

您將不得不生成一個新的應用密碼。

應用密碼僅在開啟兩步驗證的情況下才有效。 按照以下步驟獲取應用密碼

  1. 轉到https://myaccount.google.com/security
  2. 啟用 2FA
  3. 為電子郵件創建應用程序密碼
  4. 將該密碼(16 個字符)復制到 Nodemailer auth 中的 pass 參數中。
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

並創建應用密碼https://myaccount.google.com/apppasswords?rapt=AEjHL4PZB2jtGe1EVQ1dS_jyte5bhU_hn44yc3rDR0k3BnmcIqzmocSf5sBDIN88P8vB7-owMYAWLK6m37OyA-_2C6IE7qapTg

所以谷歌會發送一個應用密碼,你可以用 nodemailer 登錄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM