Java Mail timeout & connectiontimeout handling


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?

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

To answer my own question: Is it possible to catch  & handle this timeout operations?

Yes, it is automatically thrown as MessagingException. It looks like as given below.

Regarding simulation/reproducing of timeout operation, I still don't have idea.

I'll go ahead and close this question.

javax.mail.MessagingException: Exception reading response;
  nested exception is: Read timed out
        at com.sun.mail.smtp.SMTPTransport.readServerResponse(
        at com.sun.mail.smtp.SMTPTransport.openServer(
        at com.sun.mail.smtp.SMTPTransport.protocolConnect(
        at javax.mail.Service.connect(
        at javax.mail.Service.connect(
        at javax.mail.Service.connect(

Open in new window

Gurvinder Pal SinghCommented:
check this link, you can set these properties in you code

props.put("mail.smtp.connectiontimeout", "2000"); \\connection time out in milliseconds

props.put("mail.smtp.timeout", "2000"); \\IO time out in milliseconds

ZoniacAuthor Commented:
Hi gurvinder372,

I know that this property can be set as given in your link.

My question was:
1) In case if timeout and/or connectiontimeout are reached, is it possible to handle this exception in Java/JavaMail?  In other words, will Java/JavaMail throw this as an exception, so that I can handle this specifically in my application?

2) Also, is it possible to simulate/reproduce this timeout operation on my own assuming I've complete administration access to the SMTP server?
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Gurvinder Pal SinghCommented:
1) I think there is different specific exception thrown for different configuration
please check this link for an example

you can catch SendingFailedException, and then find out what was the root cause.

2) You can always make the mail server un-available (by disconnecting the network or shutting down mail server)

ZoniacAuthor Commented:
Hi gurvinder372,

On timeout operation, I'm thinking of at it in 2 different aspects:
1) SMTP server is not down:  In this case, the SMTP server is busy servicing email requests concurrently and continuously for more than 5 minutes (I'm sending bulk mails).  At some point of time during servicing, it is not returning or throwing any exceptions to the Java/JavaMail program.  In fact, I've experienced this kind of situation in my real time.  In case, if I've not set both mail.smtp.timeout and mail.smtp.connectiontimeout properties within my code, it would never return and goes into infinite state.

How can we simulate/reproduce this kind of situation?  Any ideas?

2) SMTP server itself is down or not running: In this case, it is straight-forward.  It would clearly say "Could not connect to SMTP host" and returns back throwing exception.  And this in turn can be easily handled within Java/JavaMail.
you can simulate tcp server socket by typing on linux machine:
nc -l machine_ip:port

It will listen on specific ip and port and you can test timeout exceptions on connection to this socket.


Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.