18

I was using my gmail account to send an smtp alert messages to my users email . Example, registration or account blocked etc. I am using a nodemailer and was email were sent successfully without a single failure. Below is my code .

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
});

Just yesterday , signup for google for business account to my @mydomain account, then replace gmail with my new google for business email that is

var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "user@mydomain.com",
        pass: "userpass"
    }
});

The problem is, it does not send the email with new account. It rather returned the titled error on my console. I tried change the security of my new account to allow less secure apps from the google console all to no avail. Please what does this error implies? Also considering the user name and pwd for my email are used, is that the best option ? Please how is best achieved ? Any help would be appreciated.

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
  • Node mailer is obviously trying to connect to localhost(127.0.0.1) which is unlikely the Google's mail server. That would mean there's something wrong in transport setting. Try this `nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');` – Molda Jun 25 '16 at 06:11
  • @Molda thank you for your time.Your solution works with gmail.com account. It fails when i replace the email with `@mydomain.com`. I get the error above. I am confused , i was expecting it to work considering both are google account. – Nuru Salihu Jun 25 '16 at 06:52
  • Aren't you by any chance changing smtp.gmail.com to smtp.mydomain.com? – Molda Jun 25 '16 at 07:02
  • Possible duplicate of [Nodemailer: ECONNREFUSED](http://stackoverflow.com/questions/14654736/nodemailer-econnrefused) – Alongkorn Jun 25 '16 at 07:29
  • @Molda nope i was'nt . i Used `nodemailer.createTransport('smtps://user%myDomain.com:pass@smtp.gmail.com');`. – Nuru Salihu Jun 25 '16 at 07:36

7 Answers7

24

Thank you for your time guys. Below is what works for me .

nodemailer.createTransport('smtps://user%myDomain.com:pass@smtp.gmail.com');

changed to

var smtpConfig = {
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
        user: 'user@myDomain.com',
        pass: 'pass@pass'
    }
};
var transporter = nodemailer.createTransport(smtpConfig);

I Found the example above on the doc https://github.com/nodemailer/nodemailer . I am suspecting this may happened to you if your password contained @ symbol considering u and the smtps link. Its therefore advisable to split it into an object without the @ symbol interfering with your smtps url. This is my guess thought. Nonetheless the above solution works for me. Plus don't forget to allow less secure apps from your google console.

Nuru Salihu
  • 4,756
  • 17
  • 65
  • 116
4

Adding a config for Bluehost for anyone using it.

let transporter = nodemailer.createTransport({
    host: 'box1109.bluehost.com',
    port: 465,
    secure: true,
    auth: {
        user: 'someusername@somewebsite.com',
        pass: 'yourpassword'
    }
});

You can verify the options using the verify() method.

transporter.verify((err, success) => {
    if (err) console.error(err);
    console.log('Your config is correct');
});

If you are experiencing this error, make sure that the host property is set properly.

console.log(transporter.options.host);

I spent 20 minutes on this error to find out that the host property was undefined (I was using an environment variable to port it into the config).

Zach Gollwitzer
  • 2,114
  • 2
  • 16
  • 26
4

I was using secure: false and I faced the same issue on production and below was the solution that worked for me:

const transporter = nodemailer.createTransport({
  host: <hostname>,
  port: 25,
  secure: false,
  logger: true,
  debug: true,
  ignoreTLS: true // add this 
});

Hope this will help someone.

itsHarshad
  • 1,519
  • 14
  • 16
1

I have faced the same error, this worked for me

try {
            var transporter = nodemailer.createTransport({
                service: 'gmail',
                port:465,
                secure: true, // true for 465, false for other ports
                logger: true,
                debug: true,
                secureConnection: false,
                auth: {
                    user: 'test@gmail.com', // generated ethereal user
                    pass: '*****', // generated ethereal password
                },
                tls:{
                    rejectUnAuthorized:true
                }
            })

         var info = await transporter.sendMail({
                from: 'myuser@outlook.com', // sender address
                to: 'anotheruser@hotmail.co.uk', // list of receivers
                subject: 'Test Email', // Subject line
                text: 'Hello world ?' // plain text body
                html: output, // html body
        });
        }
       catch(error) {
                console.log(error)
       }
Rakesh
  • 11
  • 2
0

I figured out this problem thanks to this way.

const nodemailer = require("nodemailer");

const sendEmail = async(mailOptions) => {

let transporter = nodemailer.createTransport({
            service: 'gmail',
            port:465,
            secure: true,
            secureConnection: false,
            auth: {
                user: 'your mail address',
                pass: 'your mail address password', 
            },
            tls:{
                rejectUnAuthorized:true
            }
});

let info = await transporter.sendMail(mailOptions);
console.log(`Message Sent: ${info.messageId}`);
}
diyarovski
  • 31
  • 1
  • 9
0

in my case the proxy was the error so see the proxy farewell setting

0

If nothing else works, it might be because of your server provider didn't enable port 465 for you. I'm using A2 Hosting, they are not enable port 465 by default, after talking with the technical support then only I can send out the email by using google workspace email.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Fu Nian Wong
  • 1,047
  • 1
  • 6
  • 14