开发者

Java Email Templates with UI?

My website, built in Java/JSP, will need to send about 5-10 predefined emails and then adhoc emails to those who are registered. I'm looking to avoid 'reinventing the wheel'. I'd prefer if the email templates could be simply managed with a WYSIWYG type of gui. So far everything I've read about (Velocity, Freemarker, etc) are OK for the predefined templates, don't have a UI and don't re开发者_JS百科ally help with adhoc emails.

Just curious am I best off writing something myself or is there something out there that can help?


Why do you need a GUI to craft your emails? It is best to keep the email content as simple as possible instead of embedding HTML tags in it. If your user decides to open the email with plain/text, then all they see are just bunch of ugly tags.

Using template engine such as Velocity or Freemarker is the way to go to craft your email template. Even with adhoc emails, you may want to keep the header and footer to be the same using the email template and you can swap out the body content with your adhoc messages.

In my project, I have an email template using Velocity, like this:-

exception-email.vm file

** Please do not reply to this message **

The project administrator has been notified regarding this error.
Remote Host : $remoteHost
Server      : $serverName
Request URI : $requestURI
User ID     : $currentUser
Exception   : 

$stackTrace

To get the constructed email as string, I do the following:-

private String getMessage(HttpServletRequest request, Throwable t) {
    Map<String, String> values = new HashMap<String, String>();
    values.put("remoteHost", request.getRemoteHost());
    values.put("serverName", request.getServerName());
    values.put("requestURI", request.getRequestURI());
    values.put("currentUser", ((currentUser != null) ? currentUser.getLanId() : "(User is undefined)"));

    StringWriter sw = new StringWriter(500);
    t.printStackTrace(new PrintWriter(sw));

    values.put("stackTrace", sw.toString());

    return VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "exception-email.vm", values);
}

Granted, I'm using Spring to wire in velocityEngine:-

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
    <property name="velocityProperties">
        <props>
            <prop key="resource.loader">class</prop>
            <prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
        </props>
    </property>
</bean>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜