Email Price Checker – Part 1: Initial Setup

This post will walk through a complete project from start to scratch. Previously I’d created a small Selenium price checking test which runs great, but means that my pc has to run the app and no one can use it except me. This new project will build on that but receive an email, check the prices (slowly) and return an email with the cheapest codes back to the sender – that way I can use it from work 😉

For this app, I’m using Mule IDE, Java 1.6+, a Ubunutu server to run the application once written and an email address dedicated to listening for the price checking requests.

Lets get started!

To start with, we need a new project. This is using 3.3.2 of Mule because that’s the version I’d downloaded during the Mule training course. There is a newer version available, but it’s not necessary for now.

price-checker-1

I tend to always use Maven as it makes it a lot easier to pull in dependencies.

price-checker-2

So, once the projects been setup, we get the visual flow page ready to start building the application.

price-checker-3

At this point it might be worth explaining what I want to achieve…
I need the application to:

  • Read emails from an email account.
  • Check they originate from a specific set of addresses to filter out rubbish.
  • Check they contain a code for the website we will be checking prices on (CPC).
  • Delay the message if there is already one in progress in order to prevent us being banned on the CPC website.
  • Each code contains 100 price sub codes – check the prices one at a time and delay between each sub code to prevent being banned.
  • Return the lowest codes and the price ex+inc vat to the sender.

First we need an email endpoint and connector for both sending and receiving of emails. I’m using POP3, but you could use IMAP. Additionally the body of the email will only contain the code needed to price check, so the email to string transformer has been chosen.

price-checker-4

price-checker-5

Or in code terms, that looks like:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:context="http://www.springframework.org/schema/context" xmlns:pop3s="http://www.mulesoft.org/schema/mule/pop3s"
	xmlns:smtp="http://www.mulesoft.org/schema/mule/smtp" xmlns:email="http://www.mulesoft.org/schema/mule/email"
	xmlns:pop3="http://www.mulesoft.org/schema/mule/pop3" xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.3.2"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/pop3s http://www.mulesoft.org/schema/mule/pop3s/current/mule-pop3s.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/smtp http://www.mulesoft.org/schema/mule/smtp/current/mule-smtp.xsd
http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd">

	<pop3s:connector name="Pop3sConnector" doc:name="POP3"/>

    <context:property-placeholder location="pricechecker.properties"/>

	<flow name="PriceCheckerFlow1" doc:name="PriceCheckerFlow1">
		<pop3s:inbound-endpoint host="${email.host}"
			port="${email.port}" user="${email.account}" password="${email.password}"
			responseTimeout="240000" connector-ref="Pop3sConnector" doc:name="POP3"/>
        <logger message="Message Received: #[message.payload]" level="DEBUG" doc:name="Logger"/>
	</flow>
</mule>

And the properties file mentioned contains the following:

price-checker-6

At this point, I’m going to test what I have by adding a logger to see what the payload is before I continue.

price-checker-7

Run the application by right clicking on the project –> Run As –> Mule Application.

At this point I spent the next 8 hours or more tearing my hair out as I could not get anything to connect to my email account… After installing 5 different Comodo SSL certificates and the one for my web hosting, I still couldn’t connect. These are some useful settings for anyone in the same position:

-Dmail.debug=true
-Djavax.net.debug=all

When I looked very carefully at the certificates being checked, I found that avast! (antivirus) was interfering with the certificate hashes meaning the connection wouldn’t authenticate!

When eventually you do get it communicating with your email box, having sent an email to the box…

price-checker-8

…you’d see something like this in the logs:

price-checker-9