11

I am new in Node js and I try send a message like example to other e-mail and when I run the file in the terminal from linux I get this error:

root@me:/home/memee/Desktop/test/NodeJS/mail# nano example.js
root@me:/home/memee/Desktop/test/NodeJS/mail# node example.js
{ Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials 77-v6sm41437784pga.40 - gsmtp
    at SMTPConnection._formatError (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
    at SMTPConnection._actionAUTHComplete (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:1335:34)
    at SMTPConnection._responseActions.push.str (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:366:26)
    at SMTPConnection._processResponse (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
    at SMTPConnection._onData (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
    at TLSSocket._socket.on.chunk (/home/memee/Desktop/test/NodeJS/mail/node_modules/nodemailer/lib/smtp-connection/index.js:510:47)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8  https://support.google.com/mail/?p=BadCredentials 77-v6sm41437784pga.40 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN' }

I understand that my error is the login but my password and email is correct.

My code :

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'example@gmail.com',
    pass: 'here I put my password'
  }
});

var mailOptions = {
  from: 'from@gmail.com',
  to: 'to@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);
  }
});

my version of node and npm :

root@me:~# node --version
v8.11.3
root@me:~# npm --v
6.1.0
root@me:~# 

I used this tutorial :tutorial

7 Answers7

17

I ran into this recently and it was related to the Less secure apps settings on the account.

Choosing "Allow users to manage their access to less secure apps" fixed it.

enter image description here

Richard A
  • 2,100
  • 15
  • 13
  • With the current google security, right after this fix please look into https://stackoverflow.com/a/51981381/6821441, since you will have to disable captcha – Abhishek Ekaanth Oct 25 '21 at 15:36
5

Turn on Allow less secure apps at https://www.google.com/settings/security/lesssecureapps

m9m9m
  • 1,655
  • 3
  • 21
  • 41
5

I'd the same problem earlier but after removing '@gmail.com' from username worked fine for me.
Error '535-5.7.8 Username and Password not accepted' occurs only in following mentioned cases:

  1. Wrong credentials.
  2. adding @gmail.com in the username, avoid it!
  3. two-factor authentication enabled. If yes, then disable it.
  4. https://myaccount.google.com/lesssecureapps if it is OFF, turn it ON to enable lesssecureapps.
kartik tyagi
  • 6,256
  • 2
  • 14
  • 31
2

your email and password are not matching, look at the error message: response: '535-5.7.8 Username and Password not accepted..

see this part of your code:

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'example@gmail.com', // here use your real email
    pass: 'here I put my password' // put your password correctly (not in this question please)
  }
});

If you make a login with your user and password, you will be able to send emails.

I used the same code to send this email, but with my credentials: I used the same code to send this email

jmsalcido
  • 1,057
  • 11
  • 16
2

I tried this and worked for me:

#1 Create a google app password https://support.google.com/accounts/answer/185833

#2 Use:

host: "smtp.gmail.com",
port: 587,
auth: {
  user: "Your username here e.g:abc@gmail.com",
  pass: "Your password here"
}
1

Step1 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

Read below instruction Enable imap

Short url for Enable IMAP in IMAP access section GO TO URL

const nodemailer = require("nodemailer");
let transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
port: 587,
secure: false, // true for 465, false for other ports
type:"oauth2",
auth: {
  user: "myusernmae@gmail.com", // generated ethereal user
  pass: "Mypassword", // generated ethereal password
}});

transporter.verify(async function(error, success) {
if (error) {
  console.log(error);
} else {
  let info = await transporter.sendMail({
      from: '"Fred Foo " <foo@example.com>', // sender address
      to: "manoj@gmail.com, baz@example.com", // list of receivers
      subject: "Hello ✔", // Subject line
      text: "Hello world?", // plain text body
      html: "<b>Hello world?</b>", // html body
  });
  console.log(info);
  console.log("Server is ready to take our messages");
}});

Working Example

manoj patel
  • 1,150
  • 12
  • 10
0

Here is the complete solution to it. Cheers!!!

If someone facing below issue

(firebase functions) Error: Forbidden Your client does not have permission to get URL /

The solution to it is :

  • Delete your Cloud Functions
  • Update Firebase CLI npm install -g firebase-tools
  • Re-deploy your functions
Maheshvirus
  • 6,749
  • 2
  • 38
  • 40