JSF web applications can send email messages under program control
using the
JavaMail API available through the
javax.mail package.
We present a simple JSF page (and associated Java classes) that collects
information for a mail
message and sends it using a bean's
action method.
The initial page shown below is rendered by the
index.xhtml JSF
page shown in the
Code section.
The page's built-in validation insists that all form fields be non-empty:
If all form fields are non-empty, the
ClientBean's
send
method is invoked.
It calls the
MailService utility's
sendMessage method,
which throws an exception if the recipient address does not legally
parse:
If the address does parse then the message is sent.
Note that just because a message is successfully sent does not mean it
will reach its target.
In the example below, if
foo@bar.com is not a legal address, a
"bounced" message will be sent to the originating email account
specified by the private constants in
MailService.java.
This section shows the relevant JSF and Java files for this application.
- index.xhtml: collects information for a mail message and
invokes a bean's send method when button is clicked
- ClientBean.java: class that stores message information and
whose send method uses the MailService utility
- MailService.java: class using the JavaMail API and
which must be configured with valid mail user credentials