Java – What’s a good html e-mail template system that also, correctly, renders a text/plain alternative


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

Best Solution

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

Related Question