带有 UI 的 Java 电子邮件模板?

标签 java email templates

我的网站是用 Java/JSP 构建的,需要发送大约 5-10 封预定义的电子邮件,然后再向已注册的人发送临时电子邮件。我希望避免“重新发明轮子”。如果可以使用 WYSIWYG 类型的 gui 简单地管理电子邮件模板,我会更喜欢。到目前为止,我读到的所有内容(Velocity、Freemarker 等)都适用于预定义模板,没有 UI 并且对临时电子邮件没有真正帮助。

只是好奇我最好还是自己写一些东西,还是有什么可以提供帮助的东西?

最佳答案

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

使用 Velocity 或 Freemarker 等模板引擎是制作电子邮件模板的方法。即使是临时电子邮件,您可能希望使用电子邮件模板保持页眉​​和页脚相同,并且您可以用临时邮件替换正文内容。

在我的项目中,我有一个使用 Velocity 的电子邮件模板,如下所示:-

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

当然,我正在使用 Spring 连接 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>

关于带有 UI 的 Java 电子邮件模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5092929/

相关文章:

html - Rockettheme 模板侧边栏出现故障

Java泛型无法将类型转换为类型

java - 右键单击在 NetBeans 8.0.2 中不起作用

Java/Windows 获取应用程序位置

java - 有没有一种方法可以对重构交换整数进行编码

ruby-on-rails - 如何处理来自 Rails 应用程序的带有多个大附件的传入电子邮件?

javascript - 电子邮件正文变量

email - 如何在 FOSUserBundle 中为 Controller 重置设置 sender_name?

c++ - 获取没有对象的成员函数的返回类型

c++ - 模板实际上必须是编译时构造吗?