java 使用 bean 从 JSF 页面发送邮件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7787180/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:27:38  来源:igfitidea点击:

Send mail from JSF page using beans

javaemailjsfprimefaces

提问by Dimmy3

i am creating a web application using jsf and primefaces, and my question is how to send an email through site's contact form (i've done this using php before, very easily, but never using jsf). I have created form on contact.xhtml page, as well as bean class to support that, and all that form should do is to send bean's data to a predefined mail (ie. gmail). I have also found several "tutorials" of how to send email using JavaMail, but nothing seems to work properly. The form itself, consists of name, email and message fields.

我正在使用 jsf 和 primefaces 创建一个 Web 应用程序,我的问题是如何通过站点的联系表单发送电子邮件(我以前使用 php 很容易地完成了此操作,但从未使用过 jsf)。我已经在 contact.xhtml 页面上创建了表单,以及支持它的 bean 类,并且该表单应该做的就是将 bean 的数据发送到预定义的邮件(即 gmail)。我还找到了几个关于如何使用 JavaMail 发送电子邮件的“教程”,但似乎没有任何东西可以正常工作。表单本身由姓名、电子邮件和消息字段组成。

Can someone write how to do that, or give me a link. I would be very gratefull.

有人可以写下如何做到这一点,或者给我一个链接。我将不胜感激。

Do i need my site to be running on (online) server, or i can test it from localhost.

我是否需要我的网站在(在线)服务器上运行,或者我可以从本地主机测试它。

Thank you in advance.

先感谢您。

采纳答案by Bozho

In very short:

简而言之:

  • make a <h:commandButton action="#{yourBean.send}"
  • make a managed bean annotated with @ManagedBean("yourBean")that has a send(..)method
  • get commons-emailand read its short "User guide"; get a working smtp server (commons-email depends on JavaMail, so get that on the classpath as well)
  • in the send method use commons-email to send the email
  • 做一个 <h:commandButton action="#{yourBean.send}"
  • 使带注释托管bean@ManagedBean("yourBean")具有send(..)方法
  • 获取commons-email并阅读其简短的“用户指南”;获得一个可以工作的 smtp 服务器(commons-email 依赖于 JavaMail,所以也要在类路径上获取它)
  • 在发送方法中使用 commons-email 发送电子邮件

(You should go through a JSF tutorial to see how to gather the form parameters)

(您应该阅读 JSF 教程以了解如何收集表单参数)

Note that java is a bit more complex. "Send mail through JSF" isn't a particularly good question. It consists of two questions:

请注意,java 有点复杂。“通过 JSF 发送邮件”并不是一个特别好的问题。它由两个问题组成:

  • how to have a form submitted with JSF (every tutorial explains that)
  • how to send email in Java in general
  • 如何使用 JSF 提交表单(每个教程都解释了这一点)
  • 一般如何用 Java 发送电子邮件

回答by Bozho

It is all about how to capture JSF output. My approach is just to call the JSF page via JAX-RS, capture output and put it into e-mail. Be carefull to make the page as simplest as possible (JSF likes to add a lot of JS code) , and set absolute URLs to resources. Here is my code (simplified):

这完全是关于如何捕获 JSF 输出。我的方法只是通过 JAX-RS 调用 JSF 页面,捕获输出并将其放入电子邮件。注意页面尽量简单(JSF喜欢加很多JS代码),资源设置绝对URL。这是我的代码(简化):

@Resource(name = "mail/mySession")
Session mailSession;

//make request, capture output
Client client = ClientBuilder.newClient();
String htmlBody = 
        client.target("http://localhost:8080/AppName/jsfpage.xhtml")
        .queryParam("id", 10) //we can add some params
        .request()
        .get(String.class);

//send email
Message message = new MimeMessage(mailSession);
message.setFrom();
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]"));
message.setHeader("X-Mailer", "JavaMail");
message.setSubject("Subject here");
message.setContent(htmlBody, "text/html; charset=utf-8"); //set captured html as content
message.setSentDate(new Date());
Transport.send(message);

回答by Ilya Sidorovich

On my practice SEAM Mail really helps in sending mail.

在我的实践中,SEAM Mail 确实有助于发送邮件。

Here you can find a good tutorial on it:

在这里你可以找到一个很好的教程:

Sending mail from JSF - Seam mail

从 JSF 发送邮件 - Seam 邮件