【问题标题】:why could have connection timed out exception if the default timeout value is infinite?如果默认超时值是无限的,为什么会出现连接超时异常?
【发布时间】:2016-06-18 13:56:44
【问题描述】:

使用springJavaMailSender发送邮件,有时会出现以下异常

org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: mail.foo.com, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: mail.foo.com, 25; timeout -1;
  nested exception is:
java.net.ConnectException: Connection timed out
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:432) ~[spring-context-support-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:345) ~[spring-context-support-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340) ~[spring-context-support-4.2.6.RELEASE.jar!/:4.2.6.RELEASE]

一开始我想可能是默认的超时时间不够长,我可以设置一个更长的值来避免异常。例如

<prop key="mail.smtp.timeout">10000</prop>

但是当我阅读文档时,我发现默认超时值为infinite

请看:https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

如果它是无限的,它应该永远被阻止,为什么会有ConnectException

【问题讨论】:

标签: spring jakarta-mail connection-timeout


【解决方案1】:

由于您的问题更多是关于为什么您会收到Connection timed out 默认超时值是无限的;我的 2 美分将是查看操作系统的连接超时值。要在 linux 中检查或修改它(我认为默认为 20 秒),你可以看看这个:How to view/change socket connection timeout on Linux?

即使您使用任何其他操作系统,您也应该能够通过谷歌搜索如何检查或修改该值。

但是,这只会回答您的问题,而不会解决您的问题。要解决您的问题,您需要查看不同的连接失败原因(在 Roman C 提供的链接中提供)并检查问题所在。最常见的是您使用 http/https 而服务器允许另一个,或者如果不是服务器端的防火墙问题,则存在一些端口或 url 问题。

【讨论】:

    猜你喜欢
    • 2012-01-12
    • 2010-09-10
    • 2012-04-16
    • 2011-12-02
    • 1970-01-01
    • 2018-11-12
    • 2010-10-26
    • 2019-12-04
    • 1970-01-01
    相关资源
    最近更新 更多