Mail Transport Sample Guide

Introduction

This sample demonstrates how you can use the Mail transport in WSO2 AppServer to invoke Web Services. Client sends the message to a mail box in a mail server and the service listens to that mail box and receives the message.

Configuration

Before deploying a service which uses the mail transport, you have to enable transports for receiving and sending messages in the CARBON_HOME/repository/conf/axis.xml file. By default, mail transport receiver and sender are disabled.

Mail transport Sender

                <transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
                    <parameter name="mail.smtp.from">synapse.demo.0@gmail.com</parameter>
                    <parameter name="mail.smtp.user">synapse.demo.0</parameter>
                    <parameter name="mail.smtp.password">mailpassword</parameter>
                    <parameter name="mail.smtp.host">smtp.gmail.com</parameter>

                    <parameter name="mail.smtp.port">587</parameter>
                    <parameter name="mail.smtp.starttls.enable">true</parameter>
                    <parameter name="mail.smtp.auth">true</parameter>
                </transportSender>
            

Mail transport Receiver

                <transportReceiver name="mailto" class="org.apache.axis2.transport.mail.MailTransportListener">

                </transportReceiver>
            

The services.xml of the service should contain the following parameters to receive messages through the mail transport.

                <parameter name="transport.mail.Address">synapse.demo.1@gmail.com</parameter>
                <parameter name="transport.mail.Protocol">pop3</parameter>
                <parameter name="transport.PollInterval">5</parameter>
                <parameter name="mail.pop3.host">pop.gmail.com</parameter>
                <parameter name="mail.pop3.port">995</parameter>
                <parameter name="mail.pop3.user">synapse.demo.1</parameter>
                <parameter name="mail.pop3.password">mailpassword</parameter>

                <parameter name="mail.pop3.socketFactory.class">javax.net.ssl.SSLSocketFactory</parameter>
                <parameter name="mail.pop3.socketFactory.fallback">false</parameter>
                <parameter name="mail.pop3.socketFactory.port">995</parameter>
                <parameter name="transport.mail.ContentType">text/xml</parameter>
            

You have to set the following parameters to your ServiceClient to receive messages, if your service has In-Out operations.

                serviceClient.getAxisService().addParameter("transport.mail.Address", "synapse.demo.0@gmail.com");
                serviceClient.getAxisService().addParameter("transport.mail.Protocol", "pop3");
                serviceClient.getAxisService().addParameter("transport.PollInterval", "5");
                serviceClient.getAxisService().addParameter("mail.pop3.host", "pop.gmail.com");
                serviceClient.getAxisService().addParameter("mail.pop3.user", "synapse.demo.0");
                serviceClient.getAxisService().addParameter("mail.pop3.password", "mailpassword");
                serviceClient.getAxisService().addParameter("mail.pop3.port", "995");

                serviceClient.getAxisService().addParameter("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                serviceClient.getAxisService().addParameter("mail.pop3.socketFactory.fallback", "false");
                serviceClient.getAxisService().addParameter("mail.pop3.socketFactory.port", "995");
                serviceClient.getAxisService().addParameter("transport.mail.ContentType", "text/xml");
            

Your client axis2.xml file also should contain the above menstioned transport receiver and sender as explained above.

NOTE: You can see how above configurations are used in the sample by going through the source code and configuration files provided under CARBON_HOME/samples/Mail.

Building the Service

  1. If the server is already running, shut down the server and open CARBON_HOME/repository/conf/axis2.xml and uncomment "mailto" transportReceiver and transportSender. This sample can be run with the default values set for the parameters.
  2. Use "ant" command in the CARBON_HOME/samples/Mail/ to build the service.
  3. This will create the Mail Test service (MailTestService.aar file) in the "services" directory and copy it to the CARBON_HOME/repository/deployment/server/axis2services directory. If you start AppServer, MailTestService will be available as a deployed service.

If you go the the Service Dashboard of the MailTestService, you can see the following mail transport endpoint under the "Endpoints" section.

                mailto:synapse.demo.1@gmail.com
            

The WSDL for the service should be viewable at:

                http://<host>:<port>/services/MailTestService?wsdl
            

You can find the mail transport endpoints in the WSDL as well.

Running the Client

To run the sample client for the service, go to samples/Mail directory and type:

sh run-client.sh

Check the server console and you'll see the message printed from the service on receipt of the message.