带 UI 的 Java 电子邮件模板?

发布于 2024-10-19 00:33:38 字数 215 浏览 2 评论 0原文

我的网站是用 Java/JSP 构建的,需要发送大约 5-10 封预定义的电子邮件,然后向注册者发送临时电子邮件。我希望避免“重新发明轮子”。我希望电子邮件模板可以通过所见即所得类型的 gui 进行简单管理。到目前为止,我读到的所有内容(Velocity、Freemarker 等)都适用于预定义模板,没有 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 really help with adhoc emails.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惟欲睡 2024-10-26 00:33:38

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

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

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

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

为了将构建的电子邮件作为字符串获取,我执行以下操作:-

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文