views:

898

answers:

4

we have an application that needs to send out various different types of template email. The current code is very cumbersome and not very flexible. Does any one konw of a library to help with this type of work... We are looking for some kind of templating library for email.

+7  A: 

Perhaps Apache Velocity could work for you?

javamonkey79
+1 I've used Velocity + Spring for exactly this before.
cletus
Me too. (Velocity, not Spring)
Evan
+4  A: 

StringTemplate is also a very nice template engine.

Dave Ray
Good call. As a side-note, you would want to do the replacements BEFORE passing the subject/body content into an API like JavaMail. Otherwise, you may end-up producing an invalid EML file due to encoding or line wrap issues.
Chase Seibert
We ended up using StringTemplate. Very simple yet flexible for our situation.
J. Scarbrough
+4  A: 

I prefer Freemarker, here over Velocity. I am finding Freemarker much more simple in this case. Check out Freemarker vs Velocity.

If you are using Spring, then you may be interested in using Velocity or Freemarker with Spring Framework.

Adeel Ansari
A: 

I ran into a similar problem about a year ago. In our case, our front end developers were all familiar with JSP, and I really did not want to throw another templating engine into the mix. I something that relied on the servlet container's JSP processor to generate e-mail content for me.

It's fairly straightforward:

  1. I had to have a JSP page in my application (you can put it in /WEB-INF if you don't want it externally accessible).
  2. I wrote a custom HttpServletResponse and ServletOutputStream that captures content written by the servlet container and turns it into a String, and relied on RequestDispatcher.include(...) to make a "request" to the template JSP (I also wrote a custom HttpServletRequest to isolate the original request from mutation).
  3. Because this is a bit of a hack, and not the way the servlet API was intended to be used, I encapsulated all this in a utility class, so that all the client code has to do is pass in the path to the JSP template, and get back the processed content.
Jack Leow