nodemailer 连接 ECONNREFUSED

问题描述 投票:0回答:1

我正在为我的投资组合工作,我使用 nodemailer express 和 nodemon 我需要为 nodemailer 工作。我不知道这里有什么问题

// 服务器脚本

const contactForm = document.querySelector('.cnt-form');
let message = document.getElementById('message');
let name = document.getElementById('name');
let email = document.getElementById('email');
let subject = document.getElementById('subject');
let phone = document.getElementById('phone');
contactForm.addEventListener('submit', (e)=>{
    e.preventDefault();
    let formData = {
        message: message.value,
        name: name.value,
        email: email.value,
        subject: subject.value,
        phone: phone.value
    }
    let xhr = new XMLHttpRequest();
    xhr.open('POST', '/');
    xhr.setRequestHeader('content-type', 'application/json');
    xhr.onload = function(){
        console.log(xhr.responseText);
        if(xhr.responseText == 'success'){
            alert('Email Sent Successfully');
            message.value = '';
            name.value = '';
            email.value = '';
            subject.value = '';
            phone.value = '';
        }else{
            alert('Something went wrong!');
        }
    }
    xhr.send(JSON.stringify(formData));
});

这是我的客户端代码

const express = require('express');
const nodemailer = require('nodemailer');
const app = express();


const PORT = process.env.PORT || 5000;

app.use(express.static('public'));
app.use(express.json())
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/public/index.html');
})
app.post('/', (req, res) => {
    console.log(req.body);
    const transporter = nodemailer.createTransport({
        service: 'smpt.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'xxxxxxxx',
            pass: 'xxxxxxxx'
            
        }
    })
    const mailOptions = {
        from: req.body.email,
        to: '[email protected]',
        subject: `Message from ${req.body.email}:  ${req.body.subject}`,
        text: req.body.message,
        contact: req.body.phone
    }
    transporter.sendMail(mailOptions, (error, info)=>{
        if(error){
            console.log(error);
            res.send('error');
        }else{
            console.log('Email sent: ' + info.response);
            res.send('success');
        }
    })
});
app.listen(PORT, ()=>{
    console.log(`server running on port ${PORT}`);
});

这是我的服务器端

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

上面的代码是错误的

我尝试更改安全端口但仍然无法正常工作,我该怎么办?谢谢

node.js smtp nodemailer
1个回答
0
投票
  1. 检查电子邮件服务器是否正在运行并可在指定的主机和端口上访问:

您可以通过在终端中使用“telnet”命令来测试电子邮件服务器是否正在运行并且可以在指定的主机和端口上访问。这是一个例子:

telnet smtp.gmail.com 465

如果电子邮件服务器正在运行并且可以访问,您应该会看到类似这样的响应:

Trying 74.125.133.109...
Connected to smtp.gmail.com.
Escape character is '^]'.
220 smtp.gmail.com ESMTP a15sm2599253qkb.68 - gsmtp

如果您没有看到响应,这可能表明电子邮件服务器未运行或无法在指定的主机和端口上访问。

  1. 检查是否有任何网络或防火墙问题可能会阻止与电子邮件服务器的连接: 您可以通过在终端中使用“ping”命令来检查是否有任何网络或防火墙问题可能会阻止与电子邮件服务器的连接。这是一个例子:
ping smtp.gmail.com

如果 ping 成功,您应该看到类似这样的响应:

PING smtp.gmail.com (74.125.133.109) 56(84) bytes of data.
64 bytes from smtp.gmail.com (74.125.133.109): icmp_seq=1 ttl=108 time=62.1 ms
If the ping is unsuccessful, this could indicate that there is a network or firewall issue blocking the connection to the email server.

验证您的登录凭据(用户名和密码)是否正确: 您可以通过网络浏览器登录您的电子邮件帐户来验证您的登录凭据(用户名和密码)是否正确。如果您能够成功登录,则表明您的登录凭据是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.