Nodemailer send email without smtp transport

余生颓废 提交于 2019-12-03 06:38:51

问题


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

回答1:


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



回答2:


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



回答3:


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.




回答4:


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



回答5:


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.




回答6:


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




回答7:


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.



来源:https://stackoverflow.com/questions/23016208/nodemailer-send-email-without-smtp-transport

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!