Currency Conversion Service

This use case polls an email, gets the ‘from’ and ‘to’ currency for conversion from the subject and calls yahoo finance API to get conversion rates in CSV format. This CSV can contain multiple records which are sent back to the caller as HTML. It also records the Action and Response into the database and writes the response to a file

Steps to follow:

  1. Read emails via IMAP and transform the data into required API input format
  2. Exceptional Handling
  3. Call Yahoo finance API, parse the response, generate HTML response and send it to caller
  4. Process Email details to  File system and Database

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

Please refer link Read Email post and see pre-requisites section on the top.

After sorting out pre-requisites. Proceed for further steps.

1. Read emails via IMAP and transform the data into required API input format

1.Create new project

2. open src/main/app and click on currencycoversion.xml

As we mentioned above, we should reply to the caller with the response coming from Yahoo finance API. Let’s create a sub flow for the same in advance so that we could use it in other flows. Add a logger into the flow to indicate we have reached this sub flow and add SMTP connector.

3. In path src/main/app) enter the IMAP details and SMTP details needed for Gmail.

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.

If you want to know more about configuring properties, you can refer Configuring Properties documentation of mulesoft.

4. Configure SMTP connector. Click on SMTP connector of our canvas, In General tab add your details directly or you can add them in file like above and use them like below (using $ and {..})

in Security tab, enable SMTPS


Click on the ‘+’ icon beside connector configuration and configure as below

in the security tab, enable SMTPS

Note that while using SMTPS connector, we need to add tls-client and tls-trust-store tags. your smtps connector configuration in xml should be like below

Now we have the sub-flow ready. All we have to do now would be to invoke this sub-flow whenever we need to send a mail.


5. Then, start with our new flow, from the Mule Palette drag the IMAP connector. Follow below steps to configure the IMAP inbound endpoint.

  • Click on IMAP connector then, In the General tab, Basic settings, observe that we are getting the value for Host, Port, User and Password from the file (from previous step). Refer below; Figure 2: General Settings. Click on the security tab and enable IMAPS. (Refer Figure 1: Security tab )

    Figure 1: General settings

    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). Click OK.

    Figure 3: Connector Configuration – General

    Figure 4: Connector Configuration- Security tab


6. Create 2 new session variables after IMAP connector to store from address and subject.

7. We will use choice component to check and validate the email subject. For this use case, we expect to receive a mail with the subject as Conversion Rate <Fromcurrency> <Tocurrency> (e.g., Conversion Rate GBP INR, Conversion Rate USD INR)

In the coming steps, we will be validating the same using choice in mule.

1. Create a new sub-flow called country_lookupSub_flow. Let it be empty for now we will be adding required message processors soon.

2. Add the below choice component after the two session variables that we just created.

3. As you see we check if the subject contains Conversion Rate. If so we route to the currently empty countryconversionSub_flow.

4.Else if we receive invalid subject, we call the send_response sub-flow after setting the payload as ‘Invalid Request’

5.Below is how checkForEmailSubject flow would look like till this stage


2. Exception Handling

1. Drag a Catch Exception Strategy in the exception part of checkForEmailSubject flow

2. Add a logger to log the error message

3. Set the payload with the error message and payload at the time error occurred.

4. Add a flow reference to send_response flow so that so that the sender will get notification of the exception caused.

Below is the configuration XML part for catch exception strategy

Below is how the flow looks with exceptions being handled.

As of now, we have created a flow that receives incoming emails, checks if the subject is valid and proceeds to the currencyconversionSub_Flow.

If the subject is invalid a mail is sent back to the caller that the subject was invalid.

3. Call Yahoo finance API, parse the response, generate HTML response and send it to caller

1. Now, we need to call Yahoo Finance API in this section. Assume that, Our subject line we are giving is Conversion Rate GBP EUR. Our  URL should be like below.

2. Here we need to get GBP and EUR in concatenate form GBPEUR.. we will get it using below mule expression. While specifying the path in HTTP endpoint we will use this expression.

3. open currencyconversionSub_Flow and drag HTTP endpoint

4. click on HTTP connector and open plus icon beside Connector Configuration, Add Host as and port as 80

5. In Path area of General Configuration, add path as

/d/quotes.csv?s=#[message.inboundProperties.subject.subSequence(15,message.inboundProperties.subject.length()).replaceAll(‘[” “]’,””)]=X&f=sl1d1t1ba&e=.csv

6. Payload after accessing that API will come as a .csv  file in object form. We need to parse this CSV into our required form. To parse this payload first we will convert it to a string. Then we will split that payload using a comma and we will make individual element by its name using session scope variable in Message Properties transformer.

7. After Object to String, payload will be like below

8. we will split this payload in set payload using mule expression #[payload.split(“,”,6)]

observe the image, in right side input section, we can verify our payload as String.

9. Now, we need to get each element separately. So, add Message Properties Transformer to add multiple variables at a time.

10. Now, we will make this into xml format using transform message component. Add tags as you want and use our session variables like below.

11. At this point, you will get the data in XML format.Then, add XSLT transformer to get the result in HTML format.

You need to add your .xsl file

xsl file data I have given

12. Now, to check the output use logger with expression #[message.payloadAs(java.lang.String)] and adding a choice router to check whether the output is coming or not. If our payload contains “N/A”, we will set payload to “invalid currencies” and call send_response sub flow otherwise it will go to processRecords sub flow. Our whole flow should be like below

for processing Emails details to Filesystem and Database, processRecords Flow should be like below.

If you want more description of this flow, you can refer below link and go to third section mentioned.

Complete configuration of this project in XML is

Complete code you can download from link


Right Click on the project and Run As Mule application. Wait till the application starts up.

Send test email like below

in file system you can have output as


Lessons learned from this use case

– Understanding of how API works
– Working with CSV files in Mule
– Exception handling