node.js Nodemailer在没有smtp传输的情况下发送电子邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23016208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:08:59  来源:igfitidea点击:

Nodemailer send email without smtp transport

node.jsemailnodemailer

提问by Vinz243

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

我正在尝试通过没有 SMTP 传输的 nodemailer 发送电子邮件。所以我这样做了:

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.

我在 Windows 7 32 上。

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

编辑这似乎是一个 Windows 相关的错误,因为它在 linux 上工作

EDIT #2

编辑#2

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

在 git shell 上,如果我输入telnet smtp.gmail 587它会在此处被阻止:

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

回答by Risto Novik

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.

从您的示例输出来看,它似乎连接到错误的端口 25,打开的 gmail smtp 端口为 SSL 465 和其他 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.

Nodemailer 根据电子邮件域检测正确的配置,在您的示例中,您没有设置传输器对象,因此它使用配置的默认端口 25。要更改端口,请在选项中指定类型。

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

这是应该与 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
});

回答by rahul

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

使用 nodemailer 0.7.1。
如果您之前安装了 nodemailer,则通过以下方式将其删除

npm remove nodemailer

now install it by

现在安装它

npm install [email protected]

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 ? <[email protected]>", // sender address
    to: "[email protected], [email protected]", // list of receivers
    subject: "Hello ?", // Subject line
    text: "Hello world ?", // plaintext body
    html: "<b>Hello world ?</b>" // html body
});

回答by LeOn - Han Li

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

看起来目前nodemailer没有smtp发送邮件的选项了。我会建议采取一看的sendmail这确实库不是需要任何SMTP / AUTH发送电子邮件。它为您提供与在 linux 中使用 sendmail 类似的体验。

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

sendmail({
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Hello World',
  html: 'Hooray NodeJS!!!'
}, function (err, reply) {
  console.log(err && err.stack)
  console.dir(reply)
})

You can also turn on the silentto make it less verbose: const sendmail = require('sendmail')({silent: true});. One thing to notice is the sender cannot be those domains that has DMARCpolicylike [email protected].

您还可以打开silent以使其不那么冗长:const sendmail = require('sendmail')({silent: true});。有一点要注意的是发送者不能是具有这些领域DMARC的政策一样[email protected]

回答by vodolaz095

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

可能是 Windows 防火墙或防病毒软件阻止了传出访问。尝试启用 nodemailer 调试消息。

Enabling debug

启用调试

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

transport.sendMail({
    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
}, console.error);

回答by dv3

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.

您需要有一个 SMTP 服务器,一个代表您转发电子邮件的电子邮件服务器。因此,要么设置您自己的 SMTP 服务器(如 Haraka),要么向 Nodemailer 提供凭据以连接到一个(例如 Gmail、MSN、Yahoo)。即使我已经开始学习 NodeJs 并试图在我的项目中包含一个电子邮件功能,这也是我面临的同样问题。

回答by pitu

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

当您进行设置时,它允许发送邮件。 https://www.google.com/settings/security/lesssecureapps这个对我有用

回答by Brian F Leighty

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.

这在 nodemailer 中似乎是可能的。不确定在提供其他答案时是否存在这种情况。如此处所述:https: //nodemailer.com/transsports/sendmail/您可以使用内置的 sendmail(如果可用)进行发送。我已经测试了这个供我自己使用并且工作正常。SMTP 有明显的优势,但说不可能是不正确的。