Home » Java » Enterprise Java » Sending Event Invitations With Seam

About Igor Madjeric

Igor Madjeric

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<m:message xmlns="http://www.w3.org/1999/xhtml"

      <m:header name="Content-Class" value="urn:content-classes:calendarmessage"/>
      <m:from name="Test Mail" address="[email protected]" />
      <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
ORGANIZER:MAILTO:[email protected]

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"

<!-- 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() {
LOG.info("Send invitation method is called!");
try {
} catch (Exception e) {
status = STATUS_FAIL;
LOG.info("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.

Reference: Sending Event Invitations With Seam from our JCG partner Igor Madjeric at the Igor Madjeric blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter


One comment

  1. Be aware that ICS specifies rn line endings, so the above example might not work with strictly conforming clients unless the editor is configured for Windows-style EOLs.


    i can recommend ical4j (http://ical4j.sourceforge.net) for generating and parsing ical data. It takes some getting used to but it has full support for ICS (meaning “it supported everything we needed it to do in the last project i used it in”).

Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns