使用Nodejs Openshift应用程序中的nodemailer的ETIMEDOUT连接错误 [英] ETIMEDOUT connect error using nodemailer in Nodejs Openshift application

查看:232
本文介绍了使用Nodejs Openshift应用程序中的nodemailer的ETIMEDOUT连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Openshift的node.js单齿轮不可扩展应用程序中使用nodemailer模块时,我遇到了一些问题.正如文档所建议的那样,我初始化了传输器对象并使用了sendMail函数.我的代码的简化版本是

I'm facing some problems using the nodemailer module in my node.js single gear non-scalable application in Openshift. As the documentation suggested, I initialized the transporter object and used the sendMail function. A simplified version of my code is

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'my.mail@gmail.com',
        pass: 'mypassword'
    }
});

var mail = {
    from: 'my.mail@gmail.com',
    to: 'test@mydomain.com',
    subject: 'Test mail',
    html: 'Test mail'
};

transporter.sendMail(mail, function(error, info) {
    if(error){
        console.log(error);
    }else{
        console.log(info);
    }
});

当我在本地计算机上运行该代码时,该代码正确运行,但是当我尝试在服务器上执行该代码时,出现了ETIMEDOUT错误,好像应用程序无法连接到smtp服务器.

This code works correctly when I run it on my local machine, but when I try to execute it on the server I got an ETIMEDOUT error, as if the application couln't connect to the smtp server.

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

我尝试增加超时连接参数,但是得到的结果相同.

I've tried to increase the timeout connection parameter, but I got the same results.

var transporter = nodemailer.createTransport({
    service: 'Gmail',
    auth: {
        user: 'my.mail@gmail.com',
        pass: 'mypassword'
    },
    connectionTimeout: 5 * 60 * 1000, // 5 min
});

我缺少任何防火墙或默认的Openshift设置或环境变量吗?

Is there any firewall or default Openshift settings or enviroment variable I'm missing?

推荐答案

我还需要运行一些测试,但是我想我已经找出了问题所在.通过用户名和密码进行身份验证是Gmail的不安全身份验证方法.如Nodemailer文档中所述

I've to run a few more tests, but I think I figured out what the problem was. The authentication via username and password is a not-secure authentication method for Gmail. As written in the Nodemailer documentation

即使Gmail是开始发送电子邮件的最快方法,但绝不是首选的解决方案,除非您使用OAuth2身份验证. Gmail希望该用户是实际用户,而不是机械手,因此,每次登录尝试时,它都会运行大量试探法,并阻止任何看起来可疑的内容,以保护用户免受帐户劫持尝试的侵害.例如,如果您的服务器位于另一个地理位置,则可能会遇到麻烦–一切都在您的开发机上正常运行,但消息在生产中被阻止.

Even though Gmail is the fastest way to get started with sending emails, it is by no means a preferable solution unless you are using OAuth2 authentication. Gmail expects the user to be an actual user not a robot so it runs a lot of heuristics for every login attempt and blocks anything that looks suspicious to defend the user from account hijacking attempts. For example you might run into trouble if your server is in another geographical location – everything works in your dev machine but messages are blocked in production.

我使用XOAuth2升级了我的授权方法,这在

I upgraded my authorization method using XOAuth2, as well explained in this guide and now the mails are correctly sent.

这篇关于使用Nodejs Openshift应用程序中的nodemailer的ETIMEDOUT连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆