Java timeout timeout and connection time

I am using JavaMail to send email requests to an SMTP server.

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

Programmatically, I want to catch both the time when the timeout, and / or the session execution time are reached in Java and process things accordingly. Turning in the sense, I need to repeat the same email address again next time.

How do I handle this in Java / JavaMail? Can these timeouts be captured and processed?

EDIT

In addition, is it possible to simulate / play this timeout operation on my own if I have full administrative access to the SMTP server?

+5
source share
2 answers

Answering your second question: on your test computer, just DROP all outgoing connections to your SMTP server using iptables:

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

Thus, it looks like an unresponsive SMTP server, and you can test exception handling.

+4
source

All:

Answering my question, I experienced this myself.

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

Yes, it is automatically called 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 selected in this line:

Transport.connect ();

, / , SMTP-? "

?

+1

All Articles