58

The code below is perfect to send emails using node.js code/program. However, still getting error mentioned in the title.

var nodemailer = require('nodemailer');

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

var mailOptions = {
  from: 'haideryaqoobengr@gmail.com',
  to: 'haideryaqoob720@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});
Haider Yaqoob
  • 1,725
  • 2
  • 11
  • 17

8 Answers8

84

Yes, code is perfect. However, you need to allow less secure apps from your google account to send emails. Through this link. Allow less secure apps From your Google Account

Haider Yaqoob
  • 1,725
  • 2
  • 11
  • 17
25

Google disabled less secure apps now so you need to setup Login with app password In summary you must setup your access with two factor authentication in order to allow the app password

transport: {
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: {
    user: 'contact@gmail.com',
    pass: 'app password',
  },
},
DariusV
  • 2,645
  • 16
  • 21
  • 4
    Thanks! For people who cannot see the option to setup the "App Passwords", make sure to enable two step verification on your Gmail account. I had to do that in order to see the option to access the "App Passwords" page on the [Google Account Security Page](https://myaccount.google.com/security). – James Jun 08 '22 at 08:12
21

There is very simple solution for this. Follow the following steps to send emails from your gmail using node (nodemailer)

  1. Step1: Open this link https://myaccount.google.com/security
  2. Step2: Enable 2 factor authentication
  3. Click on App passwords just below the 2 factor authentication
  4. From Select App options select Other and write your app name it could be any name like mycustomapp
  5. It will generate you the password copy the password from the popup and use the following code.
  6. Use that copied password in the Auth password section my password was this rkancqhzgvmzsdaqyx

  const nodemailer = require('nodemailer');

  const transporter = nodemailer.createTransport({
   service: 'gmail',
   host: 'smtp.gmail.com',
   port: 465,
   secure: true,
   auth: {
    user: 'niazi@gmail.com',
    pass: 'rkancqhzgvmzsdaqyx',
   },
  });

  const sendEmail = (email, token) => {
   const mailOptions = {
    from: 'niazi@gmail.com',
    to: email,
    subject: 'Email verification',
    html:
  '<p>Please click on the following link to verify your email address:</p>' +
  '<a href="http://localhost:3000/verify/' +
  token +
  '">http://localhost:3000/verify/' +
  token +
    '</a>',
  };

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log('Error in sending email  ' + error);
      return true;
    } else {
     console.log('Email sent: ' + info.response);
     return false;
    }
   });
  };

  module.exports = sendEmail;
Laeeq Khan Niazi
  • 568
  • 4
  • 11
  • Now you can import this file in your any file and use sendEmail function just pass a receiver email and other parameters that you required. – Laeeq Khan Niazi Jan 16 '23 at 14:52
  • 1
    Well, As of now, there is a change in the 3rd step, i.e. App passwords can be found at the bottom of the page of the Two-Factor Authentication screen. – Daniyal Malik Aug 29 '23 at 12:20
7

For gmail, Less secure app access is no longer available.

https://support.google.com/accounts/answer/6010255?authuser=1&hl=en&authuser=1&visit_id=637902855221010844-2637605848&p=less-secure-apps&rd=1

3

Google disabled less secure apps, to resolve the issue one need to setup "Login with app password" and to allow the app password "setup two factor authentication"

when 2-Step-Verification is on and one get a "password incorrect" error, sign in to your account and go to security, try to use an App Password.

transport: {
  host: 'smtp.gmail.com',
  port: 465,
  secure: true,
  auth: {
    user: 'contact@gmail.com',
    pass: 'app password',
  },
},
Neha
  • 170
  • 5
2

Go to Less secure app access . convert icon Allow less secure apps to ON.

Link : https://myaccount.google.com/lesssecureapps

Ejaz khan
  • 1,535
  • 8
  • 17
1

Less secure app no longer available: go to https://myaccount.google.com/apppasswords and setup "App password".

App password will work only if you have 2FA setup for your account, read more: https://support.google.com/accounts/answer/185833?hl=en

It's just different password per every registered app. It's always better to not use the main account password so makes total sense.

stopsopa
  • 408
  • 4
  • 20
0

You could create a password for your application to use that email. Go to manage account, then to security, you will see the add password option. You will need to add the name of your application then click generate.

Dharman
  • 30,962
  • 25
  • 85
  • 135