Skip to content

Apply configurable timeouts when sending emails #85

Closed

Description

@amanteaux

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.

Activity

  1. bbottema commented on Jul 10, 2017

    @bbottema
    Owner

    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.

  2. amanteaux commented on Jul 10, 2017

    @amanteaux
    ContributorAuthor

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

  3. added this to the 4.3.0 milestone on Aug 9, 2017
  4. added 5 commits that reference this issue on Aug 12, 2017
  5. self-assigned this
    on Aug 12, 2017
  6. changed the title [-]Add timeout to send email asynchronously [/-] [+]Apply configurable timeouts when sending emails[/+] on Aug 12, 2017
  7. bbottema commented on Aug 12, 2017

    @bbottema
    Owner

    Released in 4.3.0.

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

  8. bbottema commented on Aug 26, 2026

    @bbottema
    Owner

    Current 9.x usage (verified against 9.3.2):

    Mailer mailer = MailerBuilder
        .withSMTPServer("smtp.example.com", 587)
        .withSessionTimeout(10_000) // milliseconds
        .buildMailer();

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions