24

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" <myname@gmail.com>',
        to : mail_id,   //user@gmail.com
        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.

Sri Harsha
  • 641
  • 2
  • 8
  • 18

7 Answers7

40

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.

Sachin
  • 603
  • 5
  • 10
  • thank you! I suddenly ran into this issue, and changing this setting fixed it. – user1577390 Nov 23 '18 at 15:55
  • In my case the issue still existed. Check [Gmail - display unlock captcha](https://www.google.com/accounts/DisplayUnlockCaptcha) – Shivam Jha Jun 20 '21 at 12:23
  • Looks like less secure apps is no longer supported. Nodemailer GitHub issue on this (https://github.com/nodemailer/nodemailer/issues/1424) has the updated steps. – Nesho Neshev Sep 21 '22 at 15:29
16

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.

Daniel Danielecki
  • 8,508
  • 6
  • 68
  • 94
  • You really need to allow access to the two.. Both less secure app and captacha. The two links above really worked for me.. Thanks. one upvote for u – Yinka Oct 31 '19 at 11:39
15

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',
Sujay U N
  • 4,974
  • 11
  • 52
  • 88
9

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.

smartbart24
  • 504
  • 4
  • 4
6

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.

d1fficult
  • 931
  • 8
  • 18
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Dima Kozhevin Jul 30 '20 at 13:31
  • Thank you for mentioning this, i have updated the resource! @DimaKozhevin – d1fficult Jul 30 '20 at 16:15
2

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: 'yourmail@gmail.com', 
    pass: 'your_new_app_password', 
  },
...
Nesho Neshev
  • 739
  • 5
  • 4
0

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.
AMDP
  • 327
  • 2
  • 12