Skip to main content

Sending Event Invitations With Seam

These days one of my colleagues had problems with sending event invitation using mail templates with seam (version 2.x). In basic this should not be a hard task, so I will in short explain what need to be done for sending event invitation using seam mail templates.
When you send mail invitation you need to send an email with attachment which contains information about particular event. I will create simple template and sender class which will be used for sending invitation.

Seam 2.x include additional components which are responsible for sending mails and creating templates. To use this features we need to include seam mail components in application, with maven we could do it like this:


The seam templating mechanism allow us to create mail template like we do it for standard jsp pages. It is easy and simple to learn, and you also can use standard jsp tags, JSF if you use it. In this example i will not go deeper in the usage of seam mail templating mechanism, Below you can find simple example of template used for sending invitation. 

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<m:message xmlns=""

      <m:header name="Content-Class" value="urn:content-classes:calendarmessage"/>
      <m:from name="Test Mail" address="no-reply-mail@invitation.example" />
      <m:to name="Igor Madjeric">#{eventInvitation.recipient}</m:to>

     <h:outputText value="Test invitation" />

      <m:attachment contentType="text/calendar;method=CANCEL" fileName="invitation.ics">
PRODID:-//Direct Scouts GmbH//INA//DE
SUMMARY;CHARSET=UTF-8:Test invitation

As you can see, it is not complicated, it is like making JSP page. When you creating invitation you need make attention on UID, its a unique identifier for event for which you create invitation, so if you later need to change something about that event you just need to use same UID. For this example I've created EventInvitation model class which contains data needed for event. They do not contains a lot of data but you can extend it if you need more.

package ba.codecentric.mail.sender.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

public class EventInvitation {

   SimpleDateFormat iCalendarDateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmm'00'");
   private String recipient;
   private String location;
   private String description;
   /* Start and stop dates */
   private Date start;
   private Date end;
   public String getRecipient() {
      return recipient;
   public void setRecipient(String recipient) {
      this.recipient = recipient;
   public String getLocation() {
      return location;
   public void setLocation(String location) {
      this.location = location;
   public String getDescription() {
      return description;
   public void setDescription(String description) {
      this.description = description;
   public String getStartAsString() {
      return iCalendarDateFormat.format(start);
   public String getEndAsString() {
      return iCalendarDateFormat.format(end);
   public Date getStart() {
      return start;
   public void setStart(Date start) {
      this.start = start;
   public Date getEnd() {
      return end;
   public void setEnd(Date end) {
      this.end = end;
   public String getCurrentDateAsString() {
      return iCalendarDateFormat.format(new Date());
   public String toString() {
       return "EventInvitation [recipient=" + recipient + ", location="+ location + ", description=" + description + ", start=" + start + ", end=" + end + "]";

It is simple seam component with page scope, leave as longs as page. From template you can see  that we used methods ..AsString for setting dates values. That is because, we cant simple use raw date for representing date in invitation, instead of that we format dates using next format "yyyyMMdd'T'HHmm'00'".

For filling dates I've used next simple form:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<ui:composition xmlns=""

<!-- main content -->
<ui:define name="MainContent">
<div class="WelcomeContent">
<rich:panel header="Welcom To Seam Mail Invitation Sender" style="width: 315px">
Start:<rich:calendar value="#{eventInvitation.start}"
datePattern="dd/M/yy hh:mm a"
<br />
End:<rich:calendar value="#{eventInvitation.end}"
datePattern="dd/M/yy hh:mm a"
<br />
Location:<h:inputText value="#{eventInvitation.location}" id="location"/>
<br />
Description:<h:inputText value="#{eventInvitation.description}" id="description"/>
<br />
Recipient:<h:inputText value="#{eventInvitation.recipient}" id="recipient"/>
<a4j:commandButton value="Send Invitation"
action="#{mailInvitationSender.sendInvitation}" reRender="info" />
<h:panelGroup id="info">
<h:outputText value="Status: #{mailInvitationSender.status} " rendered="#{not empty mailInvitationSender.status}" />

Nothing complicated just simple page for filling data. And at end we will take look in sender class.

package ba.codecentric.mail.sender.controller.impl;

import javax.ejb.Remove;
import javax.ejb.Stateful;

import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.faces.Renderer;
import org.jboss.seam.log.Log;

import ba.codecentric.mail.sender.controller.LocalMailInvitationSender;
import ba.codecentric.mail.sender.model.EventInvitation;

public class StandardMailInvitationSender implements LocalMailInvitationSender {

private static final String STATUS_SUCCESS = "SUCCESS";
private static final String STATUS_FAIL = "FAIL";

private static String INVITATION_TEMPLATE = "/invitation.xhtml";

private static Log LOG;

// Component used for rendering template.
@In(create = true)
private Renderer renderer;

private EventInvitation eventInvitation;

private String status;

public String getStatus() {
return status;

public void setStatus(String status) {
this.status = status;

public void sendInvitation() {"Send invitation method is called!");
try {
} catch (Exception e) {
status = STATUS_FAIL;
}"Invitation sending:" + status);

public void done() {
LOG.debug("Bean removed!");


This is simple class which use renderer for creating mail based on template. So there is nothing special. Of course you need to configure mail session in components.xml but that is simple configuration. You need to add next line in components.xml:

<mail:mail-session session-jndi-name="java:/Mail" />

And that's all. Your application is ready for sending invitations :).
Note: line above in components.xml will create mail session component which will be used by seam for sending mails. For example if you use JBoss 4.xx you may edit configuration in "mail-service.xml" file. 
But how to configure mail session is out of scope of this post, if you need more information about this topic you can check one of my older post Configure Seam Mail.

Popular posts from this blog

Checking file's "magic numbers"

Few days ago I had very interesting task. Our customer required that we perform checking of so called file's "magic numbers" to determinate does uploaded file correspond to it's extension. 
We are already allowed only to upload files with some predefined extensions (PDF, DOC ...). But this can not prevent some evil user to update an exe file after renaming it to PDF or DOC.
So first of all I will explain what are "magic numbers", and then I will show how we handle them.

Simple Workflow Engine With Spring

Few months ago, during working on one of the company project, we had need to developed  REST services which is used for sending an email depending on data sent by client application. During developing this service we decide to create simple workflow engine which will be charged for sending an email, but also this engine can be used for any kind of simple flows. In this article i will explain step by step how you can implement your simple workflow engine which can handle sequence flow.

Configure Seam Mail

From time to time we have need to send an e-mail from application. This task should not be so complicated, but if you do it for the first time you can get some troubles and it can take you some time. Because of that I will try to explain how you can configure it correctly.