Spring Boot: Listening for incoming emails in our applications

David Marko  30 January 2017 05:31:00
There are several options how to process incoming emails in our applications. We can for example route email into standard mailbox and periodically download emails into our application with POP3 protocol. Or we can run our own SMTP server inside Spring Boot application. And this is the case!

Adding Spring Boot project dependency

I prefer using Gradle for all my Spring Boot projects so adding dependency means adding one line into build.gradle file in my project directory. SubethaSMTP is library that contains embeddable SMTP server implementation, great option for our purpose.

compile ("org.subethamail:subethasmtp:3.1.7")

Starting SMTP server when Spring Boot starts

SMTP is an active service that must be started during Spring Boot application startup. To do so, we can create @Service class, like the one below, with @PostConstruct and @PreDestroy annotated methods. We can also inject properties parameters that will help us to configure service properly. This code is detected during Spring Boot start up process and @PostConstruct method is run for us. We must not to forget to stop SMTP server in @PreDestroy method otherwise the service would occupy the TCP port.


public class SMTPServerService {


String enabled=””;


String hostName=””;


String port=””;

SMTPServer smtpServer;

public SMTPServerService() {



public void start() {


SimpleMessageListenerImpl l = new SimpleMessageListenerImpl();

smtpServer = new SMTPServer(new SimpleMessageListenerAdapter(l));




System.out.println(“****** SMTP Server is running for domain “+smtpServer.getHostName()+” on port “+smtpServer.getPort());

} else {

System.out.println(“****** SMTP Server NOT ENABLED by settings “);




public void stop() {


System.out.println(“****** Stopping SMTP Server for domain “+smtpServer.getHostName()+” on port “+smtpServer.getPort());




public boolean isRunning() {

return smtpServer.isRunning();



Processing incoming emails

While  starting SMTP server with code above, we specified ‘SimpleMessageListenerImpl()’ class as SMTP Server parameter. The processing class must implement ‘SimpleMessageListener’ that requires implementation of two methods only. Method accept can check originator / recipient emails and optionally allow or reject email delivery. Method deliver then processes email based on application logic. Everything is quite clear and processing requires minimal of code.

public class SimpleMessageListenerImpl implements SimpleMessageListener {

public SimpleMessageListenerImpl() {



public boolean accept(String from, String recipient) {

    return true;



public void deliver(String from, String recipient, InputStream data) {

 Session session = Session.getDefaultInstance(new Properties());

 MimeMessage m = new MimeMessage(session,data);

 ReceivedEmail email=new ReceivedEmail(m);
 // ... here we go with email message ...


You can find original blog post here ...