Monday, May 30, 2011

Automatically save an Email attachment to a Database

This article explains how the UltraESB is used to connect an email inbox and  a database.
The sample which is described below supports the ESB to get  an email attachment and it can be saved to a table separately in the database.
The UltraESB supports polling both pop3 and IMAP email boxes . In this example we will poll the GMail account, "sunethcha@gmail.com" every 10 seconds and get attachment separately and send it's include data to database by UltraESB.  
We can get idea about this scenario by using image below,

Email attachment to database

To solve this scenario, first we need to create a database

Creating a database is easy
In this example we use the derby database which ships with the JDK, to host our table. Following link will provide you a guide to set up a sample database with Derby
in this example I have created database, named "ATTACHDB". Before creating it you need to run the network server and ./ij  (as mentioned in the above link). Afterwards the database is created and connected, by executing the following commands. 
connect 'jdbc:derby://localhost:1527/database;create=true;user=admin;password=admin';

Create Database
Create table
Tables are created by executing following commands

CREATE TABLE ADMIN.ATTACHMENT ("CODENAME" varchar(20),"CODE" varchar(500));

Create table
sample - 504
This sample have three special beans ,


        
            
                
                
                
                
                
                    
                
            
        
    


This first bean is used to listen to mails, second and third beans  are used to connect with the database.


Properties of the transport declaration is as follows.


                    
                
            
            
            
                     
        

The "mainPartIdentificationPriority" is an important property of the above declaration.


The below Java code fragment  is use to separate and save data to the database



Run sample - 504


Add the sample xml file to the "ultraesb-1.4.0/samples/conf" directory. Finally run the ESB with the sample code, by running the following command.

Start sample with UltraESB
The file.txt is sent as a mail attachment and the content of the file is written to the database.

soap.txt