使用 JSP 和 Spring 3.0 发送电子邮件

发布于 2024-10-06 09:23:10 字数 556 浏览 0 评论 0原文

是否可以使用JSP设计一封电子邮件,通过Spring 3.0的JavaMailSender机制发送? JavaMailSender 可能与这个问题关系不大,但其想法是利用 JSP 的 国际化机制,其变量/循环 JSTL 代码并允许设计师可以轻松地将完成的 html 电子邮件交给我。

此外,我可以制作一个导入此 JSP 的网页,以便我可以轻松地在每封电子邮件中提供一个链接,以便能够在 Web 上查看该电子邮件,以防客户无法正确看到 html。

更新:下面的 2 个答案建议使用 Velocity 发送电子邮件而不是 JSP,所以这似乎是明智的选择。然而,对 stackoverflow 的一些快速扫描表明 FreeMarker 可能更适合我的需求。我特别希望能够有一个 JSP 页面来反映电子邮件中发送的内容。我必须将代码从 JSP 转换为 Velocity 或 FreeMarker,但 FreeMarker 给我带来的优势是我可以重用大量 JSP 代码,特别是 taglib。这有道理吗?

Is it possible to design an email using JSP to be sent through Spring 3.0's JavaMailSender mechanism? JavaMailSender probably has little to do with this question, but the idea is to take advantage of JSP's <fmt:message key="phrasetitle"/> internationalization mechanism, its variable/loop JSTL code and allow designers to easily hand me completed html emails.

Additionally, I could make a webpage that imports this JSP so that I can easily provide a link in each email to be able to view that email on the web in case their client doesn't see the html properly.

UPDATE: The 2 answers below suggest using Velocity for email instead of JSP, so it seems like that's the smart choice. However, just some quick scanning of stackoverflow suggests FreeMarker might be better suited for my needs. In particular I want to be able to have a JSP page which mirrors what's sent out in email. I'll have to translate the code from JSP to Velocity or FreeMarker, but the advantage FreeMarker gives me is I can reuse a lot of the JSP code, particularly taglibs. Does this make sense?

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

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

发布评论

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

评论(3

泪冰清 2024-10-13 09:23:10

JSP 不是一种模板技术。例如,Velocity 是一个更好的选择。

如果您确实坚持使用 JSP,那么您必须通过 java.net.URL

InputStream input = new URL("http://localhost:8080/context/mailtemplate.jsp").openStream();

JSP is not a templating technology. For that for example Velocity is a better choice.

If you really insist in using JSP, then you have got to execute it locally by java.net.URL.

InputStream input = new URL("http://localhost:8080/context/mailtemplate.jsp").openStream();
玩世 2024-10-13 09:23:10

有一些 Spring 文档使用 Velocity 可以做你想做的事。我用它在 Spring 中处理电子邮件,效果很好。

我通常做的是拥有一个 EmailService,我从控制器调用它,控制器的工作是负责发送电子邮件。其中的方法接受命令对象,该对象在速度模板中访问并呈现为字符串(电子邮件正文)。然后您可以使用 MailSender 等发送邮件。

There's some Spring documentation about using Velocity which will do what you want. I use it to handle emails in Spring and it works great.

What I typically do is to have an EmailService which which I call from a Controller whose job is to take care of sending emails. Methods within that accept a Command Object, which is accessed in the velocity template and rendered as a String (the email body). You can then use MailSender etc to send the mail.

眉目亦如画i 2024-10-13 09:23:10

尽管我同意 Velocity 或 Freemarker 可能是更好的选择,但如果您确实需要呈现 JSP 并将输出捕获为字符串(您可以将其传递给 JavaMailer),请查看我的关于 捕获 JSP 输出

它使用Spring 3,还允许使用 fmt:message、fmt:formatNumber 和 spring:message。这个想法基本上是使用 RequestDispatcher.include 方法将页面呈现为 MockHttpServletRequest。

Although I agree that Velocity or Freemarker may be a better choice, if you really need to render a JSP and capture the output as a String (which you could pass to a JavaMailer), check out my tutorial on Capturing JSP Output

It uses Spring 3, and also allows use of fmt:message, fmt:formatNumber, and spring:message. The idea is basically to use the RequestDispatcher.include method to render the page into a MockHttpServletRequest.

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