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.
Create a class called
MailSenderAsyncService
that implements theMailSender
interface.Import the
org.springframework.core.task.TaskExecutor
package.Create a private class called
MailRunnable
. Here is the complete code forMailSenderAsyncService
:public class MailSenderAsyncService implements MailSender{ @Resource(name = "mailSender") private MailSender mailSender; private TaskExecutor taskExecutor; @Autowired public MailSenderAsyncService(TaskExecutor taskExecutor){ this.taskExecutor = taskExecutor; } public void send(SimpleMailMessage simpleMessage) throws MailException { taskExecutor.execute(new MailRunnable(simpleMessage)); } public void send(SimpleMailMessage[] simpleMessages) throws MailException ...