Monday, February 4, 2013

Notifications made easy

Did you ever miss a general notification service. I just had the situation that the built in notifications of the given system ( ... it was not Alfresco, but this big parent of it) were note suitable enough. So I decided to realize my own notification system by using Freemarker templates (

The task is to create an HTML email body by using an email template instead to build your text by using String concatenation. So what I did is the following:

  •  I created 3 classes: Model, Template and Processor
  • The Model class is just a wrapper which allows you to add key-value-pairs to a root-HashMap. I just used it to avoid to add data to a Map<Object,Object>.
  • The Template has a constructor which allows you to specify the template root location. The default constructor reads the 'folder' attribute from a property file which must be stored within the classpath ( The get-Method returns an instance of the Freemarker template which has the requested name (E.G. simple.ftl)
  • The Processor generates the output dependent on the model and the template.
  • I also added a helper class for reading the configuration from the file.
  • 2 Exception classes were created to differ between 'Template not found' and 'Template could not be processed'.
  • The Main class has only testing purposes

My quite simple mail body template has 4 regions. One for the headers, one for the text of the message, one to print out some key value pairs and finally one for a footer.


<div id="header" style="font-family:Arial; color:#5678a9;">






<div id="content" style="font-family:Arial;">





<div id="props" style="font-family:Arial;">

<#list props as prop>

  <b>${prop.key}</b>: ${prop.value} </br>





<div id="footer" style="font-family:Arial;">




The Main class wires the model and the template together by using the Processor class.

        Model model = new Model();


        model.add("title", "Notification");

        model.add("subtitle", "Important info");

        model.add("text", "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog.");


        List<Map<String,String>> keyvaluePairs = new ArrayList<Map<String,String>>();


        Map<String, String> keyvaluePair1 = new HashMap<String, String>();

        keyvaluePair1.put("key", "User");

        keyvaluePair1.put("value", "David Maier");


        Map<String, String> keyvaluePair2 = new HashMap<String, String>();

        keyvaluePair2.put("key", "E-Mail");

        keyvaluePair2.put("value", "");







        model.add("footer", "More ECM stuff is available here:  <a href=''></a>");


        String result = Processor.process(model, "simple.ftl");



The result looks already quite good:

I also already added the Java Mail API dependencies and the source code tree is now the following one. Maybe I will publish the code later.

1 comment:

  1. Added the source code here: