Java Mail timeout & connectiontimeout handling

和自甴很熟 提交于 2019-12-12 11:03:38

问题


I'm using JavaMail to send email requests to an SMTP server.

I would like to set both "mail.smtp.connectiontimeout" and "mail.smtp.timeout" properties within my code.

Programmatically, I want to catch both when timeout and/or connectiontimeout operations are reached in Java and handle things accordingly. Handling in the sense, I need to retry the same email once again the next time.

How do I handle this in Java/JavaMail? Is it possible to catch & handle this timeout operations?

EDIT

Also, is it possible to simulate/reproduce this timeout operation on my own assuming I've complete administration access to the SMTP server?


回答1:


Answering your second question: On your test machine just DROP all outgoing connections to your SMTP Server with iptables:

   iptables -I OUTPUT 1 -p tcp -s 192.168.1.20 --dport 25 -j DROP

This way it does look like an unresponsive smtp server and you can test your exception handling.




回答2:


All:

Am answering my question, after experiencing this on my own.

How do I handle this in Java/JavaMail? Is it possible to catch & handle this timeout operations?

Yes, it is automatically thrown as javax.mail.MessagingException.

javax.mail.MessagingException: Exception reading response;
  nested exception is:
        java.net.SocketTimeoutException: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
        at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1260)
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
        at javax.mail.Service.connect(Service.java:297)
        at javax.mail.Service.connect(Service.java:156)
        at javax.mail.Service.connect(Service.java:105)

This exception is thrown exactly at this line:

Transport.connect();

Only open question I've now is "Is it possible to simulate/reproduce this timeout operation on my own assuming I've complete administration access to the SMTP server?"

Any ideas from experts?



来源:https://stackoverflow.com/questions/2773108/java-mail-timeout-connectiontimeout-handling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!