2011-02-23 112 views
2

我的网站使用Java/JSP构建,需要发送约5-10个预定义的电子邮件,然后将电子邮件发送给已注册的用户。我正在寻求避免“重新发明轮子”。我更喜欢如果电子邮件模板可以简单地用所见即所得类型的gui进行管理。到目前为止,我读过的所有内容(Velocity,Freemarker等)都适用于预定义的模板,没有用户界面,也没有真正帮助特别电子邮件。带UI的Java电子邮件模板?

只是好奇我最好自己写点东西还是有东西可以帮助?

回答

2

为什么你需要一个GUI来制作你的电子邮件?最好尽可能简化电子邮件内容,而不要在其中嵌入HTML标签。如果你的用户决定用纯文本/文本打开电子邮件,那么他们看到的只是一堆丑陋的标签。

使用模板引擎(如Velocity或Freemarker)是制作电子邮件模板的方法。即使使用adhoc电子邮件,您也可能希望使用电子邮件模板使页眉和页脚保持不变,并且可以使用adhoc消息替换正文内容。

在我的项目,我有使用Velocity电子邮件模板,像这样: -

异常email.vm文件

** 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 

为了得到构建电子邮件作为字符串,我做的以下内容: - :

-

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); 
} 

当然,我在velocityEngine使用Spring线

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