NodeMailer Invalid Login

41.4k Views Asked by At

I am new to node.js programming .I am using nodemailer module for sending emails.

const nodemailer = require ('nodemailer'),
credentials=require('./credentials.js');
var mailTransport=nodemailer.createTransport({
    service:'Gmail',
    auth: {
        user : credentials.gmail.user,
        pass : credentials.gmail.password,
    }
});
function sendMail(mail_id){
    mailTransport.sendMail({
        from: ' "my name" <[email protected]>',
        to : mail_id,   //[email protected]
        subject : 'Hello',
        text: "Hello How do u do ?",
    },function(err,info){
        if(err){
            console.log('Unable to send the mail :'+err.message);
        }
        else{
            console.log('Message response : '+info.response);
        }
    });
}
exports.sendMail=sendMail;

This is my program for sending emails to different users. But I am getting Invalid Login . I don't have any idea why this is coming . I am new to node.js and server side scripting.
I am using my gmail username and password for credentials.
Please help me.

8

There are 8 best solutions below

1
On BEST ANSWER

Did you double-check your login credentials? Also did you double-check your "from" adress to match your email?

I used the nodemailer for some tests 3 weeks ago with the gmail example given on the github page and it worked like a charm:

https://github.com/andris9/Nodemailer

Invalid login indicates mistyped/wrong credentials.

1
On

U need to Enable Security for Apps :

|*| If u r using gmail,

Use :

    service: 'gmail',

Goto : 

    https://myaccount.google.com/lesssecureapps

Enable : 

    Allow less secure apps: ON

|*| If u r using yahoo,

Use :

    service: 'yahoo',

Goto : 

    https://login.yahoo.com/account/security

Enable : 

    Allow apps that use less secure sign in

|*| If u r using Live or Hotmail, No need to enable anything.

Use :

    service: 'hotmail',
2
On

In my case, turning on less secure apps only was not enough: https://myaccount.google.com/lesssecureapps

I had to enable display unlock captcha also: https://accounts.google.com/DisplayUnlockCaptcha

This solved my issue and i was able to send emails using nodemail and gmail.

This thing is not random actually nodemailer community site itself says to perform the second step to enable captcha, if turning on the less secure apps do not work alone.
https://community.nodemailer.com/using-gmail/

enter image description here

Above image is taken from the nodemailer article link i have shared.

0
On

if you have a workspace user account you may need to, as an admin:

  1. Create a user group
  2. Add the user to that group
  3. Go to the user group settings and enable unsecure apps under Security so that other groups emails won't be affected
  4. If that isn't enough (this worked for me after everything was fine for months with first three steps), go to myaccount.google.com and log in with the specific user email and password, go to Security again and turn on less secure apps.
3
On

One reason could be the 'modern security standard' protection from Gmail.

Check you gmail inbox for any new mail having subject "Google Account: sign-in attempt blocked"

If yes, open the mail and click on the link https://www.google.com/settings/security/lesssecureapps

set 'Access for less secure apps' to 'Turn on'. Try again, it should be working now.

0
On
  1. Go to https://myaccount.google.com/security

  2. Enable 2FA

  3. Create App Password for Email

  4. Copy that password (16 characters) into the pass parameter in Nodemailer auth.

Working fine when I followed this steps to proceed the nodemailer.

0
On

Adding this as a separate answer, since the accepted answer no longer works.

Since May 30, 2022, Google no longer supports less secure apps. There is a nodemailer GitHub issue on this (https://github.com/nodemailer/nodemailer/issues/1424) with the updated steps:

...,
  auth: {
    user: '[email protected]', 
    pass: 'your_new_app_password', 
  },
...
1
On

Particularly 2 issues: or you don't have enabled Less Secure Apps https://myaccount.google.com/lesssecureapps or you don't have enabled Display Unlock Captcha https://accounts.google.com/DisplayUnlockCaptcha, you need to turn on both of them.