javaemailjakarta-mailsimple-java-mail

Can the Simple Java Mail Mailer instance be reused? Is it thread safe?


I need some clarification about the usage of Mailer class of the Simple Java Mail library, after creating a Mailer instance like:

Mailer mailer = MailerBuilder.withSMTPServer(
        smtpHost,
        smtpPort,
        smtpUser,
        smtpPassword
    ).
    withTransportStrategy(TransportStrategy.SMTPS).
    buildMailer();

I send email with mailer.sendEmail(email).

Can the Mailer instance be reused to send multiple mails? Or I have to create one for every email I want to send? Right now I tested and it seems to be OK to send multiple emails but not sure.

Is it thread safe?


Solution

  • The documentation of Simple Java Mail explicitly says you can create the mailer once and reuse it many times and that it's thread-safe, specifically the section Configure once, reuse many times:

    You can preconfigure a Mailer and use it many times. It is thread-safe.

    Additionally it provides forms of Asynchronous parallel batch sending and clustering, which further reinforces its thread-safety. Or at least, if something provides parallel batch sending, I would expect it to be thread-safe, even if it hadn't been declared explicitly.