Mailing service is one of the most inportant service provided by the application servers, which allows us to simply lookup the mail service JNDI and then get access to the mailing subsystem to send mails. JBoss AS7 provides a very easy way to configure and use the mail-service. Unlike in JBoss AS6 here we need not to separately deploy mail-service.xml file in the $PROFILE/deploy directory in order to configure the mail-service on JBoss.

Now we need to just add few lines of tags inside the domain.xml or standalone-full.xml file in order to use the mailing service of jboss. We will quickly see what we were supposed to do till JBoss AS6 in order to create the mailing service.

JBoss AS6 mail session configuration
In JBoss AS6 we used to create *-service.xml file like mail-service.xml file like following and used to deploy this file inside the “$PROFILE/deploy” directory.

<?xml version="1.0" encoding="UTF-8"?>
  <mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">
    <attribute name="JNDIName">java:/TestMail</attribute>
    <attribute name="User">someUsername</attribute>
    <attribute name="Password">somePassword</attribute>
    <attribute name="Configuration">
      <!-- A test configuration -->
        <!-- Change to your mail server prototocol -->
        <property name="mail.store.protocol" value="pop3"/>
        <property name="mail.transport.protocol" value="smtp"/>

        <!-- Change to the user who will receive mail  -->
        <property name="mail.user" value="userone"/>

        <!-- Change to the mail server  -->
        <property name="mail.pop3.host" value="mail.middlewaremagic.com"/>

        <!-- Change to the SMTP gateway server -->
        <property name="mail.smtp.host" value="smtp.middlewaremagic.com"/>

        <!-- The mail server port -->
        <property name="mail.smtp.port" value="25"/>
        <property name="mail.smtp.auth" value="true"/>

        <!-- Change to the address mail will be from  -->
        <property name="mail.from" value="testuser@middlewaremagic.com"/>

        <!-- Enable debugging output from the javamail classes -->
        <property name="mail.debug" value="true"/>

JBoss AS7 Mail Session.

In JBoss AS7 we need to edit the domain.xml or standalone-full.xml file by editing the subsystem “urn:jboss:domain:mail:1.0” inside the “jboss-as-7.1.0.Beta1/standalone/configuration/standalone-full.xml” or “jboss-as-7.1.0.Beta1/domain/configuration/domain.xml” file as following:

Here we are creating a mail-session with JNDI name “java:/MiddlewareMagicMail”

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

Also in the same above file at the bottom “standard-sockets” part add the “mail-smtp-middleware” declaration as following

    <socket-binding-group name="standard-sockets" default-interface="public">
        <socket-binding name="http" port="8080"/>
        <socket-binding name="https" port="8443"/>
        <socket-binding name="jacorb" port="3528"/>
        <socket-binding name="jacorb-ssl" port="3529"/>
        <socket-binding name="jmx-connector-registry" interface="management" port="1090"/>
        <socket-binding name="jmx-connector-server" interface="management" port="1091"/>
        <socket-binding name="management-native" interface="management" port="9999"/>
        <socket-binding name="management-http" interface="management" port="9990"/>
        <socket-binding name="messaging" port="5445"/>
        <socket-binding name="messaging-throughput" port="5455"/>
        <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 name="mail-smtp-middleware">
            <remote-destination host="smtp.middlewaremagic.com" port="1234"/>

Step1). Once the above configuration is added in the above file then we can start the JBoss AS7 as following:

cd /home/userone/jboss-as-7.1.0.Beta1/bin
./standalone.sh --server-configuration=standalone-full.xml


cd /home/userone/jboss-as-7.1.0.Beta1/bin

Step2). Now to test whether our mail-serivce configuration is working fine or not we can write a simple web application to send some mails. So we will create an application indide our file system. Create a directory with some where as “/home/userone/MailSession_Demo”

Step3). Now place the following kind of servlet inside the “/home/userone/MailSession_Demo/src” directory with name “MailSenderServlet.java”

package servlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;

import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.annotation.Resource;

public class MailSenderServlet extends HttpServlet
     private Session mailSession;

     public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
	PrintWriter out=res.getWriter();
	try    {
		MimeMessage m = new MimeMessage(mailSession);
		Address from = new InternetAddress("senderUser@middlewaremagic.com");
		Address[] to = new InternetAddress[] {new InternetAddress("recieverUser@middlewaremagic.com") };
		m.setRecipients(Message.RecipientType.TO, to);
		m.setSubject("JavaMail Test - 2 ");
		m.setSentDate(new java.util.Date());
		m.setContent("Test from inside JBoss AS7 Server","text/plain");
		out.println("<font color=green> Mail Sent Successfully.</font>");
	catch (javax.mail.MessagingException e) 
		out.println("<font color=red>Error in Sending Mail: "+e+"</font>");

Step4). Now in order to build and deploy the web application we will write an Ant build script as following inside “/home/userone/MailSession_Demo/” with name “build.xml”

<project name="MailServiceDemo" default="all">
<property name="jboss.home" value="/home/userone/jboss-as-7.1.0.Beta1" />
<property name="jboss.module.dir" value="${jboss.home}/modules" />
<property name="basedir" value="." />
<property name="tmp.dir" value="tmp" />
<property name="src.dir" value="src" />
<property name="output.dir" value="build" />
<property name="war.name" value="MailSessionWebApp.war" />

   <path id="jboss.classpath">
     <fileset dir="${jboss.module.dir}">
        <include name="**/*.jar"/>

        <target name="init">
           <delete dir="${output.dir}" />
           <mkdir dir="${output.dir}" />
           <delete dir="${tmp.dir}" />
           <mkdir dir="${tmp.dir}/WEB-INF/classes" />

        <target name="all" depends="deploy" />

        <target name="build_war" depends="init">
           <javac srcdir="${src.dir}" destdir="${tmp.dir}/WEB-INF/classes"  includes="MailSenderServlet.java" classpathref="jboss.classpath">
               <compilerarg value="-Djava.endorsed.dirs=${jboss.module.dir}/javax/annotation/api/main"/>
           <jar jarfile="${output.dir}/${war.name}" basedir="${tmp.dir}" compress="true" />
           <delete dir="${tmp.dir}"/>

        <target name="deploy" depends="build_war">
            <echo message="*******************  Deploying the WAR file ${war.name} *********************" />
            <echo message="********** ${output.dir}/${war.name} to ${jboss.home}/standalone/deployments **********" />
            <copy todir="${jboss.home}/standalone/deployments/">
                <fileset dir="${output.dir}/">
                  <include name="${war.name}"/>
            <echo message="*******************  Deployed Successfully   *********************" />


Step5). Open a shell prompt and Make sure that ANT bin directory and JDK bin directories are present in your PATH variable as following, then run the ant script “build.xml”:

export PATH=/home/userone/jdk1.6.0_21/bin:/home/userone/apache-ant-1.7.1/bin:$M2_HOME/bin:$PATH

Buildfile: build.xml

   [delete] Deleting directory /home/userone/MailSession_Demo/build
    [mkdir] Created dir: /home/userone/MailSession_Demo/build
    [mkdir] Created dir: /home/userone/MailSession_Demo/tmp/WEB-INF/classes

    [javac] Compiling 1 source file to /home/userone/MailSession_Demo/tmp/WEB-INF/classes
      [jar] Building jar: /home/userone/MailSession_Demo/build/MailSessionWebApp.war
   [delete] Deleting directory /home/userone/MailSession_Demo/tmp

     [echo] *******************  Deploying the WAR file MailSessionWebApp.war *********************
     [echo] ********** build/MailSessionWebApp.war to /home/userone/jboss-as-7.1.0.Beta1/standalone/deployments **********
     [copy] Copying 1 file to /home/userone/jboss-as-7.1.0.Beta1/standalone/deployments
     [echo] *******************  Deployed Successfully   *********************


Total time: 6 seconds

Step6). Now open a web browser and then access the application as following:

MiddlewareMagic Team

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.