O'Reilly logo

Mastering Spring Application Development by Anjana Mankale

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Sending Spring mails over a different thread

There are other options for sending Spring mail asynchronously. One way is to have a separate thread to the mail sending job. Spring comes with the taskExecutor package, which offers us a thread pooling functionality.

  1. Create a class called MailSenderAsyncService that implements the MailSender interface.
  2. Import the org.springframework.core.task.TaskExecutor package.
  3. Create a private class called MailRunnable. Here is the complete code for MailSenderAsyncService:
    public class MailSenderAsyncService implements MailSender{ @Resource(name = "mailSender") private MailSender mailSender; private TaskExecutor taskExecutor; @Autowired public MailSenderAsyncService(TaskExecutor taskExecutor){ this.taskExecutor = taskExecutor; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required