Over a million developers have joined DZone.
Platinum Partner

Send email with Seam 3 Mail and JMS

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Seam 3 Mail module provides a simple API to use Java Mail API to send email messages.

Basic Configuration

Assume you have already created a Maven-based Java EE 6 application.

Add seam mail dependency to your pom.xml file.


Add basic mail configuration in your META-INF/seam-beans.xml.

<mail:MailConfig serverHost="smtp.gmail.com"
    username="<your gmail account>"
    password="<your password>">
    <ee:modifies />

In your Java code, inject MailMessage and Session.

private transient Instance<MailMessage> mailMessage;

private transient Instance<Session> session;

MailMessage is provided by Seam3 module, it is a fluid API to build message object and send message. Session is from the standard Java Mail API.

MailMessage msg = mailMessage.get();
msg.subject("test subject")

Everything works well.

But if you try to send an email from jsf pages, the page will be blocked when the email is being sent.

EJB 3.1 provides a simple way to execute asynchronous action. You can simply create a @Stateless EJB and put the logic in a @Asynchronous method. But unfortunately when you try to use @Asynchronous and CDI together, it does not work.

JMS provides standard asynchronous processing capability for Java EE, Seam3 also includes a JMS module.

Send email asynchronously with JMS

Seam 3 also provides a JMS module which simplify JMS and CDI integration, we can utilize JMS to process the asynchronous work.

Configure the JMS connection factory in your META-INF/seam-beans.xml.

    <ee:modifies />

Create a standard JMS listener to handle JMS message.

public class MailProcessorMDB extends MessageListener {

    @EJB MailProcessor processor;

    public void send()...//

    protected void onMessage(Message _msg) throws JMSException {
        if (log.isDebugEnabled()) {
            log.debug("call handleMessage...");

        ObjectMessage objMessage = (ObjectMessage) _msg;
        EmailMessage mailMessage = (EmailMessage) objMessage.getObject();

MailProcessor is a @Stateless EJB which is use for sending mail.

When a message is arrived, the listener will call MailProcessor to process email.

Use an interface to observe the CDI event and route it to the JMS queue.

public void mapStatusToQueue(@Observes @NoneBlocking EmailMessage message,
        @JmsDestination(jndiName = "java:/queue/test") Queue q);

In the presentation layer, fire an event directly. The event object will be routed as the payload of JMS message automatically.


Run the project

I assume you have installed the latest Oracle JDK 7, JBoss AS 7.1.1.Final and Apache Maven 3.0.4.

  1. Check out the complete codes from github.com.

    git clone git://github.com/hantsy/seam3-mail-demo.git
  2. Start JBoss AS with standalone full profile which includes JMS support.

    <JBOSS_HOME>\bin\standalone.bat --server-config=standalone-full.xml
  3. Deploy the application into the running JBoss AS.

    mvn clean package jboss-as:deploy
  4. Open your browser and go to http://localhost:8080/seam3-mail-demo.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}