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.
+1 I've used Velocity + Spring for exactly this before.
cletus
2009-01-18 23:54:31
Me too. (Velocity, not Spring)
Evan
2009-01-19 01:52:29
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
2009-01-20 15:40:54
We ended up using StringTemplate. Very simple yet flexible for our situation.
J. Scarbrough
2009-03-19 02:55:22
+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
2009-01-19 04:04:40
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:
- 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).
- 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).
- 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
2009-01-19 15:59:42