SeamFramework.orgCommunity Documentation

Seam Mail


1. Seam Mail Introduction
1.1. Getting Started
2. Configuration
2.1. Minimal Configuration
3. Core Usage
3.1. Intro
3.2. Contacts
3.2.1. String Based
3.2.2. InternetAddress
3.2.3. EmailContact
3.2.4. Content
3.2.5. Attachments
4. Templating
4.1. Basics
4.2. Velocity
4.3. FreeMarker
5. Advanced Features
5.1. MailTransporter
5.2. MailConfig
5.3. EmailMessage

Seam mail is an portable CDI extension designed to make working with Java Mail easier via standard methods or plugable templating engines.

By default the configuration parameters for Seam Mail are handled via configuration read from your application's seam-beans.xml. This file is then parsed by Seam Solder to configure the MailConfig class. You can override this and provide your own configuration outside of Seam Mail but we will get into that later.

First lets add the relevant maven configuration to your pom.xml



 <dependency>
  <groupId>org.jboss.seam.mail</groupId>
  <artifactId>seam-mail-impl</artifactId>
  <version>${seam.mail.version}</version>
</dependency>

Now now that is out of the way lets provide JavaMail with the details of your SMTP server so that it can connect and send your mail on it's way.

This configuration is handled via Seam Solder which reads in the configuration from your application's seam-beans.xml and configures the MailConfig class prior to injection.



<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:s="urn:java:ee"
       xmlns:mail="urn:java:org.jboss.seam.mail.core"
       xsi:schemaLocation="
      http://java.sun.com/xml/ns/javaee
      http://docs.jboss.org/cdi/beans_1_0.xsd">

    <mail:MailConfig
            serverHost="my-server.test.com"
            serverPort="25">
        <s:modifies/>
    </mail:MailConfig>
    
</beans>

That is all the configuration necessary to send a simple email message. Next we will take a look at how to configure and use the supported templating engines.

Important

JBoss AS 7.0.x does not correctly load all the modules to support sending mail AS7-1375. This is easily fixed By replacing the module definition at $JBOSS_HOME/modules/javax/activation/api/main/module.xml with the following



<module xmlns="urn:jboss:module:1.0" name="javax.activation.api">
    <dependencies>
        <module name="javax.api" />
        <module name="javax.mail.api" >
            <imports><include path="META-INF"/></imports>
        </module>   
    </dependencies>        
        
     <resources>
        <resource-root path="activation-1.1.1.jar"/>
            
        <!-- Insert resources here -->
    </resources>
</module>

This will be fixed in AS 7.1.x

At it's base an email consists of various destinations and content. Seam Mail provides a wide varerity of methods of ways to configure the following address fields

MailTransporter is an interface that allows you to control how the message is handled when the send() method is called. The default implementation simply sends the message with a javax.mail.Session. The main drawback of this is that the thread is blocked until your configured email server accepts your messages. This can take milliseconds or minutes depending on the size of your message load on your SMTP server. While this may be fine for most simple cases, larger applications may wish to employ a queuing function so that messages appear to send instantly and do not block application execution.

Overiding the default MailTransporter is simple



      MailTransporter myTransporter = new MailQueueTransporter();
      MailMessage msg = mailMessage.get();
      msg.setMailTransporter(myTransporter);
      

A simple implementation might convert the message into a MimeMessage for sending and then fire as a CDI event so that a CDI @Observer can queue and send the message at a later time.

The MailTransporter might look something like this.



public class MailQueueTransporter implements Serializable, MailTransporter {
  private static final long serialVersionUID = 1L;
  @Inject
  @QueuedEmail
  private Event<MimeMessage> queueEvent;
  @Inject
  @ExtensionManaged
  private Instance<Session> session;
  @Override
  public EmailMessage send(EmailMessage emailMessage) {
      MimeMessage msg = MailUtility.createMimeMessage(emailMessage, session.get());
      
      queueEvent.fire(msg); 
      
      emailMessage.setMessageId(null);
      
      try {
          emailMessage.setLastMessageId(msg.getMessageID());
      }
      catch (MessagingException e) {
          throw new SendFailedException("Send Failed ", e);
      }
      return emailMessage;
  }
}