Image for post
Image for post

Spring Boot: Listening for incoming emails in your application

David Marko
Jan 26, 2017 · 2 min read

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 ...

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store