views:

395

answers:

2

Hi,

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?

A: 

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?

Gnanam
+2  A: 

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.

Janning
@Janning: Thanks for that simulation steps. I'll try that.
Gnanam