Search code examples
javascriptnode.jssendmail

Error: connect ECONNREFUSED 127.0.0.1:465 send mail


I use the email account of my site [email protected] to send smtp alert messages to the email of my users. but it still gives me this error

Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {

errno: 'ECONNREFUSED', code: 'ESOCKET', syscall: 'connect', address: '127.0.0.1', port: 465, command: 'CONN' }

here is my code

// send mail function
exports.sendMailer = (optionsTransporterMail = OptionsTransporterMail, optionsMail = OptionsMail, callback) => {
  const nodemailer = require('nodemailer');

  let transporter = nodemailer.createTransport(optionsTransporterMail);

  transporter.sendMail(optionsMail, function (err, info) {
    callback(err, info)
  });
}

sendMailer({
  service: 'mail.mywebsite.com',
  port: 465,
  secure: true,
  auth: {
    user: '[email protected]',
    pass: '12345678'
  }
}, {
  from: '"My app <[email protected]>',
  to: [email protected],
  subject: "Notify",
  text: "Hello worlds",
  html: "Hello worlds"
}, (err, info) => {
  if err console.log(err);

  console.log(info)
})

it still gives this error

Error: connect ECONNREFUSED 127.0.0.1:465
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1137:16) {
  errno: 'ECONNREFUSED',
  code: 'ESOCKET',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 465,
  command: 'CONN'
}

Solution

  • I assume you want to use mail.mywebsite.com in order to send an email. In those terms, you have to set up host value in options, because it seems like by default it uses localhost (127.0.0.1).

    Please try the following code:

    sendMailer({
      host: 'mail.mywebsite.com',
      port: 465,
      secure: true,
      auth: {
        user: '[email protected]',
        pass: '12345678'
      }
    }, {
      from: '"My app <[email protected]>',
      to: [email protected],
      subject: "Notify",
      text: "Hello worlds",
      html: "Hello worlds"
    }, (err, info) => {
      if err console.log(err);
    
      console.log(info)
    })