How to send an Email using WSO2 ESB


This post explains how to send an email in WSO2 ESB using Payload Factory mediator . You can find plenty of examples which do the same by using Script mediator.

This example is tested in WSO2 ESB 4.8

1.Uncomment and configure the SMTP server information ESB_HOME/repository/conf/axis/axis2.xml file as 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">esbadmin</parameter>
   <parameter name="mail.smtp.password">*****</parameter>
   <parameter name="mail.smtp.from">esbadmin@gmail.com</parameter>
</transportSender>


2. Start the ESB and add the following proxy.

\
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="EmailSender"
transports="https,http"
statistics="disable"
trace="disable"
startOnLoad="true">
   <target>
   <inSequence>
   <log/>
   <property name="messageType" value="text/html" scope="axis2" type="STRING"/>
   <property name="ContentType" value="text/html" scope="axis2"/>
   <property name="Subject" value="Testing ESB" scope="transport"/>
   <property name="OUT_ONLY" value="true"/>
   <property name="FORCE_SC_ACCEPTED" value="true" 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:client@gmail.com"/>
      </endpoint>
   </send>
   </inSequence>
   <outSequence/>
   </target>
   <description/>
</proxy>

3. When you send a request to this proxy service, client@gmail.com should receive a mail from esbadmin@gmail.com

Advertisements