node.js Nodemailer:ECONNREFUSED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14654736/
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:05:00  来源:igfitidea点击:

Nodemailer: ECONNREFUSED

node.jsnodemailer

提问by user1756980

I don't know what I'm missing, I use the Nodemailer example:

我不知道我错过了什么,我使用 Nodemailer 示例:

var nodemailer = require("nodemailer");

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport("SMTP",{
    service: "Gmail",
    auth: {
        user: "[email protected]",
        pass: "userpass"
    }
});

// setup e-mail data with unicode symbols
var mailOptions = {
    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
}

// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

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

I just changed the user and pass in auth to my gmail account info (also tried with their values), and I changed the "to" email address to my email address. I get:

我刚刚更改了用户并将 auth 传递给我的 gmail 帐户信息(也尝试了他们的值),我将“to”电子邮件地址更改为我的电子邮件地址。我得到:

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect' }

What am I missing? I don't see anything in the documentation that says I need to do anything more than this, so why won't it work? Thank you in advance.

我错过了什么?我在文档中没有看到任何内容说我需要做更多的事情,为什么它不起作用?先感谢您。

采纳答案by user1756980

It was a firewall issue. Turns out there was nothing wrong with the code, I just didn't understand what the error message implied.

这是一个防火墙问题。结果证明代码没有任何问题,我只是不明白错误消息的含义。

回答by IT Vlogs

Following step:

以下步骤:

  1. Login gmail account.

  2. Enable pop3 in settings tabs

  3. Enable less secure apps at: Enable less secure apps for gmail account

  4. Display Unlock Captcha at: Display unlock Captcha

  5. Defined email option and sending

    var mailOptions = {
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // use SSL
        auth: {
            user: '[email protected]',
            pass: 'password'
        }
    }    
    
    mailer = nodemailer.createTransport(mailOptions);
    
    mailer.sendMail({
    ....
    }, function(err, response) {
            if (err) {
                 console.log(err);
            }
            console.log(response);
    });
    
  1. 登录gmail帐户。

  2. 在设置选项卡中启用 pop3

  3. 在以下位置启用不太安全的应用程序:为 Gmail 帐户启用不太安全的应用程序

  4. 显示解锁验证码:显示解锁验证码

  5. 定义的电子邮件选项和发送

    var mailOptions = {
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // use SSL
        auth: {
            user: '[email protected]',
            pass: 'password'
        }
    }    
    
    mailer = nodemailer.createTransport(mailOptions);
    
    mailer.sendMail({
    ....
    }, function(err, response) {
            if (err) {
                 console.log(err);
            }
            console.log(response);
    });
    

Hope this block code help you.

希望此块代码对您有所帮助。

回答by brendecimus

I was also using a gmail account to send the email, you might need an Application-specific password from google to allow nodemailer to work properly.

我还使用 gmail 帐户发送电子邮件,您可能需要来自谷歌的特定于应用程序的密码才能让 nodemailer 正常工作。

https://support.google.com/mail/answer/1173270?hl=en

https://support.google.com/mail/answer/1173270?hl=en

回答by brendecimus

ECONNREFUSED inidcates that this is some kind of connection or firewall issue.

ECONNREFUSED 表明这是某种连接或防火墙问题。

Can you connect to smtp.gmail.com port 465 with any other application from the same machine, for example with openssl?

您可以使用同一台机器上的任何其他应用程序(例如使用 openssl)连接到 smtp.gmail.com 端口 465 吗?

openssl s_client -connect smtp.gmail.com:465

回答by boneyao

if you use new nodemailer version

如果您使用新的 nodemailer 版本

nodemailer.createTransport({
    host,
    port,
    secure:,
    auth:{
        user,
        pass
    }
})

more see https://github.com/andris9/nodemailer-wellknown

更多见https://github.com/andris9/nodemailer-wellknown

回答by Alex

In my case I just had to disable my anti virus, Avast.

就我而言,我只需要禁用我的防病毒软件 Avast。

To white list on avast do this:

要在 avast 上加入白名单,请执行以下操作:

These steps may vary according to your Avast version, including the IP used here

这些步骤可能因您的 Avast 版本而异,包括此处使用的 IP

1) Open Avast, go to: Menu -> Settings. (It is located on the top right corner)

1) 打开 Avast,前往:菜单 -> 设置。(它位于右上角)

2) Go to General Tab -> Exceptions -> Add Exception

2) 转到常规选项卡 -> 例外 -> 添加例外

enter image description here

在此处输入图片说明

3) Get the IP of your gmail, it is going to be with the error, I think. enter image description here

3) 获取您的 gmail 的 IP,我认为会出现错误。 在此处输入图片说明

4) Now paste the IP on your exception enter image description here

4)现在将IP粘贴到您的例外上 在此处输入图片说明

Hope it helps.

希望能帮助到你。

回答by Alex Hunter

The actual problem fix is that you need to place the SERVICE. Better if you use ENVIRONMENT VARIABLES

实际的问题修复是您需要放置SERVICE。如果使用环境变量会更好

            var transporter = nodemailer.createTransport({
                service: process.env.EMAIL_SERVICE,
                auth: {
                    user:process.env.EMAIL_USER,
                    pass:process.env.EMAIL_PASSWORD
                }
            })