[英]Why JavaMail connection timeout is too long
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.我将连接超时设置为 1000 ms = 1s,但它被忽略了。 When i debug and set wrong username and password i catch
当我调试并设置错误的用户名和密码时,我发现
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 ?
我怎样才能减少超时时间?
Can you setup the Socket I/O timeout as well.您也可以设置 Socket I/O 超时。 When it is connected but failed to read data from the server then it will continue to wait.
当它已连接但未能从服务器读取数据时,它将继续等待。
prop.put("mail.smtp.timeout", 1000);
Read timeout indicates you are connected but not able to read data from the server.读取超时表示您已连接但无法从服务器读取数据。
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");
Since you're using SSL, you can try to configure smtps
namespace, 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
.顺便说一句:属性中的超时值可以作为
int
和String
传递。 JavaMail will handle them both properly (at least v1.5+). JavaMail 将正确处理它们(至少 v1.5+)。
不,只是因为 value 必须是字符串“1000”而不是整数 1000
I resolve my problem by changing to the newest version of JavaMail (to JavaMail 1.5).我通过更改为最新版本的 JavaMail(到 JavaMail 1.5)解决了我的问题。 I write about it there: http://openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html
我在那里写过: http : //openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html
thank's everybody for help, specially to Bill Shannon :)感谢大家的帮助,特别是 Bill Shannon :)
mail.smtp.connectiontimeout邮件.smtp.connectiontimeout
type: int类型:int
Desc: Socket connection timeout value in milliseconds.描述:以毫秒为单位的套接字连接超时值。 This timeout is implemented by java.net.Socket.
这个超时是由 java.net.Socket 实现的。 Default is infinite timeout.
默认为无限超时。
mail.smtp.timeout type: int mail.smtp.timeout类型:int
Desc: Socket read timeout value in milliseconds.描述:以毫秒为单位的套接字读取超时值。 This timeout is implemented by java.net.Socket.
这个超时是由 java.net.Socket 实现的。 Default is infinite timeout.
默认为无限超时。
mail.smtp.writetimeout type: int mail.smtp.writetimeout类型:int
Desc: Socket write timeout value in milliseconds.描述:以毫秒为单位的套接字写入超时值。 This timeout is implemented by using a java.util.concurrent.ScheduledExecutorService per connection that schedules a thread to close the socket if the timeout expires.
此超时是通过对每个连接使用 java.util.concurrent.ScheduledExecutorService 来实现的,该服务安排一个线程在超时到期时关闭套接字。 Thus, the overhead of using this timeout is one thread per connection.
因此,使用此超时的开销是每个连接一个线程。 Default is infinite timeout.
默认为无限超时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.