// create reponse message
MimeMessage response = emailerFactory.createMessage();
emailerFactory.setTo(response, user.getEmail());
EmailTemplate template = errorMessageTemplates.get(actionName);
if (template == null)
{
log.warn("Missing template for error response message for action {}. Sending generic response.",
actionName);
emailerFactory.setSubject(response, "Error processing received email");
emailerFactory.setTextBody(response,
"There was an error processing your email. " + cleanException.getMessage()
+ " Original message is attached.");
}
else
{
// prepare for template rendering
Context velocityContext = new VelocityContext(velocityGlobalContext);
velocityContext.put("action", actionName);
velocityContext.put("params", actionSelection.getParams());
velocityContext.put("user", user);
velocityContext.put("exception", cleanException);
velocityContext.put("originalException", inException);
velocityContext.put("settings", systemSettingsDao.execute(null));
// build the subject
StringWriter writer = new StringWriter();
velocityEngine.evaluate(velocityContext, writer, "EmailSubject-" + actionName,
template.getSubjectTemplate());
emailerFactory.setSubject(response, writer.toString());
// build the text body
Template vt = velocityEngine.getTemplate(template.getTextBodyTemplateResourcePath());
writer.getBuffer().setLength(0);
vt.merge(velocityContext, writer);
emailerFactory.setTextBody(response, writer.toString());
// build the HTML body
vt = velocityEngine.getTemplate(template.getHtmlBodyTemplateResourcePath());
// HTML-escape all content inserted
EventCartridge ec = new EventCartridge();
ec.addEventHandler(new EscapeHtmlReference());
ec.attachToContext(velocityContext);
writer.getBuffer().setLength(0);