Reputation: 8477
Use spring JavaMailSender
to send email, sometimes there are below exception
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]
At first I thought maybe the default timeout is not long enough, and I could set a longer value to avoid the exception. e.g.
<prop key="mail.smtp.timeout">10000</prop>
but when I read the document I find the defalt timeout value is infinite
please see: https://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html
So if it is infinite it should be blocked forever, why could have ConnectException
?
Upvotes: 0
Views: 1203
Reputation: 3943
Since your question is more about why you are getting a Connection timed out
when the default timeout value is infinite; my 2 cents would be to look at the operating system's connection timeout value. To check or modify that in linux(which is by default 20 seconds, i think), you can have a look a this: How to view/change socket connection timeout on Linux?
Even if you are using any other OS, you should be able to google around about how to check or modify that value.
However, that would just answer your question and not solve your problem. To solve your problem, you need to look at different connection failure reasons (which are provided in the link provided by Roman C) and check what is wrong. Most common is that you are using http/https while the server allows the other or there is some port or url problem if its not a firewall issue from server side.
Upvotes: 1