简体   繁体   中英

Send Mail in Node Js

I need to send email using Node Js. I have used the following code,

    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");
           }
        });

When I run this code, I get error,

   { 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' }

I have allowed access to my gmail account using the following link

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

But still I am getting this error.Please help.

  1. For Gmail you may need to configure "Allow Less Secure Apps" in your Gmail account. Click Here.

  2. If you are using 2FA in that case you would have to create an Application specific password.

  3. You also may need to unlock your account with "Allow access to your Google account" to use SMTP.

I can upload a working example on github if you wish. Authentication is the error here.

Hope it helps.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM