You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Apply configurable timeouts when sending emails #85
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:
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.
Default timeout set to 1 minute. Can be set programmatically (mailer.setSessionTimeout(int)), or with property simplejavamail.defaults.sessiontimeoutmillis.
Activity
bbottema commentedon Jul 10, 2017
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.
amanteaux commentedon Jul 10, 2017
Thank you for your quick answer! I will try to put up something this week.
Adding timeouts to SMTP transport sockets for issue bbottema#85
#85 finalized code for setting socket timeout values on the session. …
#85 overhauled setting sessionTimeout, so that it can be set (overrid…
#85 Documented the Java API for overriding session timeout
#85 whoops, forgot to update example code
#85 In case of exceptions, log the failed email
[-]Add timeout to send email asynchronously [/-][+]Apply configurable timeouts when sending emails[/+]bbottema commentedon Aug 12, 2017
Released in 4.3.0.
Default timeout set to 1 minute. Can be set programmatically (
mailer.setSessionTimeout(int)), or with propertysimplejavamail.defaults.sessiontimeoutmillis.bbottema commentedon Aug 26, 2026
Current 9.x usage (verified against 9.3.2):
The old mutable setter moved to the mailer builder in 5.0. This single value applies to the SMTP connect, read, and write timeouts. See the 5.0 builder migration and current programmatic configuration.