Error: connect ECONNREFUSED 127.0.0.1:465 send mail

Error: connect ECONNREFUSED 127.0.0.1:465 send mail

我使用我网站的电子邮件帐户 contact@monsite.com 将 smtp 警报消息发送到我的用户的电子邮件。但它仍然给我这个错误

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

错误号:'ECONNREFUSED', 代码:'ESOCKET', 系统调用:'connect', 地址:'127.0.0.1', 端口:465, 命令:'CONN' }

这是我的代码

// 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: 'contact@mywebsite.com',
    pass: '12345678'
  }
}, {
  from: '"My app <contact@mywebsite.com>',
  to: user@domain.com,
  subject: "Notify",
  text: "Hello worlds",
  html: "Hello worlds"
}, (err, info) => {
  if err console.log(err);

  console.log(info)
})

它仍然报错

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

我假设您想使用 mail.mywebsite.com 来发送电子邮件。在这些术语中,您必须在选项中设置 host 值,因为默认情况下它似乎使用 localhost (127.0.0.1).

请尝试以下代码:

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

  console.log(info)
})