Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Username and Password not accepted when using nodemailer?

This is my settingController:

var sendSmtpMail = function (req,res) {   var transport = nodemailer.createTransport({   service:'gmail',    auth: {              user: "[email protected]",              pass: "qwerr@wee"         }    });    var mailOptions = {         from: "[email protected]",          to:'[email protected]',          subject: req.body.subject+"nodejs working ?",          text: "Hello world ?",       }     transport.sendMail(mailOptions, function(error, response){       if(error){          res.send("Email could not sent due to error: "+error);          console.log('Error');        }else{          res.send("Email has been sent successfully");          console.log('mail sent');       }    });  

in postman I got the error like that:

Email could not sent due to error:

Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at 535 5.7.8  https://support.google.com/mail/?p=BadCredentials g7sm64435626pfj.29 - gsmtp 
like image 648
uma raja Avatar asked Aug 03 '17 08:08

uma raja


People also ask

Does Nodemailer work with gmail?

Once less secure apps is enabled now nodemailer can use your gmail for sending the emails.

Does Nodemailer work on localhost?

In the localhost its not working because a secure and safetly connection is required for sending an email but using gmail[smtp(simple mail transfer protocol)] we can achieve it functionality.


1 Answers

I think that first you need to Allow less secure apps to access account setting in your google account - by default this settings is off and you simply turn it on. Also you need to make sure that 2 factor authentication for the account is disabled. You can check how to disable it here.

Then I use the following script to send emails from a gmail account, also tested with yahoo and hotmail accounts.

const nodemailer = require('nodemailer');  let transporter = nodemailer.createTransport({     host: 'smtp.gmail.com',     port: 587,     secure: false,     requireTLS: true,     auth: {         user: '[email protected]',         pass: 'your.password'     } });  let mailOptions = {     from: '[email protected]',     to: '[email protected]',     subject: 'Test',     text: 'Hello World!' };  transporter.sendMail(mailOptions, (error, info) => {     if (error) {         return console.log(error.message);     }     console.log('success'); }); 

If you put the previous code in send-email.js for example, open terminal and write:

node send-email 

You should see in the console - success, if the email was send successfully or the error message returned by nodemailer

Don't forget to first do the setting - Allow less secure apps to access account.

I hope this code will be useful for you. Good Luck!

like image 108
codtex Avatar answered Sep 20 '22 07:09

codtex