Thing which seemed very Thingish inside you is quite different when it gets out into the open and has other people looking at it

Friday, August 24, 2012

How to send mails using WSO2 ESB (sending your payload data to e-mail)

Suppose you have a web service which returns some data and you want to send that data into a mail... WSO2 ESB provides an easy mechanism to do this by creating a proxy service.

First get your endpoint (web service endpoint which returns data ).

Step 1 - Enable mail transport in axis2.xml.

Go to your axis2.xml (under WSO2ESB_HOME/repository/conf/axis2/), and uncomment mailto transportSender as shown below.

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

Step 2 Creating the proxy Service.

Go WSO2 ESB, start up the server. Under Main menu -> Axis2 Services -> Add -> Proxy Service. Click on Custom Proxy Service. Give an appropriate Proxy Service Name. (Give the WSDL URL as your requirement)

Step 3 - Define Endpoint

In the define endpoint section click on define inline, click on create -> Address Endpoint and give the address end point of your service which you get data from. I am going to invoke the Data Service which i created in my previous blog. And my Address endpoint will be



Step 4 - Creating the Out Sequence. Go to Out sequence -> Define Inline

In the In Out sequance We need to add a
A Log Mediator - Which logs the incoming message
Three Property Mediators
    1. Subject - Which will be the subject of our mail
    2. MessageType - Message Type of our mail
    3. ContentType - Content Type of our mail
    4. Send mediator - Send the mail to our email address

To add the log mediator, Click on add child -> Core -> Log.  And in the log mediator give the Log level as full.

To add the three properties,  Click on add child -> Core -> Property. And add three properties one by one.
property name="Subject" value="CEP Event" scope="transport" type="STRING"
property name="MessageType" value="text/html" scope="axis2" type="STRING"
property name="ContentType" value="text/html" scope="axis2"  type="STRING"

To add the send mediator Click on add child -> Core -> Send. And Select Endpoint Type -> Define inline -> Address Endpoint -> mailto:amani.soysa@gmail.com

Once you add the three properties and the log mediator your out sequance editor should look like below.



Click save and close and finish creating your proxy Service.


<proxy xmlns="http://ws.apache.org/ns/synapse" name="MyMailProzy" transports="https,http" statistics="disable" trace="disable" startOnLoad="true">
   <target>
      <inSequence />
      <outSequence>
         <log level="full" />
         <property name="Subject" value="CEP Event" scope="transport" />
         <property name="MessageType" value="text/html" scope="axis2" type="STRING" />
         <property name="ContentType" value="text/html" scope="axis2" />
         <property name="OUT_ONLY" value="true" scope="default" type="STRING" />
         <send>
            <endpoint>
               <address uri="mailto:amani.soysa@gmail.com" />
            </endpoint>
         </send>
      </outSequence>
      <endpoint>
         <address uri="http://localhost:9765/services/PersonsDataService" />
      </endpoint>
   </target>
   <publishWSDL uri="http://localhost:9765/services/PersonsDataService?wsdl" />
   <description></description>
</proxy>



You can invoke your proxy service by going to try it in the service list, and you should get a mail according to your payload. My next blog post will show how we can receive emails to WSO2 ESB using a standard email client.






1 comment:

  1. Hi Sparkle,
    It's good article, help me much.

    But, when I does with the step, I have not get the payload in e-mail.

    ReplyDelete