Wednesday, February 17, 2016

How to use InputEmailAdapter with WSO2 BAM 2.5.0 to Get Email Body

Email event adapter is an internal event adaper that comes with WSO2 products by default. It can be configured with  XML, text, and JSON input mapping types. You can use this adapter to retrieve email events. Refer to WSO2 BAM documentation[1] for more details regarding the adapter.

Let see how to configure the InputEmailAdapter and get a email body through that.


  1. Extract thhe WSO2 BAM 2.5.0 pack.
  2. Enable the mail transport receiver in Axis2 level by uncommenting the following line in <PRODUCT_HOME>/repository/conf/axi2/axis2.xml file 
  3.  <transportReceiver name="mailto" class="org.apache.axis2.transport.mail.MailTransportListener"/>  
    
  4. Since we are going to handle plain text messages you need to add the following message builder to axis2.xml file MessageBuilders section
  5.  <messageBuilder contentType="text/plain" class="org.apache.axis2.format.PlainTextBuilder"/>  
    

  6. Then you can follows the steps upto "Configuring input email event adapter" mentioned in the doc[1] to configure the email adapter.
  7. Then go to Event Streams section Management Console -> Main -> Event Streams
  8. Click Add Event Stream. Fill the information similar to below image
     
  9. When you select Add, it will asked to select Event Builder for the newly created stream.
  10. Select Custom Event Builder from that
  11. It will show a window to configure the Builder. Put some name for the builder and select adapter that you already configured in step 2 from the Input Event Adaptor Name list. Put some name to email service name. Npte that When you set the email service name, the email subject should be SOAPAction:urn:<email_service_name>. Select text as the input mapping type and click add event builder
  12. Then go to the Event Streams window again and you will see your newly created stream
  13. Click In-Flows in the newly created stream
  14. There you will see your email event builder is added to the in flow of the stream
  15. Click Edit button of that builder
  16. By default you will see custom mapping is disabled as below
     <mapping customMapping="disable" type="wso2event"/>  
    
  17. Change that as follows
     <mapping customMapping="enable" type="text">  
         <property>  
           <from regex="<tag>(.+?)</tag>"/><!--I'm going to take the message between tags in my email body-->  
           <to name="email_body" type="string"/><!--here name is the event attribute that we put when creating the stream-->  
         </property>  
       </mapping>  
    


  18. Click update.
  19. Now send an email with the subject as SOAPAction:urn:<email_service_name> to the email address that you configured with the body as <tag>This is my email Body</tag>
  20. If everything is successfully configured you should be able to see the email body through the Cassandra Explorer as below

You can also refer to [2] and [3] for more details regarding the email adapter.
[1] - https://docs.wso2.com/display/BAM250/Input+Email+Event+Adapter
[2] - http://wso2.com/library/articles/2013/08/writing-custom-event-adaptors-for-cep-3.0.0/
[3] - https://docs.wso2.com/pages/viewpage.action?pageId=34605846

1 comment:

  1. good article, i can't imagine why need to go through so much trouble just to access a body of an email. Development languages and frameworks support to be efficient and make developers productive. Anything other than

    import parserLib;
    let text = email;

    let body = parserLib.getBody(text);

    is overkill IMHO :)

    ReplyDelete