Nodemailer: Econnrefused

I don't know what I'm missing, I use the Nodemailer example:

var nodemailer = require("nodemailer");

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    from: "Fred Foo ✔ <>", // sender address
    to: ", ", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}

// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    // if you don't want to use this transport object anymore, uncomment following line
    //smtpTransport.close(); // shut down the connection pool, no more messages
});

I just changed the user and pass in auth to my gmail account info (also tried with their values), and I changed the "to" email address to my email address. I get:

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }

What am I missing? I don't see anything in the documentation that says I need to do anything more than this, so why won't it work? Thank you in advance.

9

8 Answers

It was a firewall issue. Turns out there was nothing wrong with the code, I just didn't understand what the error message implied.

2

Following step:

  1. Login gmail account.

  2. Enable pop3 in settings tabs

  3. Enable less secure apps at: Enable less secure apps for gmail account

  4. Display Unlock Captcha at: Display unlock Captcha

  5. Defined email option and sending

    var mailOptions = {
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // use SSL
        auth: {
            user: '',
            pass: 'password'
        }
    }    
    
    mailer = nodemailer.createTransport(mailOptions);
    
    mailer.sendMail({
    ....
    }, function(err, response) {
            if (err) {
                 console.log(err);
            }
            console.log(response);
    });
    

Hope this block code help you.

I was also using a gmail account to send the email, you might need an Application-specific password from google to allow nodemailer to work properly.

1

ECONNREFUSED inidcates that this is some kind of connection or firewall issue.

Can you connect to smtp.gmail.com port 465 with any other application from the same machine, for example with openssl?

openssl s_client -connect smtp.gmail.com:465
1

The actual problem fix is that you need to place the SERVICE. Better if you use ENVIRONMENT VARIABLES

            var transporter = nodemailer.createTransport({
                service: process.env.EMAIL_SERVICE,
                auth: {
                    user:process.env.EMAIL_USER,
                    pass:process.env.EMAIL_PASSWORD
                }
            })

if you use new nodemailer version

nodemailer.createTransport({
    host,
    port,
    secure:,
    auth:{
        user,
        pass
    }
})

more see

In my case I just had to disable my anti virus, Avast.

To white list on avast do this:

These steps may vary according to your Avast version, including the IP used here

1) Open Avast, go to: Menu -> Settings. (It is located on the top right corner)

2) Go to General Tab -> Exceptions -> Add Exception

3) Get the IP of your gmail, it is going to be with the error, I think.

4) Now paste the IP on your exception

Hope it helps.

firewall problem

to block the firewall in order to send email, you need to allow port or disable all port.

allowing specific port:

$ sudo ufw allow 587

to disable firewall

$ sudo ufw disable

$ sudo ufw reset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest