Seam now includes an optional components for templating and sending emails.
Email support is provided by jboss-seam-mail.jar. This JAR contains the mail JSF controls, which are used to construct emails, and the mailSession manager component.
The examples/mail project contains an example of the email support in action. It demonstrates proper packaging, and it contains a number of example that demonstrate the key features currently supported.
In Seam Mail, an email is just facelet:
<m:message xmlns="http://www.w3.org/1999/xhtml" xmlns:m="http://jboss.com/products/seam/mail" xmlns:h="http://java.sun.com/jsf/html"> <m:from name="Peter" address="peter@example.com" /> <m:to name="#{person.firstname} #{person.lastname}">#{person.address}</m:to> <m:subject>Try out Seam!</m:subject> <m:body> <p><h:outputText value="Dear #{person.firstname}" />,</p> <p>You can try out Seam by visiting <a href="http://labs.jboss.com/jbossseam">http://labs.jboss.com/jbossseam</a>.</p> <p>Regards,</p> <p>Peter</p> </m:body> </m:message>
The <m:message> tag wraps the whole message, and tells Seam to start rendering an email. Inside the <m:message> tag we use an <m:from> tag to set who the message is from, a <m:to> tag to specify a sender (notice how we use EL as we would in a normal facelet), and a <m:subject> tag.
The <m:body> tag wraps the body of the email. You can use regular HTML tags inside the body as well as JSF components.
So, now you have your email template, how do you go about sending it? Well, at the end of rendering the m:message the mailSession is called to send the email, so all you have to do is ask Seam to render the view:
@In(create=true) private Renderer renderer; public void send() { try { renderer.render("/simple.xhtml"); facesMessages.add("Email sent successfully"); } catch (Exception e) { facesMessages.add("Email sending failed: " + e.getMessage()); } }
If, for example, you entered an invalid email address, then an exception would be thrown, which is caught and then displayed to the user.
Whilst most mail readers nowadays support HTML, some don't, so you can add a plain text alternative to your email body:
<m:body> <f:facet name="alternative">Sorry, your email reader can't show our fancy email, please go to http://labs.jboss.com/jbossseam to explore Seam.</f:facet> </m:body>
Often you'll want to send an email to a group of recipients (for example your users). All of the recipient mail tags can be placed inside a <ui:repeat>:
<ui:repeat value="#{allUsers} var="user"> <m:to name="#{user.firstname} #{user.lastname}" address="#{user.emailAddress}" /> </ui:repeat>
The mail templating example shows that facelets templating 'just works' with the Seam mail tags.
Our template.xhtml contains:
<m:message> <m:from name="Seam" address="do-not-reply@jboss.com" /> <m:to name="#{person.firstname} #{person.lastname}">#{person.address}</m:to> <m:subject>#{subject}</m:subject> <m:body> <html> <body> <ui:insert name="body"> This is the default body, specified by the template.</ui:insert> </body> </html> </m:body> </m:message>
Our templating.xhtml contains:
<ui:param name="subject" value="Templating with Seam Mail" /> <ui:define name="body"> <p>This email demonstrates that you can easily use <i>facelets templating features</i> in your code!</p> </ui:define>
Sometimes you'll want to add other headers to your email. Seam provides support for some (see Section 16.3, “Tags”). For example, we can set the importance of the email, and ask for a read receipt:
<m:message xmlns:m="http://jboss.com/products/seam/mail" importance="low" requestReadReceipt="true">
Otherise you can add any header to the message using the <m:header> tag:
<m:header name="X-Sent-From" value="JBoss Seam" />
To include Email support in your application, include jboss-seam-mail.jar in your WEB-INF/lib directory. If you are using JBoss AS there is no further configuration needed to use Seam's email support. Otherwise you need to make sure you have the JavaMail API, an implementation of the JavaMail API present (the API and impl used in JBoss AS are distributed with seam as lib/mail.jar), and a copy of the Java Activation Framework (distributed with seam as lib/activation.jar.
The Seam Email module requires the use of Facelets as the view technology. Future versions of the library may also support the use of JSP. Additionally, it requires the use of the seam-ui package.
The mailSession component uses JavaMail to talk to a 'real' SMTP server.
A JavaMail Session may be available via a JNDI lookup if you are working in an JEE environment or you can use a Seam configured Session.
The mailSession component's properties are described in more detail in Section 21.8, “Mail-related components”.
The JBossAS deploy/mail-service.xml configures a JavaMail session binding into JNDI. The default service configuration will need altering for your network. http://wiki.jboss.org/wiki/Wiki.jsp?page=JavaMail describes the service in more detail.
<components xmlns="http://jboss.com/products/seam/components" xmlns:core="http://jboss.com/products/seam/core" xmlns:mail="http://jboss.com/products/seam/mail"> <mail:mailSession sessionJndiName="java:/Mail" /> </components>
Here we tell Seam to get the mail session bound to java:/Mail from JNDI.
A mail session can be configured via components.xml. Here we tell Seam to use smtp.example.com as the smtp server,
<components xmlns="http://jboss.com/products/seam/components" xmlns:core="http://jboss.com/products/seam/core" xmlns:mail="http://jboss.com/products/seam/mail"> <mail:mailSession host="smtp.example.com" /> </components>
Emails are generated using tags in the http://jboss.com/products/seam/mail namespace. Documents should always have the message tag at the root of the message. The message tag prepares Seam to generate an email.
The standard templating tags of facelets can be used as normal. Inside the body you can use any JSF tag which doesn't require access to external resources (stylesheets, javascript).
Root tag of a mail message
importance — low, normal or high. By default normal, this sets the importance of the mail message.
precedence — sets the precedence of the message (e.g. bulk).
requestReadReceipt — by default false, if set, a read receipt will be request will be added, with the read receipt being sent to the From: address.
Set's the From: address for the email. You can only have one of these per email.
name — the name the email should come from.
address — the email address the email should come from.
Set's the Reply-to: address for the email. You can only have one of these per email.
address — the email address the email should come from.
Add a recipient to the email. Use multiple <m:to> tags for multiple recipients. This tag can be safely placed inside a repeat tag such as <ui:repeat>.
name — the name of the recipient.
address — the email address of the recipient.
Add a cc recipient to the email. Use multiple <m:cc> tags for multiple ccs. This tag can be safely placed inside a repeat tag such as <ui:repeat>.
name — the name of the recipient.
address — the email address of the recipient.
Add a bcc recipient to the email. Use multiple <m:bcc> tags for multiple bccs. This tag can be safely placed inside a repeat tag such as <ui:repeat>.
name — the name of the recipient.
address — the email address of the recipient.
Add a header to the email (e.g. X-Sent-From: JBoss Seam
name — The name of the header to add (e.g. X-Sent-From).
value — The value of the header to add (e.g. JBoss Seam).
Set's the subject for the email.
Set's the body for the email. Supports an alternative facet which, if an HTML email is generated can contain alternative text for a mail reader which doesn't support html.
type — If set to plain then a plain text email will be generated otherwise an HTML email is generated.