Error when sending email using nodemailer

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' }


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: '[email protected]', //email address to send from
        pass: 'somerealemailpassword' //the actual password for that account
    }
});

Similar question

Sample code: OAuth2 in Nodemailer

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