Using Mail transport to Invoke Web Services
This sample demonstrates how you can use the Mail transport in WSO2 Application Server to invoke Web Services. You can find the sample discussed here in <AS_HOME>/samples/Mail
directory.
In this sample, a client sends a message to a mailbox in a mail server and the service listens to that mailbox and receives the message. Before deploying a service which uses the Mail transport, you have to enable transports for receiving and sending messages using <AS_HOME>/repository/conf/axis2/axis.xml
file. By default, the Mail transport receiver and sender are disabled.
Configuration
Mail transport Sender
<transportSender name="mailto"> <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"> </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 should also contain the transport receiver and sender as explained above.
You can see how above configurations are used in the sample by going through the source code and the configuration files provided in <AS_HOME>/samples/Mail
directory.
Building the sample
The sample is located in  <AS_HOME>\Samples\Mail
 directory.
- If the WSO2 Application Server is running, shutdown the server. Open
<AS_HOME>/repository/conf/axis2/axis2.xml
and uncomment "mailto" transportReceiver and transportSender. This sample can be run with the default values set for the parameters. - For more information, see Building Axis2 Samples .
This creates the Mail Test service (MailTestService.aar
) in the<AS_HOME>/repository/deployment/server/axis2services
directory. - On the Main menu, under Services, click List. The Deployed Services  page  will appear with the  MailTestService service under  deployed services .
- Click on the service name in the deployed services page.Â
This will open up the dashboard of the service MailTestService. Refer to the service's mail transport endpoint under the Endpoints section of the dashboard.
It should be as follows:ÂYou can find the mail transport endpoints in the service's WSDL file as well. You can view the WSDL for the service at:Â
http://<host>:<port>/services/MailTestService?wsdl
Running the client
From the terminal go toÂ
<AS_HOME>\samples\MailÂ
directory.Execute one of the following commands to run the client:
OS Command MS Windows run-client.bat Unix/Linux run-client.sh - Check the server console.
You will see the message printed, from the service on the message receiver. - Log in the to the ‘synapse.demo.1@gmail.com’ by using the password  ‘mailpassword’, and verify that a mail has been received that has the subject "SOAPAction: urn:ping"