Using freemarker to generate html to send within an e-mail, is their anyway to use a rendered jsp page instead?

独自空忆成欢 提交于 2019-12-08 09:05:08

问题


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 ?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10242625/using-freemarker-to-generate-html-to-send-within-an-e-mail-is-their-anyway-to-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!