bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)

Home Page:http://www.simplejavamail.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Apply configurable timeouts when sending emails

amanteaux opened this issue · comments

I will start with my use case: I encountered an OutOfMemoryError on a production server that was due to emails that were queuing.
To give an insight of what where going, here is what was contained in the heap space:
image

When the production team detected the error, they restarted the server, here is an extract of the logs:
logs_exact.txt

I think the problem is that in MailSender the executor threads were all waiting to get a connection from the SMTP server and during this time mails were added to the executor queue until the server run out of memory. So the real problem is that the SMTP server was down and no timeout are configured in SMTPTransport class.
To solve this problem, a solution would be to add a timeout in MailSender that will cancel the sending task if it takes more that X seconds/minutes like in https://stackoverflow.com/a/2759040/3790208.

Moreover, though the code is really nicely written in MailSender, I noticed 2 things that can be improved:

  • line 165: a Runnable should be provided instead of a Thread object that adds a little overhead,
  • line 159: sendMailClosure should be wrapped in a try catch to at least show the date of the exception.

I can make a pull request if that is ok.

By all means, give it a shot. I'm on my phone right now, I'll give a proper reply when I get a chance.

Thank you for your quick answer! I will try to put up something this week.

Released in 4.3.0.

Default timeout set to 1 minute. Can be set programmatically (mailer.setSessionTimeout(int)), or with property simplejavamail.defaults.sessiontimeoutmillis.