Using freemarker to generate html to send within a

2019-06-08 18:38发布

The e-mail is being sent using Spring and java mail sender.

Is their anyway I can use a normal jsp view and jstl tags - I don't want to learn another bunch of tags/syntax ?

Currently my code looks like this :

StringBuffer content = new StringBuffer();
Configuration configuration = freeMarkerConfigurer.getConfiguration();
String templateName = "vslEmail.ftl";
Map<String, String> templateVars = new HashMap<String, String>();
templateVars.put("firstName", "john");
templateVars.put("surname", "doe");
try {
  content.append(FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(ftlName), tempalteVars));
}
catch (Exception e) {
  // handle
}
// content.append("<br>Test data");
sendMime(defaultEmailAddress, subject, content.toString());

I would just prefer to reference a jsp instead of the ftl ?

2条回答
叛逆
2楼-- · 2019-06-08 19:06

You can explicite render a jsp, but you need a Request Object!

request.getRequestDispatcher("/WEB-INF/mai/myMail.jsp").include(request, response);

See also this StackOverflow Answer (and the other answers to that question). It shows an example to create a (fake) response object, that allows you to process the created html.

查看更多
We Are One
3楼-- · 2019-06-08 19:10

You could always fire off a request to a JSP sitting on your own web server, slurp up the response, and drop it into your email. It's a little hacky, and care must be taken to make sure you don't open up those pages to the outside world, but it would work.

查看更多
登录 后发表回答