Nodemailer send email without smtp transport

Vinz243 picture Vinz243 · Apr 11, 2014 · Viewed 44.6k times · Source

I am trying to send emails via nodemailer without SMTP transport. So i've done that:

var mail = require("nodemailer").mail;

mail({
    from: "Fred Foo ✔ <[email protected]>", // sender address
    to: "******@gmail.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
});

But when I run I get that :

> node sendmail.js
Queued message #1 from [email protected], to [email protected]
Retrieved message #1 from the queue, reolving gmail.com
gmail.com resolved to gmail-smtp-in.l.google.com for #1
Connecting to gmail-smtp-in.l.google.com:25 for message #1
Failed processing message #1
Message #1 requeued for 15 minutes
Closing connection to the server

Error: read ECONNRESET
    at errnoException (net.js:901:11)
    at TCP.onread (net.js:556:19)

I am on windows 7 32.

EDIT This seems to be a windows related bug for it worked on linux

EDIT #2

On the git shell, if I enter telnet smtp.gmail 587 it is blocked here:

220 mx.google.com ESMTP f7...y.24 -gsmtp

Answer

Risto Novik picture Risto Novik · Apr 19, 2014

From your example output it seems to connecting to wrong port 25, gmail smtp ports which are opened are 465 for SSL and the other 587 TLS.

Nodemailer detects the correct configuration based on the email domain, in your example you have not set the transporter object so it uses the default port 25 configured. To change the port specify in options the type.

Here's the small example that should work with gmail:

var nodemailer = require('nodemailer');

// Create a SMTP transport object
var transport = nodemailer.createTransport("SMTP", {
        service: 'Gmail',
        auth: {
            user: "[email protected]",
            pass: "Nodemailer123"
        }
    });

console.log('SMTP Configured');

// Message object
var message = {

    // sender info
    from: 'Sender Name <[email protected]>',

    // Comma separated list of recipients
    to: '"Receiver Name" <[email protected]>',

    // Subject of the message
    subject: 'Nodemailer is unicode friendly ✔', 

    // plaintext body
    text: 'Hello to myself!',

    // HTML body
    html:'<p><b>Hello</b> to myself <img src="cid:note@node"/></p>'+
         '<p>Here\'s a nyan cat for you as an embedded attachment:<br/></p>'
};

console.log('Sending Mail');
transport.sendMail(message, function(error){
  if(error){
      console.log('Error occured');
      console.log(error.message);
      return;
  }
  console.log('Message sent successfully!');

  // if you don't want to use this transport object anymore, uncomment following line
  //transport.close(); // close the connection pool
});