简体   繁体   English

使用bean从JSF页面发送邮件

[英]Send mail from JSF page using beans

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). 我正在使用jsf和primefaces创建一个Web应用程序,我的问题是如何通过网站的联系表单发送电子邮件(我以前使用php完成此操作,很容易,但从不使用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). 我在contact.xhtml页面上创建了表单,以及支持该表单的bean类,所有表单都应该将bean的数据发送到预定义的邮件(即gmail)。 I have also found several "tutorials" of how to send email using JavaMail, but nothing seems to work properly. 我还发现了几个关于如何使用JavaMail发送电子邮件的“教程”,但似乎没有任何工作正常。 The form itself, consists of name, email and message fields. 表单本身由名称,电子邮件和消息字段组成。

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. 我是否需要在(在线)服务器上运行我的站点,或者我可以从localhost进行测试。

Thank you in advance. 先感谢您。

In very short: 很简短:

  • make a <h:commandButton action="#{yourBean.send}" 制作一个<h:commandButton action="#{yourBean.send}"
  • make a managed bean annotated with @ManagedBean("yourBean") that has a send(..) method 使用带有send(..)方法的@ManagedBean("yourBean")注释托管bean
  • get commons-email and read its short "User guide"; 得到commons-email并阅读其简短的“用户指南”; get a working smtp server (commons-email depends on JavaMail, so get that on the classpath as well) 得到一个工作的smtp服务器(commons-email依赖于JavaMail,所以在类路径上也是如此)
  • in the send method use commons-email to send the email 在send方法中使用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. 请注意,java有点复杂。 "Send mail through JSF" isn't a particularly good question. “通过JSF发送邮件”并不是一个特别好的问题。 It consists of two questions: 它包含两个问题:

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

It is all about how to capture JSF output. 这是关于如何捕获JSF输出的全部内容。 My approach is just to call the JSF page via JAX-RS, capture output and put it into e-mail. 我的方法是通过JAX-RS调用JSF页面,捕获输出并将其放入电子邮件中。 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. 小心使页面尽可能简单(JSF喜欢添加大量JS代码),并将绝对URL设置为资源。 Here is my code (simplified): 这是我的代码(简化):

@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("asd@asd.pl"));
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);

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邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM