Nodemailer configuration Gmail disable a less secure connection
Alternative solution

Nodemailer configuration Gmail disable a less secure connection Alternative solution

·

3 min read

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: "asdfg@limitlesscircle.com",
             pass: "asdf@wee"
        }
   });
   var mailOptions = {
        from: "asdf@limitlesscircle.com", 
        to:'werfsd@gmail.com', 
        subject: req.body.subject+"nodejs working ?", 
        text: "some text",  
    }
    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');
      } 
  });

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**

it's Update (2022-05-02)

As mentioned in the comments and directly quoted from Google:

On May 30 2022, you may lose access to apps that are using less secure sign-in technology

So the bottom code will probably stop working with Gmail. The solution is to enable 2-Step Verification and generate Application password, then you can use the generated password to send emails using nodemailer.To do so you need to do the following:

  1. Go to your Google account at myaccount.google.com

  2. Go to Security

  3. In "Signing in to Google" section choose 2-Step Verification - here you have to verify yourself, in my case it was with a phone number and a confirmation code send as text message. After that you will be able to enabled 2-Step Verification

  4. Back to Security in "Signing in to Google" section choose App passwords

  5. From the Select app drop down choose Other (Custom name) and put a name e.g. nodemailer

  6. A modal dialog will appear with the password. Get that password and use it in your code.

If there is still a problem, try clearing captcha by visiting https://accounts.google.com/DisplayUnlockCaptcha from your Google account.

currently, it's working below methods:

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'YOUR-USERNAME',
        pass: 'THE-GENERATED-APP-PASSWORD'
    }
});

send();

async function send() {
    const result = await transporter.sendMail({
        from: 'YOUR-USERNAME',
        to: 'RECEIVERS',
        subject: 'Hello World',
        text: 'Hello World'
    });

    console.log(JSON.stringify(result, null, 4));
}

Old Answer (before 2022-05-02)

the old way 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: 'your.gmail.account@gmail.com',
        pass: 'your.password'
    }
});

let mailOptions = {
    from: 'your.gmail.account@gmail.com',
    to: 'receivers.email@domain.example',
    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 mail.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 definetly useful for you. Good Luck!

Did you find this article valuable?

Support ramu k by becoming a sponsor. Any amount is appreciated!