Search code examples
javaasynchronousjakarta-mailquartz-schedulersynchronous

Quartz job to send email using Javamail, but Javamail is synchronous


The title basically sums up my issue at the moment. If I have multiple instances of the Job running at/around the same time, Javamail throws an exception as it's meant to be used synchronously. Is there a way I can make it run asynchronously? Or is there an alternative to Javamail that is asynchronous?


Solution

  • I think there are several ways to deal with such kind of tasks:

    1. wrap up into a service which then periodically send batch of emails;
    2. send each email in its on thread, i.e. imply separate SMTP connection;

      @Async
      public void sendEmail(String smtpServer, String to,String from,String subject, String body) {
            send(smtpServer, to, from, subject, body);
      }