Search code examples
node.jssmtpportnodemailermailtrap

connect ECONNREFUSED 127.0.0.1:587 (nodemailer + mailtrap)


I've been trying to get the basic combination of nodemailer, using a mailtrap.io account to work, and striking out.

Here is my app.js:

const nodemailer = require('nodemailer');

let transport = nodemailer.createTransport({
    host: "smtp.mailtrap.io",
    port: 2525,
    // secure: true,
    auth: {
        user: "myusername",
        pass: "mypassword"
    },
    debug: true,
    logger: true
});

let scrapeEmailMessage = {
    //from: '[email protected]',
    to: '[email protected]',
    subject: 'Hello World',
    text: 'hello world'
};

let mailTransporter = nodemailer.createTransport(transport);

mailTransporter.sendMail(scrapeEmailMessage, function(err, data) {
    if(err) {
        console.log(err);
    } else {
        console.log('Email sent successfully');
    }
});

And here is the error output I'm getting:

[2020-11-10 14:32:20] DEBUG Creating transport: nodemailer (6.4.15; +https://nodemailer.com/; SMTP/6.4.15[client:6.4.15])
[2020-11-10 14:32:20] DEBUG Creating transport: nodemailer (6.4.15; +https://nodemailer.com/; SMTP/6.4.15[client:6.4.15])
[2020-11-10 14:32:20] DEBUG Sending mail using SMTP/6.4.15[client:6.4.15]
[2020-11-10 14:32:20] DEBUG [YlvPyvxQxE] Resolved localhost as 127.0.0.1 [cache miss]
[2020-11-10 14:32:22] ERROR [YlvPyvxQxE] connect ECONNREFUSED 127.0.0.1:587
[2020-11-10 14:32:22] DEBUG [YlvPyvxQxE] Closing connection to the server using "destroy"
[2020-11-10 14:32:22] ERROR Send Error: connect ECONNREFUSED 127.0.0.1:587
Error: connect ECONNREFUSED 127.0.0.1:587
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1129:14) {
  errno: 'ECONNREFUSED',
  code: 'ESOCKET',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 587,
  command: 'CONN'
}

I've tried a number of things to fix it, and continually get this same error message:

  • Turning on the "less secure apps" options in Gmail and using the above code to try to connect to smtp.gmail.com instead.
  • Manually editing the port numbers in the code above.
  • Toggling secure mode on and off in the code above.
  • Adding Windows Defender Firewall exceptions on ports 587, 465, as well as a couple others just for fun to see if they would work.
  • Disabling Windows Defender Firewall completely.
  • Running the code on other computers on my home network.
  • Checking "netstat -an" to see if ports 587 or 465 are listed. They are not, but is this port open all the time or only opened when needed? Could this be the issue?

Anyone have any ideas on what might be going on here? I'm just a normal guy using his home internet connection to write a small program to send himself an email alert once a day. Could my ISP be blocking this or something?

EDIT- Adding a couple new things I've tried that haven't worked:

  • Transport option "ignoreTLS: true/false" (tried both)
  • Transport option "requireTLS: true/false" (tried both)

Solution

  • You are creating two transporter.

    Instead of this : let mailTransporter = nodemailer.createTransport(transport); and mailTransporter.sendMail()

    Do this : transport.sendMail()