Read Email

In this tutorial, we will deal with our first use case of Email Responder series. As we mentioned in our introduction, our first use case is “Reading email and processing details to Database and File system”. For ease of understanding, we will break this use case in three parts as 1.Read Email, 2.Processing email related information to Database and 3.Processing email contents to file system. In this post, Let us learn how to read Emails using IMAP inbound connector.

 

Pre-requisites:

Before we start the tutorial, we would need the below pre-requisites to be in place.

1. Create a test Gmail account and enable IMAP. Please follow the instructions below.

  • Log into Gmail.
  • Click on the gear icon gear-icon  in the upper right, then select Settings.
  • Click on the Forwarding and POP/IMAP tab.
  • Under IMAP Access, select the radio button for Enable IMAP and click on Save Changes.

2. Have a database in place (up and running). I am using Oracle database.

  • Create a new table EMAILREQUEST having 3 columns as shown below
    table
  • I am using sql developer to query the database like below

3. Make sure you have Anypoint Studio installed. If this is the first project you are creating then ensure that you have prepared the development environment.
Please check the following link: https://docs.mulesoft.com/anypoint-connector-devkit/v/3.8/setting-up-your-dev-environment

Steps to follow:

1. Create a new mule project and store your  IMAP details for Gmail in mule-app.properties file

2. Configure IMAP connector using the properties file data you have given in step1

3. Add Logger to see the payload

Now, we have the “Pre-requisites” and “Steps to solve” our project are ready, go ahead and solve each step in detail way.

1. Create a new mule project and store your  IMAP details in properties file

Go to File–>New–>Mule Project

Mule flows are present under the src/main/app folder

Now, we need to configure IMAP inbound connector. Enter the IMAP details for GMAIL into the mule-app.properties file. Below is the content of properties file.

Make sure to enter your test email id and password (you created in the pre-requisites step) for IMAP.USERNAME and IMAP.PASSWORD properties respectively.

2. Configure IMAP connector using the properties file data you have given in step1

Open the emailresponder.xml. From the Mule Palette drag the IMAP connector into emailresponder.xml. Follow below steps to configure the IMAP inbound endpoint.

  • In the General tab, Basic settings, observe that we are getting the value for Host, Port, Username and Password from the mule-app.properties file (Refer Figure 1: General Settings). Click on the Security tab and enable IMAPS. (Refer Figure 2: Security tab )
  • Click on the ‘+’ icon beside connector configuration. Enter your mailbox folder (Refer Figure 3: Connector Configuration – General). Go to Security tab and enable IMAPS. (Refer Figure 4: Connector Configuration – Security).

    Figure1: General Settings

    Figure 2: Security Tab

    Figure 3: Connector Configuration – General

    Figure 4: Connector Configuration – Security

3. Drag a logger into your flow

By default, the logger logs the payload so that we can view the email details in the console(Shown in next step). Your emailresponder.xml should look as shown in the below figure

Testing:

  • To run the code. Right click on the project. Select Run As and choose Mule Application.

  • You will see the console is being filled with messages and finally you will see that your application has started.
  • Now it’s time to test our application. I have another personal Gmail account from which I would be sending an email to testintegralzone@gmail.com (a Gmail account you created for this exercise. The same is entered in mule-app.properties file)

    Click on send. Now your code should read this email and log the data in the console. Let’s wait for a second and check our console. If you hate waiting, set this property and your mail will be read almost instantly.

    Yaay!!! You see that the email is picked by Mule. See below. Check your console, scroll up to see other mail related details.

Now, we have our email related details in our payload. In next post, we will see how to process these details into Database…