为什么JavaMail连接超时太长

在我的应用程序中,我连接到服务器以validation用户。 这是代码:

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; } 

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

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

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

哪里不对 ? 我怎样才能减少时间?

您也可以设置套接字I / O超时。 当它连接但无法从服务器读取数据时,它将继续等待。

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

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

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

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

BTW:属性中的超时值可以作为intString传递。 JavaMail将正确处理它们(至少v1.5 +)。

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

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

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

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

谢谢大家的帮助,特别是比尔香农:)