Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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). 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.

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.

like image 360
Dimmy3 Avatar asked Oct 16 '11 20:10

Dimmy3


1 Answers

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-email and 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

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

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

  • how to have a form submitted with JSF (every tutorial explains that)
  • how to send email in Java in general
like image 199
Bozho Avatar answered Oct 25 '22 10:10

Bozho