为什么JavaMail连接超时时间过长

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

Why JavaMail connection timeout is too long

javajavamailconnection-timeout

提问by kuba44

In my application I connect to server to authenticate users. This is code:

在我的应用程序中,我连接到服务器以对用户进行身份验证。这是代码:

try {
        Properties prop = new Properties();
        prop.put("mail.smtp.starttls.enable","true");
        prop.put("mail.smtp.auth", "true");
        prop.put("mail.smtp.connectiontimeout", 1000);


        Session session = Session.getInstance(prop, null);
        Transport transport = session.getTransport("smtp");
        transport.connect("mion.elka.pw.edu.pl", 587, registerLog, registerPass);
        transport.close();
        return true;
    } catch (NoSuchProviderException ex) {
        Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch(AuthenticationFailedException ex) {
        Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    } catch (MessagingException ex) {
        Logger.getLogger(RegisterController.class.getName()).log(Level.SEVERE, null, ex);
        return false;
    }

I set connection timeout to 1000 ms = 1s but it's ignore. When i debug and set wrong username and password i catch

我将连接超时设置为 1000 ms = 1s,但它被忽略了。当我调试并设置错误的用户名和密码时,我发现

javax.mail.MessagingException: java.net.SocketTimeoutException: Read timed out

not after 1000 ms, but after 5000*60 ms = 5 min

不是在 1000 毫秒之后,而是在 5000*60 毫秒 = 5 分钟之后

What is wrong ? How can i reduce timeoute ?

怎么了 ?我怎样才能减少超时时间?

采纳答案by kuba44

I resolve my problem by changing to the newest version of JavaMail (to JavaMail 1.5). I write about it there: http://openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html

我通过更改为最新版本的 JavaMail(到 JavaMail 1.5)解决了我的问题。我在那里写过:http: //openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html

thank's everybody for help, specially to Bill Shannon :)

感谢大家的帮助,特别是 Bill Shannon :)

回答by Shamim Ahmmed

Can you setup the Socket I/O timeout as well. When it is connected but failed to read data from the server then it will continue to wait.

您也可以设置 Socket I/O 超时。当它已连接但未能从服务器读取数据时,它将继续等待。

prop.put("mail.smtp.timeout", 1000);

Read timeoutindicates you are connected but not able to read data from the server.

读取超时表示您已连接但无法从服务器读取数据。

回答by robbyone

No, it is just because value must be a string "1000" and not an integer 1000

不,只是因为 value 必须是字符串“1000”而不是整数 1000

回答by Bar?? ?zdemir

I had the same problem. It worked with the String instead of integer.

我有同样的问题。它使用字符串而不是整数。

prop.put("mail.smtp.timeout", "1000");    
prop.put("mail.smtp.connectiontimeout", "1000");    

回答by J. Snow

Since you're using SSL, you can try to configure smtpsnamespace, not smtp:

由于您使用的是 SSL,您可以尝试配置smtps命名空间,而不是smtp

prop.put("mail.smtps.timeout", 1000);
prop.put("mail.smtps.connectiontimeout", 1000);

BTW: Timeout values in the properties can be passed as int, as well as String. JavaMail will handle them both properly (at least v1.5+).

顺便说一句:属性中的超时值可以作为int,以及String. JavaMail 将正确处理它们(至少 v1.5+)。