Mail Transport Configuration

This document provides the guidelines on how to configure Axis2 in order to get the mail transport working.

Send your feedback or questions to: axis-dev@ws.apache.org. Prefix subject with [Axis2]. To subscribe to mailing list see here.

Content

Introduction

The inner workings of the mail transport has been divided into two parts, the transport sender for SMTP and the transport listener for POP3. The transport listener will listen to a particular email address periodically. When an email comes in it will be tunneled into an Axis2 engine. On the other hand, mail transport sender sends emails to a mail server for a particular email address.

Mail transport can be used against a generic mail server or it can be used like a mailet. The simple mailet provided with Axis2 will direct any message that is coming to a particular address into the Axis engine. The engine will process the message and will use the Transport sender to send the reply.

The mail transports have been written with the use of Sun's JavaMail and Activation jars. These should be available in your classpath to get the mail transport work.

Transport Sender

You need to have a mail account to activate the mail functionality. This can either be a generic mail server or you can start up a James mail server, which will be available here.

JavaMail sets its properties to a Properties object. In Axis2, this has been mapped to a Parameter object. Mapping has been done as follows,

For a non-SSL connection, as an example,mail transport sender can be activated by adding following entry to the axis2.xml file.

   <transportSender name="mail" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.host" locked="false">localhost</parameter>
        <parameter name="mail.smtp.user" locked="false">mary</parameter>
        <parameter name="transport.mail.smtp.password" locked="false">mary</parameter>     
   </transportSender>
  

In runtime tuning a client to set mail transport is as easy as follows,

...

Options options =  new Options();
HttpTransportProperties.MailProperties mailProps 
                    = new HttpTransportProperties.MailProperties();
mailProps.addProperty("mail.smtp.host","localhost");
mailProps.addProperty("mail.smtp.user","mary");
mailProps.setPassword("mary");

options.setProperty(HTTPConstants.MAIL_SMTP,mailProps);

...
Thus, a user familiar with setting up a SSL connection, should easily do it with the MailProperties object. For Eg.: tuning the sender to talk to gmail account. This configuration should also be done with <parameter/> in axis2.xml.
...
HttpTransportProperties.MailProperties props = 
                  new HttpTransportProperties.MailProperties();
        props.put("mail.smtp.user", "address@gmail.com");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.starttls.enable","true");
        props.put("mail.smtp.auth", "true");
        //props.put("mail.smtp.debug", "true"); // if the user wants
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.socketFactory.fallback", "false");
        props.setPassword("password");

...

Transport Receiver

For a non-SSL connection, as an example, mail Listener can be activated by adding the following entry to the axis2.xml file.

   <transportReceiver name="mail" class="org.apache.axis2.transport.mail.SimpleMailListener">
        <parameter name="mail.pop3.host" locked="false">localhost</parameter>
        <parameter name="mail.pop3.user" locked="false">bob</parameter>
        <parameter name="transport.mail.pop3.password" locked="false">bob</parameter>
        <parameter name="transport.mail.replyToAddress" locked="false">bob@localhost</parameter>
  </transportReceiver>
  

Note: The @name="transport.mail.replyToAddress" is an important parameter. It supply the Endpoint reference to the listener.

For an advanced user, this can be set to a SSL connection. As an example, lets use this transport listener to pop from a specified gmail account.

<transportReceiver name="mail" class="org.apache.axis2.transport.mail.SimpleMailListener">
        <parameter name="mail.pop3.host" locked="false">pop.gmail.com</parameter>
        <parameter name="mail.pop3.user" locked="false">address@gmail.com</parameter>
        <parameter name="mail.pop3.socketFactory.class" locked="false">javax.net.ssl.SSLSocketFactory</parameter>
        <parameter name="mail.pop3.socketFactory.fallback" locked="false">false</parameter>
        <parameter name="mail.pop3.port" locked="false">995</parameter>
        <parameter name="mail.pop3.socketFactory.port" locked="false">995</parameter>
        <parameter name="transport.mail.pop3.password" locked="false">password</parameter>
        <parameter name="transport.mail.replyToAddress" locked="false">address@gmail.com</parameter>
</transportReceiver>

Using Mail Transport in the Server Side

If the Mail Listener is need to be started as a standalone mail listener, it can be done with following command with the all the Axis2 jars and the mail dependency jars in the classpath.

java org.apache.axis2.transport.mail.SimpleMailListener repository-directory

Using Mail Transport in the Client Side

The following code segment shows how to send a one-way (IN-Only MEP) SOAP message using the mail transport, this needs the Transport Sender configured.

        
        OMElement payload = ....
        String targetEPR = "mail:axis2@localhost/axis2/services/Foo";

        ConfigurationContext configurationContext = ConfigurationContextFactory.createConfigurationContextFromFileSystem(repo,
                axis2XML);

        ServiceClient servicClient = new ServiceClient(configurationContext, null);

        Options options = new Options();
        options.setTo(targetEPR);
        options.setTransportInProtocol(Constants.TRANSPORT_MAIL);

        servicClient.setOptions(options);

        servicClient.sendRobust(payload);

Configure James as SMTP and POP Server

Download the Apache James and start James, connect to the James via Telnet for administrator James with the following code:

$telnet 127.0.0.1 4555
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
JAMES Remote Administration Tool 2.2.0
Please enter your login and password
Login id:
root
Password:
root
Welcome root. HELP for a list of commands

Add users to James

adduser axis2-server axis2
User axis2-server added
adduser axis2-client axis2
User axis2-client added
Connection closed by foreign host.

Now James is up and running with the accounts.

Using the Included Mail Server

The inbuilt mail server can be started from the command line using the following piece of code when all the necessary jars are in the class path.

java org.apache.axis2.transport.mail.server.MailServer

The server itself does not need any configuration or tinkering to work. A ConfigurationContext and the ports to operate on are the only details needed. The server will store the mails in memory against the recipient till the recipient pops it from the server. To facilitate the use in *nix environments as a non root user the POP and SMTP ports used by default config/test cases are (1024 + 25) and (1024 + 110).