Friday, June 17, 2016

How to send email using WSO2 ESB

SCENARIO:
Imagine a scenario you need to send email through WSO2 ESB where you are using the mail transport.
HOW TO:
Following steps will guide you to get your scenario up and running.
  1. Download a distribution of the WSO2 ESB [1].
    ie. wso2esb-4.8.1.zip
  2. Extract the zip file. The folder created (wso2esb-4.8.1) will be referred as ESB_HOME.
  3. Uncomment the mail TransportSender and TransportReceiver from axis2.xml.  ie. ESB_HOME/conf/axis2.xml.
    [code language="css"]<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>[/code]
    [code language="css"]<transportReceiver name="mailto" class="org.apache.axis2.transport.mail.MailTransportListener">
    </transportReceiver>[/code]
  4. Add the following line to axis2.xml's MessageFormatters.
    [code language="css"]<messageFormatter contentType="text/plain" class="org.apache.axis2.format.PlainTextFormatter"/>[/code]
  5. Go to the ESB_HOME/bin and run the script which starts wso2server.
    eg. wso2server.bat for windows environments
    wso2server.sh for linux environments
  6. Then the server will be started. You can access the management console using the following URL [2].
  7. Log into the Mangement Console using following credentials.
    username: admin
    password: admin
  8. Create a proxy service using the following proxy configuration.
    [code language="css"]<!-- Using the mail transport -->
    <definitions xmlns="http://ws.apache.org/ns/synapse">
    <proxy xmlns="http://ws.apache.org/ns/synapse"
     name="MailToTransportSender"
     transports="https,http"
     statistics="disable"
     trace="disable"
     startOnLoad="true">
      <target>
        <inSequence>
          <property name="Subject" value="Email Subject"
           scope="transport"/>
          <property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
          <property name="ContentType" value="text/plain" scope="axis2"/>
          <property name="messageType" value="text/plain" scope="axis2"/>
        <payloadFactory media-type="xml">
         <format>
          <ns:text xmlns:ns="http://ws.apache.org/commons/ns/payload">$1</ns:text>
         </format>
         <args>
           <arg value="Hello WSO2.....!"/>
         </args>
        </payloadFactory>
        <send>
          <endpoint>
             <address uri="mailto:test.automation.dummy@gmail.com"/>
          </endpoint>
        </send>
        <header name="To" action="remove"/>
          <property name="NO_ENTITY_BODY" scope="axis2" action="remove"/>
          <property name="RESPONSE" value="true" scope="default" type="STRING"/>
        <send/>
       </inSequence>
       <outSequence>
         <send/>
       </outSequence>
       </target>
       <description/>
      </proxy>
    </definitions>[/code]
  9. Create soup request to url[2] using soapui.
  10. Send a request to ESB using soapui.
    [code language="css"]<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
        <soapenv:Header/>
        <soapenv:Body>
        </soapenv:Body>
     </soapenv:Envelope>[/code]
References
[1] - http://wso2.org/downloads/esb
[2] - http://localhost:8280/services/MailToTransportSender?wsdl

No comments:

Post a Comment