问题
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