首页 > 解决方案 > 错误:运行 Node.js 以发送电子邮件时连接 ECONNREFUSED

问题描述

我正在遵循本指南,将 node.js 路由用于后端 - https://blog.mailtrap.io/react-contact-form/。当我去运行我的后端时,我得到了这个错误 -

$ node index.js
{ Error: connect ECONNREFUSED 23.217.138.109:465
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1097:14)
  errno: 'ECONNREFUSED',
  code: 'ESOCKET',
  syscall: 'connect',
  address: '23.217.138.109',
  port: 465,

这是我的 index.js 的样子 -

var transport = {
    host: 'http://localhost:3000/', 
    port: 465,
    secure: true,
    auth: {
    user: creds.USER,
    pass: creds.PASS
  }
}

var transporter = nodemailer.createTransport(transport)

transporter.verify((error, success) => {
  if (error) {
    console.log(error);
  } else {
    console.log('Server is ready to take messages');
  }
});

router.post('/send', (req, res, next) => {
  var name = req.body.name
  var email = req.body.email
  var message = req.body.message
  var content = `name: ${name} \n email: ${email} \n message: ${message} `

  var mail = {
    from: name,
    to: 'myEmail@yahoo.com',  
    subject: 'New Message from Contact Form',
    text: content
  }

  transporter.sendMail(mail, (err, data) => {
    if (err) {
      res.json({
        status: 'fail'
      })
    } else {
      res.json({
       status: 'success'
      })
    }
  })
})

有谁知道可能出了什么问题?谢谢!

标签: node.jsreactjsexpressnodemailer

解决方案


推荐阅读