nodemailer connect ECONNREFUSED

x6yk4ghg  于 2023-04-20  发布在  Node.js
关注(0)|答案(1)|浏览(131)

我正在为我的投资组合工作,我使用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: 'xxxx@mail.com',
        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'
}

上面的代码是错误
我试图改变端口的安全和仍然不工作,我该怎么办?谢谢

oipij1gg

oipij1gg1#

我在Nodemailer documentation中没有看到service选项。
变更

const transporter = nodemailer.createTransport({
        service: 'smpt.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'xxxxxxxx',
            pass: 'xxxxxxxx'
            
        }

const transporter = nodemailer.createTransport({
        host: 'smpt.gmail.com',
        port: 465,
        secure: true,
        auth: {
            user: 'xxxxxxxx',
            pass: 'xxxxxxxx' 
        }

相关问题