bbottema / simple-java-mail

Simple API, Complex Emails (Jakarta Mail smtp wrapper)
http://www.simplejavamail.org
Apache License 2.0
1.2k stars 261 forks source link

Maxing out SMTP server concurrent connections #503

Closed steveohara closed 2 months ago

steveohara commented 2 months ago

Our application is a monitoring and reporting service that sends emails to alert people of events etc. The code was using javamail direct but I switched it to simple-java-mail a while back to make our attachment handling better.

All has been good except for one customer that is reporting that we are maxing out their self-imposed limits on concurrent connections to their SMTP relay so they blacklist us. Our application runs the reports in their own thread pool so it's 'possible' that we are exceeding their limit but as we haven't changed this aspect of the service it seems unlikely to be the root cause.

We are not using batching and are not setting any connection pool parameters. Has anyone experienced any lingering of connections or similar? Should I be explicitly closing the Mailer after sendMail (thought this only had an effect if we were using asynch/batching)

bbottema commented 2 months ago

Reviewing the documentation, I realized it can be improved so that it covers your question. I've updated it, so please have a look at How connections work without batch-module.

In your case, not having the batch-module on the class-path, unless you provide your own executorService to your Mailer, there is never more than one connection active to the server at any one time. That is, unless you manually create (multiple) Mailer instances on the fly in each of your own managed threads, which is not recommended; reuse a Mailer instances across threads and configure its thread pool separately using an ExecutorService provided to the Mailer.