const transporter =  nodemailer.createTransport({
        source: 'gmail',
      auth: {
        user: process.env.EMAIL,
        pass: process.env.EMAIL_PASSWORD
      },
    });

    const mailoptions = {
      from:`Anshul kumar <${process.env.EMAIL}>`,
      to: Email,
      subject: "Reset your Password",
      text: "hi welcome",
      html: `<a href="http://localhost:5000/password/resetpassword/${newid}">click on this link to reset your password</a>`,
    };
    
    transporter.sendMail(mailoptions, (error, info) => {
      if (error) {
        console.log(error);
        res.status(500).json({ message: "can not send email", success: false });
      } else {
        res
          .status(200)
          .json({ message: "email sent successfully", success: true });
      }
    }); 

And this was the error:

Error: connect ECONNREFUSED 127.0.0.1:587 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16) { errno: -4078, code: 'ESOCKET', syscall: 'connect', address: '127.0.0.1', port: 587, command: 'CONN' }

I got this error when I used host as 'spmt-gmail.com'

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 je10-20020a170903264a00b001bc2831e1a9sm268100plb.90 - gsmtp
    at SMTPConnection._formatError (C:\Users\anshul\Desktop\full stack expense tracker\node_modules\nodemailer\lib\smtp-connection\index.js:790:19)
    at SMTPConnection._actionAUTHComplete (C:\Users\anshul\Desktop\full stack expense tracker\node_modules\nodemailer\lib\smtp-connection\index.js:1564:34)   
    at SMTPConnection.<anonymous> (C:\Users\anshul\Desktop\full stack expense tracker\node_modules\nodemailer\lib\smtp-connection\index.js:546:26)
    at SMTPConnection._processResponse (C:\Users\anshul\Desktop\full stack expense tracker\node_modules\nodemailer\lib\smtp-connection\index.js:969:20)       
    at SMTPConnection._onData (C:\Users\anshul\Desktop\full stack expense tracker\node_modules\nodemailer\lib\smtp-connection\index.js:755:14)
    at SMTPConnection._onSocketData (C:\Users\anshul\Desktop\full stack expense tracker\node_modules\nodemailer\lib\smtp-connection\index.js:193:44)
    at TLSSocket.emit (node:events:513:28)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
  code: 'EAUTH',
  response: '535-5.7.8 Username and Password not accepted. Learn more at\n' +
    '535 5.7.8  https://support.google.com/mail/?p=BadCredentials je10-20020a170903264a00b001bc2831e1a9sm268100plb.90 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN'
}
1

There are 1 best solutions below

0
DuuEyn On

Recommendation:

Ensure that the SMTP server you are using is smtp.gmail.com and that you are sending through either port 587 (with TLS/STARTTLS enabled) or 465 (with SSL enabled). Continue using an app password if you have 2-Step Verification enabled in your Gmail account.

Reference:

Gmail SMTP settings