24

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 ✔ <foo@blurdybloop.com>", // 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 foo@blurdybloop.com, to vinz243@gmail.com
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
Vinz243
  • 9,654
  • 10
  • 42
  • 86
  • Make sure you have allowed the connection through ports which connects to gmail smtp, first test telnet smtp.gmail.com 587 – Risto Novik Apr 15 '14 at 11:00
  • i am on windows so the `telnet` command doesnt work. – Vinz243 Apr 15 '14 at 15:41
  • Aw the Windows users :D, well you can enable it http://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-client.aspx. Or use some other client like Putty. – Risto Novik Apr 16 '14 at 08:03
  • nobody has an anwser? I don't want to waste 100 reputation... – Vinz243 Apr 19 '14 at 17:17

7 Answers7

17

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: "test.nodemailer@gmail.com",
            pass: "Nodemailer123"
        }
    });

console.log('SMTP Configured');

// Message object
var message = {

    // sender info
    from: 'Sender Name <sender@example.com>',

    // Comma separated list of recipients
    to: '"Receiver Name" <nodemailer@disposebox.com>',

    // 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
});
Saran
  • 3,845
  • 3
  • 37
  • 59
Risto Novik
  • 8,199
  • 9
  • 50
  • 66
  • thank you it worked... But I don't want to login. And If I change it from SMTP to direct it tells me that it was successfuly sended but I do not receive anything... – Vinz243 Apr 20 '14 at 10:51
  • 1
    What you mean by you don't want to login ? The gmail SMTP server requires it, if you want to send email without authentication you could setup your own SMTP server. – Risto Novik Apr 20 '14 at 11:24
  • 1
    I wanted to use direct transport so i could set a fake address such as `no-reply@vinz243.com` – Vinz243 Apr 20 '14 at 11:34
  • 1
    above transport object created support in `v0.7.1`. In latest `v2.7.0` to create transport object use `var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');` – Rohan Khude Jan 20 '17 at 14:45
12

Looks like the the current nodemailer does not have the option of sending mail without smtp any more. I would recommend take a look at the sendmail library which does NOT need any smtp/auth to send email. It gives you similar experience to using sendmail in linux.

const sendmail = require('sendmail')();

sendmail({
  from: 'test@finra.org',
  to: 'YourName@gmail.com',
  subject: 'Hello World',
  html: 'Hooray NodeJS!!!'
}, function (err, reply) {
  console.log(err && err.stack)
  console.dir(reply)
})

You can also turn on the silent to make it less verbose: const sendmail = require('sendmail')({silent: true});. One thing to notice is the sender cannot be those domains that has DMARC policy like xxx@capitalone.com.

LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
  • how do I force sendmail to send directly to the host in the address? e.g. I was using direct because otherwise I get the error "queryMx ENODATA" but now with sendmail I am seeing this error again. – Michael Nov 21 '17 at 17:31
  • @Michael not sure about that, you might want to do a `dig` or `nslookup` for your mx server first. – LeOn - Han Li Nov 21 '17 at 17:50
  • ah ok I see the problem. but argh! the "err" argument isn't something that I can easily parse to determine if the error is 4xx - I have to retry these myself, don't I? – Michael Nov 21 '17 at 19:13
  • @Michael Perhaps there is a errro code on the `err` object. I do not quite remember the detail. debug or do a `console.log` on err will give you the full picture of that object. :) – LeOn - Han Li Nov 21 '17 at 20:26
  • I struggled for 2 days with nodemailer and emailjs before finding this! Note that gmail will put these mails in your spam folder because they can't verify the sender, but at least it works with all the increasing security checks. – Nancy May 26 '18 at 19:31
  • sendmail package is using nodemailer mailcomposer for attachment which is deprecated. – amarinediary Jul 28 '21 at 13:05
11

use nodemailer 0.7.1.
if you previously installed nodemailer then remove it by

npm remove nodemailer

now install it by

npm install nodemailer@0.7.1

the following code send mail without login, but it will work only in production environment, it won't work locally

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

mail({
    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
});
barley
  • 176
  • 1
  • 10
  • If you are really not interesting in saving the email credentials on code, then nodemailer 0.7.1 with the above code snippet works well. Thanks! – Dipin Krishnan Jun 13 '17 at 08:01
  • can this be done with nodemailer 2.7.2? If not, is it possible for both versions to co-exist for other modules in the same project that need it? – Michael Aug 08 '17 at 18:15
  • Reverting to v0.7.1 works, but the email message is not encrypted. How do I encrypt the message? – Neutrino Jan 04 '18 at 20:36
5

probably it is windows firewall or antivirus preventing outgoing access. try to enable nodemailer debug messages.

Enabling debug

var nodemailer = require("nodemailer"),
  transport = nodemailer.createTransport('direct', {
    debug: true, //this!!!
  });

transport.sendMail({
    from: "Fred Foo ✔ <foo@blurdybloop.com>", // sender address
    to: "******@gmail.com", // list of receivers
    subject: "Hello ✔", // Subject line
    text: "Hello world ✔", // plaintext body
    html: "<b>Hello world ✔</b>" // html body
}, console.error);
vodolaz095
  • 6,680
  • 4
  • 27
  • 42
2

This appears to be possible in nodemailer. Not sure if this wasn't around when the other answers were provided. As documented here: https://nodemailer.com/transports/sendmail/ you can you use the built in sendmail if available to do your sending. I've tested this for my own use and works fine. There are obviously advantages of SMTP but to say it's not possible is not true.

Brian F Leighty
  • 922
  • 11
  • 22
0

You need to have a SMTP server, an email server which forwards the emails on your behalf. So either set up your own SMTP server like Haraka or provide credentials to Nodemailer to connect to one e.g. Gmail,MSN,Yahoo. Even I have started learning NodeJs and was trying to include an emailing feature in my project and this was the same problem I faced.

dv3
  • 151
  • 2
  • 12
  • 1
    I wanted to use Direct for this reason. BTW there is no need to have a proper email to send mails. That's why you can send emails to someone with his email even though you don't know its credentials – Vinz243 Feb 09 '15 at 21:28
  • But then wouldn't the receiving service (SMTP server) simply reject your mail or spam it. It expects to get emails from a authentic source. – dv3 Feb 10 '15 at 07:47
  • Emails with no reply doesn't exists – Vinz243 Feb 10 '15 at 15:48
0

when you on your setting , it allows to send mails. https://www.google.com/settings/security/lesssecureapps this one worked for me

pitu
  • 822
  • 3
  • 11
  • 35