8

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

Hitu
  • 276
  • 1
  • 2
  • 14

3 Answers3

11

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'
    }
});
jgillich
  • 71,459
  • 6
  • 57
  • 85
4

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. 'youremail@gmail.com'
   to: toEmailAddress, // <= recepient email address. ex. 'friendemail@gmail.com'
   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.

Rami Mohamed
  • 2,505
  • 3
  • 25
  • 33
  • Do you have an example of a free on-premises hosted "smtp server" that can be installed on a LAN ? I am confused, it appears that nodemailer can be used as an smtp server, but all examples require an EXTERNAL product as an smtp-server ?! – joedotnot May 31 '21 at 09:14
2
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"
    }
}));
KiwenLau
  • 2,502
  • 1
  • 19
  • 23