在节点J中发送邮件

时间:2016-06-07 05:22:41

标签: javascript node.js email nodemailer

我需要使用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

但我仍然收到此错误。请帮助。

2 个答案:

答案 0 :(得分:3)

  1. 对于Gmail,您可能需要在Gmail帐户中配置“允许不太安全的应用”Click Here.

  2. 如果您在这种情况下使用2FA,则必须创建特定于应用程序的密码。

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

  4. 如果您愿意,我可以在github上传一个工作示例。身份验证是错误。

    希望它有所帮助。

答案 1 :(得分:0)

我遇到了同样的问题。使用浏览器登录您的Google帐户,然后访问以下链接解锁:https://accounts.google.com/DisplayUnlockCaptcha