0
votes

I am trying to send an email using a simple nodejs app. But i am getting an erro The error is:

Error: Invalid login: 534-5.7.14 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 22-v6sm29249729pfl.126 - gsmtp at SMTPConnection._formatError (C:\Users\USER\Desktop\codeworkr\node_modules\nodemailer\lib\smtp-connection\index.js:606:19) at SMTPConnection._actionAUTHComplete (C:\Users\USER\Desktop\codeworkr\node_modules\nodemailer\lib\smtp-connection\index.js:1335:34) at SMTPConnection._responseActions.push.str (C:\Users\USER\Desktop\codeworkr\node_modules\nodemailer\lib\smtp-connection\index.js:366:26) at SMTPConnection._processResponse (C:\Users\USER\Desktop\codeworkr\node_modules\nodemailer\lib\smtp-connection\index.js:762:20) at SMTPConnection._onData (C:\Users\USER\Desktop\codeworkr\node_modules\nodemailer\lib\smtp-connection\index.js:558:14) at TLSSocket._socket.on.chunk (C:\Users\USER\Desktop\codeworkr\node_modules\nodemailer\lib\smtp-connection\index.js:510:47) at emitOne (events.js:116:13) at TLSSocket.emit (events.js:211:7) at addChunk (_stream_readable.js:263:12) at readableAddChunk (_stream_readable.js:250:11) at TLSSocket.Readable.push (_stream_readable.js:208:10) at TLSWrap.onread (net.js:597:20)

And my code is this:

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  secure: false,
 // port: 25,
  auth: {
    user: '[email protected]',
    pass: ''
  }
 /* tls: {
    rejectUnauthorized: false
  }*/
});



var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Sending Email using Node.js',
  text: 'Hello'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);=
  }

    });

I've removed the password for security purposes.

2

2 Answers

1
votes

Double check your credentials and also you need to allow less secure apps in gmail.

1) Go to https://myaccount.google.com/lesssecureapps

2) Enable Less Secure Apps option

0
votes

I have another solution for the same. If you want to remove password for security purpose, Then you can use my soltion too.

At first you need some sender account credential, you can create that from here: https://console.developers.google.com/projectselector/apis/credentials?supportedpurview=project

Node.js, Nodemailer, SMTP issue

Here is my testing code [file:server.js]

//start server
var http = require('http');
// Create a Transport instance using nodemailer
var nodemailer = require('nodemailer');

var smtpTransport = nodemailer.createTransport({
    host: "smtp.gmail.com",
    auth: {
        type: "OAuth2",
        user: "sender_email_id",
        clientId: "YOUR_CLIENT_ID",
        clientSecret: "YOUR_CLIENT_SECRET",
        refreshToken: "YOUR_REFRESH_TOKEN"
      }
});

var htmlBody = '<h2>Hello Body</h2>';
// Setup mail configuration
var mailOptions = {
    from: 'serder_email_id', // sender address
    to: 'recipent_email_id', // list of receivers
    subject: 'TEST SUBJECT', // Subject line
    text: 'Hello Body', // plaintext body
    html: htmlBody // html body
  };

var app = http.createServer(function (req, res) {
    // send mail
    smtpTransport.sendMail(mailOptions, function(error, info) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        if (error) {
            console.log('error', error)
            res.write('Error');
            res.end();
        } 
        console.log('Message %s sent: %s', info.messageId, info.response);
        smtpTransport.close();
        res.write('Email sent');
        res.end();
    });
}).listen(3000);