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: 'myemail@gmail.com',
    to: 'myemail@gmail.com',
    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:

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:


Solution

  • You are creating two transporter.

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

    Do this : transport.sendMail()