Tuesday, June 7, 2016

Sending mails with attachments via WSO2 ESB

1) Enable mail transport in axis2.xml

Navigate to <ESB_HOME>/repository/conf and enable following in axis2.xml


<transportReceiver name="mailto" class="org.apache.axis2.transport.mail.MailTransportListener">
        <!-- configure any optional POP3/IMAP properties
        check com.sun.mail.pop3 and com.sun.mail.imap package documentation for more details-->
  </transportReceiver>

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

2) Create following proxy 


<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
       name="MTOMService"
       transports="http"
       statistics="disable"
       trace="disable"
       startOnLoad="true">
   <target>
      <inSequence>
         <log level="custom">
            <property name="sequence" value="sendMailSequence"/>
         </log>
         <property name="Subject" value="sac" scope="transport"/>
         <property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
         <log level="full"/>
         <send>
            <endpoint name="FileEpr">
               <address uri="mailto:mymail@gmail.com" optimize="mtom"/>
            </endpoint>
         </send>
      </inSequence>
      <outSequence>
         <send/>
      </outSequence>
   </target>
   <publishWSDL key="gov:/trunk/services/wsdl/mywsdl"/>
   <description/>
</proxy>

3) Go to soap UI and set Enable MTOM to true which is in left side of UI.

4) Attach the file you want  to send and set the cid:filename in your request as below


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mtom="http://org.wso2/sample/mtom">
   <soapenv:Header/>
   <soapenv:Body>
      <mtom:getBinaryData>
         <!--Optional:-->
         <mtom:dataHandler>cid:sac.pdf</mtom:dataHandler>
      </mtom:getBinaryData>
   </soapenv:Body>
</soapenv:Envelope>    

            
       
5) Once you invoke the proxy you will be able to see the attached file (sac.pdf) in recipient side.

6)  In here you will get the message body as "Web Service Message Attached" in receiving mail. This is the default message body sent by ESB. So if you want to add a customized body add the following property to the proxy.


<property name="transport.mail.bodyWhenAttached"
                   value="This text will appear in body" 
                   scope="axis2"/> 

Then you will get "This text will appear in body" message as the body of receiving email.

2 comments:

  1. The email I recieve contains an attachment Untitled. without an extension although everything was done as stated. WSO2 EI 6.5.0

    ReplyDelete

Blogger Widgets