Java 中的电子邮件模板库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1176120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Email Template Library in Java
提问by Maksim
I know that there is a library called FreeMarkerto create Email templates for Java. Do you think this library is suitable to use in enterprise projects? Is there any other libraries to create email template for java?
我知道有一个名为FreeMarker的库可以为 Java 创建电子邮件模板。你认为这个库适合在企业项目中使用吗?有没有其他库可以为java创建电子邮件模板?
回答by Brian Agnew
Freemarker works well, and is very powerful. Velocityhas a simpler syntax, is somewhat less powerful, and is a lot more forgiving wrt. nulls (variables not being populated).
Freemarker 运行良好,并且非常强大。Velocity的语法更简单,功能稍弱,但更宽容。nulls(未填充变量)。
I've used both, and Velocity is very easy to get started with. Freemarker (despite its stricter implementation) offers a lot more in terms of 'correctness' and facilities.
我两个都用过,而且 Velocity 很容易上手。Freemarker(尽管其实施更严格)在“正确性”和便利性方面提供了更多。
回答by dfa
yes, it is. For the sake of completeness there are other compelling libraries like:
是的。为了完整起见,还有其他引人注目的库,例如:
- apache velocity
- string templace
- 阿帕奇速度
- 字符串模板
回答by Jeroen van Bergen
A number of years ago I've written an application for sending invoice notifications. I used Velocity to create the actual contents of the e-mails. The transport was provided by James, the e-mail server by Apache. This application sends out 10s of thousands of personalised e-mails each day.
几年前,我编写了一个发送发票通知的应用程序。我使用 Velocity 创建电子邮件的实际内容。传输由 Apache 的电子邮件服务器 James 提供。该应用程序每天发送数以千计的个性化电子邮件。
Velocity did not give me any problems, but having had some experience with FreeMarker, I would probably pick FreeMarker now.
Velocity 没有给我带来任何问题,但是在使用 FreeMarker 之后,我现在可能会选择 FreeMarker。
回答by John Doe
Just for the record, there are many other template enginesthat you can use, and not only for email. In most projects that I've worked on, Velocity was used and when it wasn't Velocity, it was Freemarker.
只是为了记录,您可以使用许多其他模板引擎,而不仅仅是电子邮件。在我参与的大多数项目中,使用了 Velocity,当它不是 Velocity 时,它是 Freemarker。
回答by mbosecke
回答by Zied Hamdi
Before I started doing my email templating, I thought I will ask my gFriend, and I came across the idea of using the JSP engine for templating (if you're in a servlet environment). I found the idea original and perfect for my needs since I'm under GAE and adding jar to GAE is a very costly decision (impact on performance)
在我开始做我的电子邮件模板之前,我想我会问我的 gFriend,我想到了使用 JSP 引擎进行模板的想法(如果你在 servlet 环境中)。我发现这个想法是原创的,非常适合我的需求,因为我在 GAE 下,向 GAE 添加 jar 是一个非常昂贵的决定(影响性能)
And how I came to that link (the original idea) https://stackoverflow.com/a/458088/2033675
以及我是如何找到那个链接的(最初的想法) https://stackoverflow.com/a/458088/2033675
So I put a link to how to do it Suggestions for Java email templating?
所以我放了一个链接,指向如何做Java 电子邮件模板的建议?
naturally in case someone will have the same constrains as I do.
当然,万一有人会和我有同样的限制。
Best
最好的
回答by Hyman Leow
Another option you may want to look into (though probably not) is to use JSP, if you are in a Servlet environment. For the most part, I wouldn't recommend this since it's really quite a lot of up front work to get this set up.
如果您在 Servlet 环境中,您可能想要研究的另一个选项(尽管可能不是)是使用 JSP。在大多数情况下,我不推荐这样做,因为要进行此设置确实需要大量的前期工作。
However, this was a useful solution in our environment, where we have a bunch of web developers who are already familiar with JSP syntax and custom tags. It was nice to be able to have them work on the e-mail contents directly without having to learn another templating language (though I'm sure FreeMarker probably isn't that hard to learn). This approach also allows us to re-use some of our custom tags in e-mail content.
然而,这在我们的环境中是一个有用的解决方案,我们有一群已经熟悉 JSP 语法和自定义标记的 Web 开发人员。很高兴能够让他们直接处理电子邮件内容,而无需学习另一种模板语言(尽管我确信 FreeMarker 可能并不难学)。这种方法还允许我们在电子邮件内容中重复使用我们的一些自定义标签。
回答by Sir RotN
In the enterprise sector many people haveuse tools like sonar or PMD to have a look on code complexity. I don't know if one of these supports any templating library. However, the level of complexity of templates I've reviewed in the wild was on a high niveau, I'd really like to prevent in my code. I tried to refactor some of them, and even though I made some success the results were never satisfying. Some days ago I refactored a 2kLoC template to 150 lines of template code and about 1.5kLoC logic in a new java class using Snippetory. Of course this left still some work to match out metrics, but now it's possible to achieve maintainable code. Snippetory templates provide a great separation of concerns by moving the data binding logic out of the template. As a bonus the template gets more re-useable. On the other hand the binding between logic and code requires some code, too. In the end te amount of code didn't change much, but readability increased dramatically.
在企业部门,许多人使用声纳或 PMD 等工具来查看代码复杂性。我不知道其中一个是否支持任何模板库。然而,我在野外的模板的复杂程度非常高,我真的很想在我的代码中防止。我试图重构其中的一些,尽管我取得了一些成功,但结果却从未令人满意。几天前,我使用Snippetory在一个新的 java 类中将一个 2kLoC 模板重构为 150 行模板代码和大约 1.5kLoC 逻辑. 当然,这仍然需要一些工作来匹配指标,但现在可以实现可维护的代码。Snippetory 模板通过将数据绑定逻辑移出模板提供了很好的关注点分离。作为奖励,模板变得更加可重用。另一方面,逻辑和代码之间的绑定也需要一些代码。最终,代码量没有太大变化,但可读性急剧增加。
For the enterprise business I'd recomment Snippetory.
对于企业业务,我建议使用 Snippetory。

