26

I'm trying to send the email gmail smtp but I'm getting the error:

My email and password is correct I'm using the nodemailer for sending the mail;

       var nodemailer = require('nodemailer');

                // create reusable transporter object using SMTP transport 
                var transporter = nodemailer.createTransport({
                    service: 'Gmail',
                    auth: {
                        admin: 'myuseremail.com',
                        pass: 'password'
                    }
                });

                var mailOptions = {
                    from: 'sender address', // sender address 
                    to: to, // list of receivers 
                    subject: 'Password Reset', // Subject line 
                    html: 'Your one time password is : <b>' + temporaryPassword + ' </b>' // html body 
                };

                transporter.sendMail(mailOptions, function (error, info) {
                    console.log(error,info);
              }

in log i'm getting the error:

    {
     [Error: Invalid login]
      code: 'EAUTH',
      response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/answer/14257 k5sm20957041pdo.48 - gsmtp',
      responseCode: 535
    } 

I try some link but that doesn't work:
https://laracasts.com/discuss/channels/general-discussion/help-email-doesnt-get-sent-with-gmail-smtp

Vishnu Mishra
  • 3,683
  • 2
  • 25
  • 36

8 Answers8

52
var transporter = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: 'user@gmail.com',
    pass: 'password'
  }
});

Try the above, and do the following which worked for me.

Login to https://www.google.com/settings/security/lesssecureapps and TURN ON Access for less secure apps.

I hope it will work for you too.

Thank you.

Jaspreet kaur
  • 630
  • 5
  • 19
pavani sri
  • 547
  • 4
  • 6
  • In case you have 2F authentication enabled, then the 'less secure apps' feature is disabled. You need to generate an app password. To do that, follow these steps: https://support.google.com/accounts/answer/185833 – Ali Alwash Mar 05 '18 at 21:43
  • I got the same error and the less secure apps tip was the solution. btw you can also login via `smtp://email@gmail.com:password@smtp.gmail.com` – Sagar V Aug 25 '19 at 13:59
32
  1. First of all, you have to enable the settings to allow less secure apps for the gmail account that you are using. Here is the link : https://myaccount.google.com/lesssecureapps

  2. Secondly, Allow access for "Display Unlock captcha option" (Allow access to your Google account). Here is the link : https://accounts.google.com/DisplayUnlockCaptcha

Jaspreet kaur
  • 630
  • 5
  • 19
2

https://myaccount.google.com/lesssecureapps

Click on this link, On your Less secure app access. so after that gmail will send email to your device , confirm that it is you. You are done happy coding

1

In case you're using OAuth2 and you're facing the issue above, configuring my transporter as shown below resolved my issue.

const transporter = nodemailer.createTransport({
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: {
    type: 'OAuth2',
    user: process.env.MAIL_USER,
    clientId: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    refreshToken: process.env.GOOGLE_CLIENT_REFRESH_TOKEN
  }
});
Kevin Danikowski
  • 4,620
  • 6
  • 41
  • 75
1

Google don't allow you to send direct e-mail to other's. First of all you have to create a app password .For that thing please follow below steps to get it done

  1. Go to your manage account section.
  2. Then click on the security link in left side.
  3. In the signing in to the google click on app password and it will ask you to sign in again after sign in
  4. It will ask "select app", for mail purpose "select mail" and after that select device from which you want to send mail.
  5. After all these steps, it will generate password for that thing and insert that like this
    var smtpTransport = nodemailer.createTransport({
    service: "Gmail",
    auth: {
        user: "example@example.com",
        pass: "password"
    }

That's it.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0
var transporter = nodemailer.createTransport({
  service: 'Gmail',
  auth: {
    user: 'user@gmail.com',
    pass: 'password'
  }
});

Try this.

pid
  • 11,472
  • 6
  • 34
  • 63
Aravinth
  • 397
  • 1
  • 4
  • 19
0

For me secure:true was giving ssl errors. I changed secure:false as below and worked for me.

let transporter = nodeMailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,    //<<here
    auth: {
        user: 'example@gmail.com',
        pass:'your gmail pass'
    }
});
N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
-1
let transporter = nodeMailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: true,
    auth: {
        user: 'example@gmail.com',
        pass:'your gmail pass'
    }
});
iBug
  • 35,554
  • 7
  • 89
  • 134
gowtham
  • 1
  • 1
  • 1
    While it seems to be a solution, it'd be nicer to provide some explanation for why it solves the problem. There are inexperienced users visiting SO who might not immediately understand what's the game changer here. – ElmoVanKielmo Jul 05 '18 at 12:56
  • it gives me an error like this : Error: 140528704497472:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:827: – srv_sud Dec 24 '18 at 07:47