Mail transport

Mail Transport Configuration

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

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, the mail transport sender sends emails to a mail server to 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 directs any message that comes in to a particular address into the Axis engine. The engine will process the message and use the Transport sender to send the reply.

The mail transports have been written with the use of Sun's JavaMail and Activation jars. They should be available in your classpath to get the mail transport to 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 .

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, the mail transport sender can be activated by adding the following entry to the server axis2.xml file.(Or look for and uncomment and modify the following in the default axis2.xml)

   <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.host">hostname</parameter>
        <parameter name="mail.smtp.user">username</parameter>
        <parameter name="transport.mail.smtp.password">password</parameter>
   </transportSender>
  
and
<transportReceiver name="mailto" class="org.apache.axis2.transport.mail.SimpleMailListener">
        <parameter name="mail.pop3.host">mailhost</parameter>
        <parameter name="mail.pop3.user">username</parameter>
        <parameter name="mail.store.protocol">pop3</parameter>
        <parameter name="transport.mail.pop3.password">password</parameter>
        <parameter name="transport.mail.replyToAddress">server_emailaddress</parameter>
        <parameter name="transport.listener.interval">3000</parameter>
    </transportReceiver>
and the following to the client axis2.xml
   <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
        <parameter name="mail.smtp.host">hostname</parameter>
        <parameter name="mail.smtp.user">username</parameter>
        <parameter name="transport.mail.smtp.password">password</parameter>
        <parameter name="mail.smtp.from">client_email_address</parameter>
   </transportSender>
  
and
<transportReceiver name="mailto" class="org.apache.axis2.transport.mail.SimpleMailListener">
        <parameter name="mail.pop3.host">mailhost</parameter>
        <parameter name="mail.pop3.user">username</parameter>
        <parameter name="mail.store.protocol">pop3</parameter>
        <parameter name="transport.mail.pop3.password">password</parameter>
        <parameter name="transport.mail.replyToAddress">client_emailaddress</parameter>
        <parameter name="transport.listener.interval">3000</parameter>
    </transportReceiver>

At runtime, tuning a client to set the mail transport is as easy as follows:

Set the end point reference to use mail transport. Say for a service named SampleService with a server email address of test@mail.com
private static EndpointReference targetEPR = 
             new EndpointReference(
               "mailto:test@mail.com?x-service-path=/axis2/services/SampleService");
and
...

 ConfigurationContext ct = ConfigurationContextFactory.createConfigurationContextFromFileSystem(
                repository_path, axis2.xml_path);
 ServiceClient sender = new ServiceClient(ct, null);
 Options options = new Options();
 options.setTo(targetEPR);
 options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);        
 options.setProperty(AddressingConstants.WS_ADDRESSING_VERSION, AddressingConstants.Final.WSA_NAMESPACE);
 options.setAction("urn:SampleService");
 sender.setOptions(options);                
 sender.engageModule("addressing");

...
Also add the parameter urn:SampleService inside the operation element of the services.xml file of the service, assuming the service name is SampleService.

Thus, a user who is familiar with setting up an SSL connection2, could easily do it with a Properties object. For example, tuning the sender to talk to the gmail account. This configuration could also be done with <parameter/> in axis2.xml as shown in the Axis2 Mail Transport document.

. Configure the options object as above and,
...
Properties props = 
                  new Properties();
        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.put("transport.mail.smtp.password", "password");
		
		//then set the Properties object in the Option object.
		options.set(org.apache.axis2.transport.mail.Constants.MAILTO, props);

...

Please note that according to the mail transport specification, only SOAP 1.2 is supported.

Transport Receiver

For a non-SSL connection, as an example, the mail Listener can be activated by adding the following entry to the server axis2.xml file.(or look for and uncomment the section)

   <transportReceiver name="mailto" class="org.apache.axis2.transport.mail.SimpleMailListener">
        <parameter name="mail.pop3.host">hostname</parameter>
        <parameter name="mail.pop3.user">username</parameter>
	<parameter name="mail.store.protocol">pop3</parameter>
        <parameter name="transport.mail.pop3.password">password</parameter>
        <parameter name="transport.mail.replyToAddress">email_address</parameter>
	<parameter name="transport.listener.interval">3000</parameter>
  </transportReceiver>
  

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

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

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

Using Mail Transport on the Server Side

If the Mail Listener is to be started as a standalone mail listener, it can be done with the 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 on 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 to be configured.

        
        OMElement payload = ....
        String targetEPR = "mailto:user@mailhost.com?x-service-path=/axis2/services/Foo";

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

        ServiceClient servicClient = new ServiceClient(configurationContext, null);

        Options options = new Options();
        options.setTo(targetEPR);
        options.setTransportInProtocol(Constants.TRANSPORT_MAIL);
	options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);        
	options.setProperty(AddressingConstants.WS_ADDRESSING_VERSION, AddressingConstants.Final.WSA_NAMESPACE);
	options.setAction("urn:SampleService");
        servicClient.setOptions(options);		
		sender.engageModule("addressing");
		
        servicClient.fireAndForget(payload);

For further reading, please refer to Axis2 Mail Transport document.

Configuring James as an SMTP and POP Server

Download Apache James and start James. Connect to the James server via Telnet as administrator James using 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 command 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 the memory against the recipient till the recipient pops it from the server. To facilitate the use in Linux environments as a non root user, the POP and SMTP ports used by default configuration/test cases are, 1024, 25 and 1024, 110 respectively.