2

I'm trying to set up nodemailer for an app I'm creating. I'm receiving an error when I try and run the code.

This is my setup:

const nodemailer = require("nodemailer");
const xoauth2 = require("xoauth2");

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    xoauth2: xoauth2.createXOAuth2Generator({
      user: '****@gmail.com',
      clientId: '*******************************',
      clientSecret: '*****************************',
      refreshToken: '*****************************'
    })
  }
});

var mailOptions = {
  from: 'Dave <*******@gmail.com>',
  to: '*******@gmail.com',
  subject: 'Nodemailer test',
  text: 'Hello world'
}

transporter.sendMail(mailOptions, function(err, res) {
  if (err) {
    console.log(err);
  } else {
    console.log('Email sent');
  }
})

Obviously, I've double checked my client ID, secret and refresh token. But I get the following error:

{ 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 y22sm6749013wry.51 - gsmtp
    at SMTPConnection._formatError (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.j    s:557:19)
    at SMTPConnection._actionAUTHComplete (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/    index.js:1248:34)
    at SMTPConnection._responseActions.push.str  (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-conn    ection/index.js:340:26)
    at SMTPConnection._processResponse (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/ind    ex.js:706:20)
    at SMTPConnection._onData   (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:5    09:14)
    at TLSSocket._socket.on.chunk   (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index    .js:461:47)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
    at TLSWrap.onread (net.js:551:20)
  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=BadCrede    ntials y22sm6749013wry.51 -  gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN' }

This is the first time I've tried using nodemailer and I feel I'm obviously missing a key piece here but not sure what.

New error when using real email and password:

{ Error: Invalid login: 534-5.7.14    <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvZ
534-5.7.14  vX8SWYwnciMLBvsoC9zLFxi_9pKu2juDkWdPY4cJngQct2L0qjKFr3aF_SlAVCV816xj-8
534-5.7.14 eC36n8fZzITno-GnJdvwRSf6eIXfeU_ohzp07tc4S3LA0x2k9xPRwAjMlsWfNoa1Iz2GwX
534-5.7.14 AyBKjwT8nmD-wpNNK5J_bN9F3OI56XFAfw0NmjxKnUfhHXPoTs0sGCc6eRn_9hgYp2TyFe
534-5.7.14 MAd2gvxDCVp5O9V-yuGa9nrch8ey4> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 f135sm7148225wmd.7  - gsmtp
at SMTPConnection._formatError  (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index.js:557:19)
at SMTPConnection._actionAUTHComplete (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index.js:1248:34)
at SMTPConnection._responseActions.push.str (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:340:26)
at SMTPConnection._processResponse (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:706:20)
at SMTPConnection._onData (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:509:14)
at TLSSocket._socket.on.chunk (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:461:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:551:20)

code: 'EAUTH', response: '534-5.7.14 Please log in via your web browser and\n534-5.7.14 then try again.\n534-5.7.14 Learn more at\n534 5.7.14 https://support.google.com/mail/answer/78754 f135sm7148225wmd.7 - gsmtp', responseCode: 534, command: 'AUTH PLAIN' }

DevB1
  • 1,235
  • 3
  • 17
  • 43

4 Answers4

4

You need to set up the transporter differently, using the actual email and password of a real Google Account.

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'somerealemailaddress@gmail.com', //email address to send from
        pass: 'somerealemailpassword' //the actual password for that account
    }
});
AP.
  • 8,082
  • 2
  • 24
  • 33
wlh
  • 3,426
  • 1
  • 16
  • 32
  • Could you use oauth on gmail to get a user's password? If so, you can plug it into auth. But I'm not sure that is possible without the user inputing it directly, which is not a secure practice. If you want to send an email from a user's account to another address, see: https://developers.google.com/gmail/api/v1/reference/users/messages/send – wlh Apr 07 '17 at 17:56
  • hmmm, I tried the above method and then I still got an error which I've pasted into the original post – DevB1 Apr 07 '17 at 18:03
  • @DaveB1 The error you show indicates you are using xoauth. You shouldn't use that here. Have you tried entering an actual password? – wlh Apr 07 '17 at 18:04
  • yes I did try that as well and it produced the error I put in the original post now – DevB1 Apr 07 '17 at 18:07
  • Here's the documentation on using oauth2. Try getting your tokens in a separate function and storing them as an object, then passing the object into the transporter per the expected fields: https://nodemailer.com/smtp/oauth2/ – wlh Apr 07 '17 at 18:08
  • @DaveB1 The error with the email and password is that you have to adjust a setting on your gmail account and allow 'less secure' apps to access your gmail account. I set up a unique gmail account just for my site, and adjusted these settings to get it to work. See here: https://support.google.com/accounts/answer/6010255?hl=en – wlh Apr 07 '17 at 18:12
  • OK so I changed my code to: let transporter = nodemailer.createTransport({ service: 'gmail', auth: { user: '***e@gmail.com', //email address to send from pass: '****' //the actual password for that account } }); – DevB1 Apr 07 '17 at 18:41
  • and i changed my gmail settings to allow less secure apps but still having the same error – DevB1 Apr 07 '17 at 18:41
  • Sorry @DaveB1, that's as far as I can take you. – wlh Apr 07 '17 at 23:45
  • Great news! Good job! – wlh Apr 08 '17 at 00:29
  • @DaveB1 Hi, I have tried the same & still Im getting invalid error.. But for me in local it is working, after deploying it in to azure cloud it is not working..You have any idea about that? If so, please do reply.. – sasi Dec 26 '18 at 09:45
1

Similar question

Sample code: OAuth2 in Nodemailer

var email_smtp = nodemailer.createTransport({      
  host: "smtp.gmail.com",
  auth: {
    type: "OAuth2",
    user: "youremail@gmail.com",
    clientId: "CLIENT_ID_HERE",
    clientSecret: "CLIENT_SECRET_HERE",
    refreshToken: "REFRESH_TOKEN_HERE"                              
  }
});
0

const xoauth2 = require("xoauth2"); Actually in my case i didn't make use of the above code which i have mentioned and yet it worked fine...Just changed the google settings from the link sent in the error and it was fine. But i get the error especially when i try to use yahoo as the service provider instead of google.I have attached a screenshot of the error which i get for your reference. The error which i get while using yahoo as service provider

0

I can see and easy way in other post. I only click accounts.google.com/DisplayUnlockCaptcha

from; Nodemailer with Gmail service not working on heroku