Nodemailer: connection refused error while sending mails through gmail SMTP

Created on 11 Jun 2018  路  17Comments  路  Source: nodemailer/nodemailer

My code in server.js as given below :

app.post('/contact/send', function (req, res) {
const transporter = nodemailer.createTransport({
service: 'smtp.gmail.com',
port: 465,
secure: false,
auth: {
user: '[email protected]',
pass: 'xyz'
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'website error sending ',
text: 'it is working!!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
res.redirect('/');
} else {
console.log('Email sent: ' + info.response);
}
});
})

when i hit the submit button i got the following error in console:

{ Error: connect ECONNREFUSED 127.0.0.1:465 at Object._errnoException (util.js:1022:11) at _exceptionWithHostPort (util.js:1044:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14) code: 'ECONNECTION', errno: 'ECONNREFUSED', syscall: 'connect', address: '127.0.0.1', port: 465, command: 'CONN' }

I have done a lot of searching for it but unable to find the same!

Anykind of help is appreciated !

Thanks

Most helpful comment

This worked for me:

const nodemailer = require('nodemailer');

const transporter = createTransport({
                           service: "gmail.com",
                           auth: {
                          user: "<your mail>",
                          pass: "<your password>"
    }
});

const mailOption = {
            from: `[email protected]`,
            to: 'receiver's mail',
            subject: "Subject of mail",
            html: "<h1>Hello there</h1>"
        };
        transporter.sendMail(mailOption, (error, info) => {
                if (error) {
                    return console.log(error);
                } else{
                    console.log('Email has been sent');
                    res.send(info);
                }
            });
    });

All 17 comments

Use:

host: 'smtp.gmail.com',
port: 465,
secure: true,

now my code look like it:

app.post('/contact/send', function (req, res) {
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'xyz'
}
});
const mailOptions = {
from: '[email protected]',
to: '[email protected]',
subject: 'Sending Email using Node.js',
text: 'That was easy!'
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
res.redirect('/');
} else {
console.log('Email sent: ' + info.response);
}
});
})

and getting the error :

{ Error: connect ETIMEDOUT 74.125.24.108:465 at Object._errnoException (util.js:1022:11) at _exceptionWithHostPort (util.js:1044:20) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1198:14) code: 'ECONNECTION', errno: 'ETIMEDOUT', syscall: 'connect', address: '74.125.24.108', port: 465, command: 'CONN' }

@andris9 now i am getting timeout err message

@AshishkrGoyal can you telnet to 465 port like this:
openssl s_client -starttls smtp -crlf -connect smpt.gmail.com:465

Resolvio su problema???? Estoy igual...

Not resolved yet.
getting below error:

{ Error: queryA EREFUSED smtp.gmail.com at QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19) errno: 'EREFUSED', code: 'EDNS', syscall: 'queryA', hostname: 'smtp.gmail.com', command: 'CONN' }

I am getting this error as well.
{ Error: connect ECONNREFUSED 74.125.69.108:465
聽聽聽 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1159:14)
聽 errno: 'ECONNREFUSED',
聽 code: 'ESOCKET',
聽 syscall: 'connect',
聽 address: '74.125.69.108',
聽 port: 465,
聽 command: 'CONN' }

any ideas?

My code is:

const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: '[email protected]',
        pass: 'my_gmail_password'
    }
});

This error shows up while I'm on a remote server. This code works on localhost just fine.

Update: When I try the following code in the first answer, I still get an error:
Code:
const transporter = nodemailer.createTransport({
service: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: '[email protected]',
pass: 'my_gmail.Password'
}
});

and get the error:

{ Error: unable to verify the first certificate
聽聽聽 at TLSSocket.onConnectSecure (_tls_wrap.js:1048:34)
聽聽聽 at TLSSocket.emit (events.js:182:13)
聽聽聽 at TLSSocket._finishInit (_tls_wrap.js:628:8) code: 'ESOCKET', command: 'CONN' }

This happens due to installed firewall too. port 465 is generally blocked. check your firewall setting and try again!
Hope it helps!

I am still getting this error out in AWS. Works locally. Is that a firewall issue?

My host blocked all messages that were not sent from their host email servers.

So this error just started. It was running fine for over a month with no issues. Did something change?

same problem here
resolved it by using IP address as host, see https://nodemailer.com/smtp/#general-options

Working locally but not in the server.
should be the SMTP-ses setting? or the remote-server-firewall settings?

Facing same issue, working locally but not online

The same issue when deployed to firebase functions.

Error: connect ECONNREFUSED 127.0.0.1:465
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)

const transporter = createTransport({
    service: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: "OAuth2",
        user: config().gmail.email,
        clientId: config().gmail.id,
        clientSecret: config().gmail.secret,
        accessToken: config().gmail.accesstoken,
        refreshToken: config().gmail.refreshtoken,
    }
});

export const sendVerificationEmail = functionAuth.user().onCreate(async (user: functionAuth.UserRecord) => {
    const verificationLink: string = await adminAuth().generateEmailVerificationLink(user.email as string);

    return await transporter.sendMail({
        from: '"Mail Name" <[email protected]>',
        to: user.email,
        subject: 'Email verification',
        text: `${ verificationLink }`,
        html: `${ verificationLink }`
    });
});

I might have found my issue. A misconfiguration on my part. Instead of service, I need to use host.

This worked for me:

const nodemailer = require('nodemailer');

const transporter = createTransport({
                           service: "gmail.com",
                           auth: {
                          user: "<your mail>",
                          pass: "<your password>"
    }
});

const mailOption = {
            from: `[email protected]`,
            to: 'receiver's mail',
            subject: "Subject of mail",
            html: "<h1>Hello there</h1>"
        };
        transporter.sendMail(mailOption, (error, info) => {
                if (error) {
                    return console.log(error);
                } else{
                    console.log('Email has been sent');
                    res.send(info);
                }
            });
    });

Was this page helpful?
0 / 5 - 0 ratings