Pandora's Box for Liferay: FakeSMTP hook

This is part 1 of a series of blog posts about a collection of tools I created during the last couple of months as a reaction to some problems I ran into.

The first one is how to handle/debug emails while testing mail related items in Liferay. There is of course the standard option of configuring Liferay to use your company's mail server or your personal email account at Google/Yahoo/etc... and using the mail.session.mail.debug=true property in your portal-ext.properties. While this works it does depend on external infrastructure and can be difficult to set up with regards to authentication. Even when you are able to get it set up correctly network issues can still be a problem and prevent your email from working correctly.

So preferably we'd like to work locally. While you could set up a local mail server there is a simpler option: FakeSMTP. This is a Java based mail server that you can run locally as a JAR that also gives you a GUI to view the email messages it received with. Because I'm a lazy developer and would probably also forget to launch the JAR each time I start Liferay I decided to try and wrap it in a hook so that it would get started automatically each time Liferay is started (or the hook is deployed).

Luckily this turned out to be pretty easy as FakeSMTP is available as a Maven artifact and so the combination of a ServletContextListener and a ProcessBuilder did the trick:

public class StartFakeSMTPListener implements ServletContextListener {

   private final Logger log = LoggerFactory.getLogger(StartFakeSMTPListener.class);

   private Process process;

   @Override
   public void contextInitialized(ServletContextEvent sce) {
      try {
         String pathToLib = sce.getServletContext().getRealPath("/WEB-INF/lib/fakesmtp-2.0.jar");

         ProcessBuilder processBuilder = new ProcessBuilder();
         processBuilder.redirectErrorStream(true);
         processBuilder.inheritIO();
         processBuilder.command("java", "-jar", pathToLib, "--start-server", "--port", "2525", "--bind-address", "127.0.0.1", "--memory-mode");

         process = processBuilder.start();
      } catch (Exception e) {
         log.error("Unexpected problem starting FakeSMTP", e);
      }
   }

   @Override
   public void contextDestroyed(ServletContextEvent sce) {
      if (process.isAlive()) {
         process.destroy();
      }
   }
}

This simple listener retrieves the path to the embedded FakeSMTP JAR and then uses a ProcessBuilder to launch the JAR with the correct configuration (it will listen for email locally on port 2525). You can find the code on my Github: https://github.com/planetsizebrain/liferay-pandora-box. When you build and deploy it to a Liferay that has been configured with mail.session.mail.smtp.port=2525 in its portal-ext.properties you should see something like this:

 

The Groovy script I used to send an email is:

import javax.mail.internet.*;
import com.liferay.portal.kernel.mail.*;
import com.liferay.mail.service.*;

MailMessage message = new MailMessage(new InternetAddress("from@liferay.com"), new InternetAddress("to@liferay.com"), "Dummy Subject", "Hello World!", false);
MailServiceUtil.sendEmail(message);

Feel free to use it to test the FakeSMTP hook with and if everything goes well: You've Got Mail.

3
Blogs
Nice emoticon

Did you know that we have used FakeSMTP on our trainings too, but the startup has to be manual.