Write to Database

In our previous Read Email post, we have successfully accepted an email into mule. In this post, we will see how to store email related data in Database.

1. After IMAP inbound connector put an expression transformer to return all the attachments received in the mail.

2. Drag a collection splitter into the flow

The collection splitter accepts a collection of objects and splits the list invoking the next message processor with each item in the collection in sequence.

 

3. Store from address in a variable.

settings

Note that I am using string utils to get only the email address from message.inboundProperties[‘fromAddress’]

4. Let’s now include a scatter-gather into the flow

The routing message processor Scatter-Gather sends a request message to multiple targets concurrently. It collects the responses from all routes and aggregates them into a single message.

In our case, we would need to store the incoming email in a database table and in the file system. In this post first, we will see how to store email information in a database table. In next post, we will see how to store email attachments in the file system…

5. Now, drag a database component and configure it.
Click on the database connector. You will see its properties below near the console tab

In General –>Basic Settings, click on the plus(+) icon next to connector configuration.In the new pop-up select Oracle Configuration and click ok. Add your database details. Test the connection and click ok


Note that you will need ojdbc6.jar to connect to Oracle Database

Select insert operation and enter insert query as shown below.


We now have successfully configured Database Connector for mule to record the email subject and senders address into our database table.

Add a logger after Database connector. Add another logger in Scatter-gather branch to run the code without errors. In next post, we will use this branch to store attachments of email into file

Testing:

  • Right click on the project. Select Run As –> Mule Application
  • You will see logs running in the console and finally the application will be deployed.
  • Send an email and check your console. You will see that your flow is being kicked off mail
  • see the console

    Check the database, you will see an entry made to the database table as shown below.

Complete code can be seen below or downloaded from
https://bitbucket.org/integralzone/blog/src/60bfef1f3a3d/email_to_db_fs/