Monday, June 23, 2014

WSO2 ESB : Mail sending with HTML complex tag supports


You can use WSO2 ESB to send mails and receive mails with polling using Mail Transport facility as explained in https://docs.wso2.org/display/ESB481/MailTo+Transport


Sending Simple HTML email from ESB:

Sending a simple email body with following html content can be done easily using following builder and formatter in axis2 configuration for given content-type (text/html).

axis2.xml changes:

 <messageBuilder contentType="text/html"   
             class="org.apache.axis2.builder.ApplicationXMLBuilder"/>  
 <messageFormatter contentType="text/html"   
              class="org.apache.axis2.transport.http.ApplicationXMLFormatter"/>   

Sample HTML mail:

 <html>   
      <head>   
       <meta http-equiv="content-type" content="text/html" />   
      </head>   
      <body>   
           <h2 style='color:green'>Testing Simple mail</h2><p>Simple mail details..</p>   
      </body>   
 </html>  



However if we include special html tags to the email content like 
 <DOCTYPE> , 
<!-- Comment --> 
<?xml - xml declaration tags for rich content validating and rendering purpose , you cannot use above mentioned XML builder and formatter and you will get errors while passing the mail message through ESB as below.

 ERROR {org.apache.axis2.transport.mail.MailTransportListener} - Failed to process message {org.apache.axis2.transport.mail.MailTransportListener}   
 org.apache.axiom.om.OMException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,122]   
 Message: DOCTYPE is not allowed   
     at org.apache.axiom.om.impl.builder.StAXOMBuilder.next(StAXOMBuilder.java:296)   
     at org.apache.axiom.om.impl.llom.OMDocumentImpl.getOMDocumentElement(OMDocumentImpl.java:109)   
     at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:570)   
     at org.apache.axiom.om.impl.builder.StAXOMBuilder.getDocumentElement(StAXOMBuilder.java:566)   
     at org.apache.axis2.builder.ApplicationXMLBuilder.processDocument(ApplicationXMLBuilder.java:81)   
     at org.apache.axis2.transport.TransportUtils.createDocumentElement(TransportUtils.java:180)   
     at org.apache.axis2.transport.TransportUtils.createSOAPMessage(TransportUtils.java:146)   
     at org.apache.axis2.transport.mail.MailTransportListener.processMail(MailTransportListener.java:501)   
     at org.apache.axis2.transport.mail.MailTransportListener.access$000(MailTransportListener.java:61)   
     at org.apache.axis2.transport.mail.MailTransportListener$MailProcessor.run(MailTransportListener.java:331)   
     at org.apache.axis2.transport.mail.MailTransportListener.processMail(MailTransportListener.java:295)   
     at org.apache.axis2.transport.mail.MailTransportListener.checkMail(MailTransportListener.java:199)   
     at org.apache.axis2.transport.mail.MailTransportListener.poll(MailTransportListener.java:80)   
     at org.apache.axis2.transport.mail.MailTransportListener.poll(MailTransportListener.java:61)   
     at org.apache.axis2.transport.base.AbstractPollingTransportListener$1$1.run(AbstractPollingTransportListener.java:67)   
     at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)   
     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)   
     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)   
     at java.lang.Thread.run(Thread.java:722)   


How to overcome this issue :


You can use BinaryRelayBuilder and ExpandingMessageFormatter to overcome this error and send text/html content-type mails through ESB as explained in below steps.

Step1: Configure mailto transport in ESB

- Download and install the ESB latest version ( http://wso2.com/products/enterprise-service-bus/ )

- Uncomment the mail TransportSender and TransportReceiver from the <ESB_HOME>/repository/conf/axis2/axis2.xml
-Configure with your mail server credentials as required.


 <transportSender 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">synapse.demo.0</parameter>  
    <parameter name="mail.smtp.password">mailpassword</parameter>  
    <parameter name="mail.smtp.from">synapse.demo.0@gmail.com</parameter>  
 </transportSender>  


 <transportReceiver name="mailto">  
 </transportReceiver>  



Step 2: Add builder and formatter for content-type “text/html”

 <messageBuilder contentType="text/html"   
             class="org.wso2.carbon.relay.BinaryRelayBuilder"/>  
 <messageFormatter contentType="text/html"   
             class="org.wso2.carbon.relay.ExpandingMessageFormatter"/>  


Step 3: Add the mail sending proxy in your ESB.

Please note that you need to force axis2 mail transport sender to use content-type based formatter specially formatting the mail message with with special html tags mentioned previously.

 <property name="FORCE_CONTENT_TYPE_BASED_FORMATTER" value="true" scope="axis2"/>  

Note: Please note, you need to have latest version of ESB or the patch https://wso2.org/jira/browse/CARBON-14796 applied to ESB 4.8.1 version to effect this property

If you have not specified the above parameter with value “true” , ESB will select default formatter related to the BinaryRelayBuilder which is org.apache.axis2.format.BinaryFormatter as default to handle binary content regardless you had specified the formatter in axis2 configuration file.


1:  <?xml version="1.0" encoding="UTF-8"?>   
2:  <proxy xmlns="http://ws.apache.org/ns/synapse"   
3:      name="JKMailTestProxy"   
4:      transports="https http"   
5:      startOnLoad="true"   
6:      trace="disable">   
7:    <description/>   
8:    <target>   
9:     <inSequence>   
10:       <property name="Subject" value="Testing rich text mail" scope="transport"/>   
11:       <property name="FORCE_CONTENT_TYPE_BASED_FORMATTER"   
12:            value="true"   
13:            scope="axis2"/>   
14:       <property name="OUT_ONLY" value="true"/>   
15:       <property name="FORCE_SC_ACCEPTED" value="true" scope="axis2"/>   
16:       <send>   
17:        <endpoint >   
18:          <address uri="mailto:synapse.demo.1@gmail.com"/>   
19:        </endpoint>   
20:       </send>   
21:       <drop/>   
22:     </inSequence>   
23:    </target>   
24:  </proxy>  



Step 4: Send mail with content including rich text and verify

Lets call proxy using SoapUI tool or curl commands.

Content-type :text/html

Content in  SOAP Body: 

 <?xml version="1.0" encoding="UTF-8"?>   
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
 <!-- Testing mail with rich content -->  
 <html>   
      <head>   
       <meta http-equiv="content-type" content="text/html" />   
      </head>   
      <body>   
           <h2 style='color:green'>Testing Simple mail</h2><p>Simple mail details..</p>   
      </body>   
 </html>  



The address  synapse.demo.1@gmail.com   will receive a mail from  synapse.demo.0@gmail.com






No comments:

Post a Comment