15

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: "gmail.user@gmail.com",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
    to: "bar@blurdybloop.com, baz@blurdybloop.com", // 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.

Morten Siebuhr
  • 6,068
  • 4
  • 31
  • 43
user1756980
  • 3,807
  • 4
  • 16
  • 13
  • Have you sent any emails from that account, already? If you have, be advised GMail SMTP is subject to limits - you can only send a couple hundred emails from an account on a given day. – S M Feb 01 '13 at 20:50
  • No, I created the account just so that I could test whether this worked. I just sent my first ever email from it, to myself, and I received it. I then tried my program again, still saying connect refused. – user1756980 Feb 01 '13 at 20:53
  • I would like to comment that I've tried to sudo it. Have you used nodemailer? @sebastian – user1756980 Feb 01 '13 at 21:00
  • I haven't used it for a long time, and when I did, all my problems were caused because I reached Gmail's sent email limit. Have you tried not using the Gmail shortcut, and typing out the full SMTP address and everything (as described here: https://github.com/andris9/Nodemailer/blob/master/README.md#setting-up-smtp) ? – S M Feb 01 '13 at 21:10
  • Yes, I tried that, and I just tried it again, it yields the exact same results. This is probably a dumb question, but nodemailer says that it is "Windows friendly", that doesn't mean "Windows only", does it? I'm running Ubuntu. @sebastian – user1756980 Feb 01 '13 at 21:19
  • Nah that won't be it. Sorry, that's all that springs to my mind, without more error information. – S M Feb 01 '13 at 21:59
  • Perhaps is this issue: https://groups.google.com/d/msg/nodejs/ObVfzUoH-TQ/gSm63mPTmdQJ For what is worth, I've used https://github.com/eleith/emailjs with gmail with no problems. – Hector Correa Feb 01 '13 at 22:35
  • 1
    ECONNREFUSED inidcates that this is somekind 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` – Andris Feb 02 '13 at 15:38
  • Thank you, it was a firewall issue. I scp'ed the project to another machine and it worked. – user1756980 Feb 04 '13 at 15:50

8 Answers8

8

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

user1756980
  • 3,807
  • 4
  • 16
  • 13
3

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.

https://support.google.com/mail/answer/1173270?hl=en

3

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

Dũng IT
  • 2,751
  • 30
  • 29
  • "Enable less secure apps" - This option is now deprecated, now, you need to setup oAuth from the [console](https://console.cloud.google.com) – danted4 May 18 '23 at 20:35
2

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
  • I'm having the same issue. I was able to use gmail from the same machine, then why firewall is blocking nodemailer ? Also, I've not set two step verification in gmail so I believe the application specific password is not required. – Balwant Kumar Singh Jan 07 '15 at 06:22
1

if you use new nodemailer version

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

more see https://github.com/andris9/nodemailer-wellknown

Morten Siebuhr
  • 6,068
  • 4
  • 31
  • 43
boneyao
  • 121
  • 1
  • 1
1

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

enter image description here

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

4) Now paste the IP on your exception enter image description here

Hope it helps.

Marcelo
  • 1,486
  • 13
  • 16
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
                }
            })
Alex Hunter
  • 212
  • 9
  • 30
0

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
Ericgit
  • 6,089
  • 2
  • 42
  • 53