Username and Password not accepted when using nodemailer?

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 https://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 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.

Sample usege

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)

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!

Leave a Comment