views:

816

answers:

4

We have a web application that periodically sends out e-mails to users. At the moment we generate the html version and the text version in the code. However, this is cumbersome to maintain.

Is there a good e-mail template system out there that can generate both the html and text versions of an e-mail from the same template for Java?

Some requirements:

  • any pictures/icons correctly embedded as mimeparts and properly linked
  • the text version needs to somewhat resemble the html version — for the benefit of users that see the text version and spam detectors
  • links need to show the URL in the text version
  • full control (or as much as possible) of the layout and style

In case it matters to your answer, we're using Struts… <cough>1</cough>.

A: 

I have successfully used Freemarker templates for preparing txt and html content for emails. If you use Spring it can make things even easier since it has all necessary plumbings. Even without Spring, JavaMail API is user friendly so it shouldn't be a problem.

Boris Pavlović
I couldn't find anything in the documentation about generating html and text from the same template. Am I missing something?
Francisco Canedo
A: 

Take the Apache Velocity. I don't know a more powerfull template engine.

furtelwart
I couldn't find anything in the documentation about generating html and text from the same template. Am I missing something?
Francisco Canedo
+2  A: 

You could use a utility which transforms HTML to Text in Java (Google for it, for example this one) by stripping tags and converting the special HTML chars. However, this will not give you everything you need, especially not formatting (like lists) and links.

Another option is to use an XSLT to transform your XHTML (write it properly...) to text and use an XSLT processor (like Xalan-J or Saxon) to run it. This is a fairly simple XSLT exercise as long as your requirements are simple (e.g. you don't care about CSS issues).

zvikico
+2  A: 

I would also recommend the use of a template engine. Create two templates, one for the text part and one for the HTML part. Use the include functionality of the template engine so you can re-use literal texts in both templates.

Jeroen van Bergen