Hitu
Hitu

Reputation: 286

SMTP using nodemailer in nodejs without GMail

I am using node mailer with GMail

smtpTransport = nodemailer.createTransport("SMTP", {
    service: "Gmail",
    auth: {
        user: "myemail ",
        pass: "mypass"
    }
});

which is working fine, but I want to use my own email server instead of GMail.

This:

smtpTransport = nodemailer.createTransport("SMTP", {
    service: "mymailservr link url",
    port : 25
    auth: {
        user: "myemail ",
        pass: "mypass"
    }
});

It throws this error:

connect ECONNREFUSED

Upvotes: 9

Views: 36949

Answers (3)

Rami Mohamed
Rami Mohamed

Reputation: 2755

make sure you have the latest NodeMailer version installed

as of today (15-jan-2020) it is v6.4.2

npm install nodemailer --save

This should work:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
   host: 'smtp.server.com', // <= your smtp server here
   port: 2525, // <= connection port
   // secure: true, // use SSL or not
   auth: {
      user: 'userId', // <= smtp login user
      pass: 'E73oiuoiC34lkjlkjlkjlkjA6Bok7DAD' // <= smtp login pass
   }
});

let mailOptions = {
   from: fromEmailAddress, // <= should be verified and accepted by service provider. ex. '[email protected]'
   to: toEmailAddress, // <= recepient email address. ex. '[email protected]'
   subject: emailSubject, // <= email subject ex. 'Test email'
   //text: emailData.text, // <= for plain text emails. ex. 'Hello world'
   html: htmlTemplate // <= for html templated emails
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
   if (error) {
      return console.log(error.message);
   }
   console.log('Message sent: %s', info.messageId);
});

Hope this helps, Thank you.

Upvotes: 6

KiwenLau
KiwenLau

Reputation: 2571

var nodemailer = require('nodemailer');
var smtpTransport = require("nodemailer-smtp-transport");

var transporter = nodemailer.createTransport(smtpTransport({
    host : "example.com",
    port: 25,
    auth : {
        user : "username",
        pass : "password"
    }
}));

Upvotes: 2

jgillich
jgillich

Reputation: 76169

The service option is only for well-known services. To specify your own host, set host.

var smtpTransport = nodemailer.createTransport('SMTP', {
    host: 'yourserver.com',
    port: 25,
    auth: {
        user: 'username',
        pass: 'password'
    }
});

Upvotes: 13

Related Questions