Pluggable mail sessions in Seam apps using JBoss7 mail module

If you are writing a web application using the Seam framework and that application is required to send mails, you usually need to configure your seam application to use a mail session. In seam, you specify mail sessions for your app in the components.xml file which is usually found in the WEB-INF/ folder of the application.


<?xml version="1.0" encoding="UTF-8"?>
<components xmlns="http://jboss.com/products/seam/components"
            xmlns:core="http://jboss.com/products/seam/core"
            xmlns:mail="http://jboss.com/products/seam/mail"
            xsi:schemaLocation=
                "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                 http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                 http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">

        .....

	<mail:mail-session />
        
        ......

</components>

The seam mail session tag in components.xml has several properties which have to be specified if you do not intend to use the default mail session. One of these properties is “session-jndi-name” which is used to reference a mail session using its JNDI name.

In JBoss7 it is very easy to specify multiple mail sessions and then instruct your seam app to use one of those sessions for sending mail. This means that your web application delegates it’s mail functionality to the server container and we have one less thing to worry about.

The point for configuration of the mail session in JBoss7 is the configuration file which is found in {JBOSS_HOME}/standalone/configuration/{mode}.xml where {mode} corresponds to the JBoss7 startup mode for which you intend to deploy your application. That is, either standalone or standalone-ha (standalone clustered mode).

Open the file you wish to use and find the mail subsystem.

In the default jboss7 distribution, the mail subsystem looks something like the following:



<subsystem xmlns="urn:jboss:domain:mail:1.0">
     <mail-session jndi-name="java:jboss/mail/Default">
         <smtp-server outbound-socket-binding-ref="mail-smtp"/>
     </mail-session>
</subsystem>

This is the default mail session, with a JNDI name of java:jboss/mail/Default. This session connects to an SMTP server and the outbound-socket-binding reference is called mail-smtp. This reference is defined within the socket-binding-group element which is also within the configuration file.


<socket-binding-group name="standard-sockets" default-interface="public" port-offset="${jboss.socket.binding.port-offset:0}">
        <socket-binding name="management-native" interface="management" port="${jboss.management.native.port:9999}"/>
        <socket-binding name="management-http" interface="management" port="${jboss.management.http.port:9990}"/>
        <socket-binding name="management-https" interface="management" port="${jboss.management.https.port:9443}"/>
        <socket-binding name="ajp" port="8009"/>
        <socket-binding name="http" port="8080"/>
        <socket-binding name="https" port="8443"/>
        <socket-binding name="jgroups-diagnostics" port="0" multicast-address="224.0.75.75" multicast-port="7500"/>
        <socket-binding name="jgroups-mping" port="0" multicast-address="${jboss.default.multicast.address:230.0.0.4}" multicast-port="45700"/>
        <socket-binding name="jgroups-tcp" port="7600"/>
        <socket-binding name="jgroups-tcp-fd" port="57600"/>
        <socket-binding name="jgroups-udp" port="55200" multicast-address="${jboss.default.multicast.address:230.0.0.4}" multicast-port="45688"/>
        <socket-binding name="jgroups-udp-fd" port="54200"/>
        <socket-binding name="modcluster" port="0" multicast-address="224.0.1.105" multicast-port="23364"/>
        <socket-binding name="osgi-http" interface="management" port="8090"/>
        <socket-binding name="remoting" port="4447"/>
        <socket-binding name="txn-recovery-environment" port="4712"/>
        <socket-binding name="txn-status-manager" port="4713"/>
        <outbound-socket-binding name="mail-smtp">
            <remote-destination host="localhost" port="25"/>
        </outbound-socket-binding>
</socket-binding-group>

The mail-smtp socket binding is defined to connect to an smtp server on localhost and port 25. Now unless you have an smtp server on your local development machine, you will most definitely want to change this.

So how exactly do we create new mail sessions and their corresponding socket bindings in the configuration?

In my configuration, I will be creating a mail session with a socket binding to the gmail email service and then my seam app will use this session for all its email sending.

Just add a new mail-session element to the mail subsystem config (right under the default mail session) as shown below:


<mail-session jndi-name="java:jboss/mail/google/myapp" from="do-not-reply@myapp.com">
         <smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp">
             <login name="gmail-account-username" password="gmail-account-password"/>
         </smtp-server>
</mail-session>

The snippet above means that we would like to create a new mail-session with jndi name, java:jboss/mail/google/myapp. This session will be connecting to an SMTP server using ssl and the socket binding to use is called mail-smtp. The login credentials for using the smtp server are specified in the login child element of the smtp-server declaration.

Now we need to configure the socket binding to point to the gmail servers:


<outbound-socket-binding name="mail-smtp">
     <remote-destination host="smtp.gmail.com" port="465"/>
</outbound-socket-binding>

The snippet above means that the socket binding called mail-smtp connects to the gmail smtp server, smtp.gmail.com on port 465 (which is the SSL port for the gmail smtp service).

And thats all!!

Now you can simply just reference the jndi name of the configured gmail mail-session in your seam components.xml as shown below:


<components xmlns="http://jboss.com/products/seam/components"
            xmlns:core="http://jboss.com/products/seam/core"
            xmlns:mail="http://jboss.com/products/seam/mail"
            xsi:schemaLocation=
                "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.2.xsd
                 http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.2.xsd
                 http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.2.xsd">

        .....

	<mail:mail-session session-jndi-name="java:jboss/mail/google/myapp" />
        
        ......

</components>

Now your seam app sends mail using a configured gmail mail session which is managed by your JBoss7 runtime. You can create as many mail-sessions as you want and also as many socket bindings as you want without having to change your components.xml. Happy mailing!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s