Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: "[email protected]",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    from: "Fred Foo ✔ <[email protected]>", // sender address
    to: "[email protected], [email protected]", // 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.

like image 600
user1756980 Avatar asked Feb 01 '13 20:02

user1756980


People also ask

Can I use Nodemailer in production?

Bookmark this question. Show activity on this post. Before changing some settings, e.g. host and port, it was working fine locally, but just won't work on production.

Does Nodemailer have a limit?

If you have a Gmail account, you could use that account with nodemailer directly. However, there are several downsides for doing so. First, there is a limit on outbound email: 500 per day, and each recipient counts as 1 outbound email.

What is Nodemailer SMTP transport?

SMTP is the main transport in Nodemailer for delivering messages. SMTP is also the protocol used between different email hosts, so its truly universal. Almost every email delivery provider supports SMTP based sending, even if they mainly push their API based sending.


2 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.

like image 97
user1756980 Avatar answered Sep 30 '22 17:09

user1756980


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: '[email protected]',
            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.

like image 24
IT Vlogs Avatar answered Sep 30 '22 16:09

IT Vlogs