在节点Js中发送邮件

我需要使用Node Js发送电子邮件。 我用了下面的代码,

var nodemailer = require("nodemailer"); //var smtpTransport = require("nodemailer-smtp-transport") var smtpTransport = nodemailer.createTransport("SMTP",{ service: "Gmail", auth: { user: "example@gmail.com", pass: "xxxxxxxx" } }); smtpTransport.sendMail({ from: "example@gmail.com", // sender address to: "example@gmail.com", // comma separated list of receivers subject: "Test Mail", // Subject line text: "Hi, Test Mail"// plaintext body }, function(error, data){ if(error){ console.log(error); }else{ console.log("Mail Sent"); } }); 

当我运行这个代码时,我得到错误,

  { AuthError: Invalid login - 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsO 534-5.7.14 nSiseM4eUKjA64AZjuUfbMQisituUebEXGcWjgh1aePgnHNUs2gNlmIrmDHMY-VHZpi_-_ShO 534-5.7.14 5piXf2MTbZWxaEbFpal3ZqHZlUIvWc8vdCZBhjuhBlzVzALTv0Swes374SR7bwoalfGGIgHI 534-5.7.14 n7mxubIXPM_BM0ehxuh2tqDLL9n3wilsOpyShjhzLZ3hZg_baUdUT2lKp2MBzXRtblrD6nDw 534-5.7.14 hL-u0QueUcDAVi0XsZqjLmmhGozaM> Please log in via your web browser and 534-5.7.14 then try again. 534-5.7.14 Learn more at 534 5.7.14 https://support.google.com/mail/answer/78754 y205sm10903842pfb.13 - gsmtp at SMTPClient._actionAUTHComplete (/var/www/html/node_modules/simplesmtp/lib/client.js:925:23) at SMTPClient._onData (/var/www/html/node_modules/simplesmtp/lib/client.js:354:29) at emitOne (events.js:96:13) at TLSSocket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:172:18) at TLSSocket.Readable.push (_stream_readable.js:130:10) at TLSWrap.onread (net.js:542:20) name: 'AuthError', data: '534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsO\r\n534-5.7.14 nSiseM4eUKjA64AZjuUfbMQisituUebEXGcW1aePgnHNUs2ghjhNlmIrmDHMY-VHZpi_-_ShO\r\n534-5.7.14 5piXf2MTbZWxaEbFpal3ZqHZlUIvWc8vdCZBuhBlzVzALTvhjh0Swes374SR7bwoalfGGIgHI\r\n534-5.7.14 n7mxubIXPM_BM0ehxuh2tqDLL9n3wilsOpyShzLZ3hZg_bahjUdUT2lKp2MBzXRtblrD6nDw\r\n534-5.7.14 hL-u0QueUcDhjAVi0XsZqjLmmhGozaM> Please log in via your web browser and\r\n534-5.7.14 then try again.\r\n534-5.7.14 Learn more at\r\n534 5.7.14 https://support.google.com/mail/answer/78754 y205sm10903842pfb.13 - gsmtp', stage: 'auth' } 

我已经允许使用以下链接访问我的Gmail帐户

 https://accounts.google.com/b/0/DisplayUnlockCaptcha 

但是,我仍然得到这个错误。请帮助。

Solutions Collecting From Web of "在节点Js中发送邮件"

  1. 对于Gmail,您可能需要在Gmail帐户中configuration“允许安全性较低的应用” 。 点击这里。

  2. 如果您在这种情况下使用2FA,您将不得不创build一个应用程序特定的密码。

  3. 您也可能需要使用“允许访问您的Google帐户”来解锁您的帐户才能使用SMTP。

如果你愿意的话,我可以上传一个工作的例子在github上。 身份validation是这里的错误。

希望能帮助到你。