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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?
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)

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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.
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Email Protocols

From novice to tech pro — start learning today.