Push Notifications from Outlook to iPhone

I work at a company that uses Good to allow me to see my exchange email and check them when I’m out of the office or away from my keyboard, this is great and I like the flexibility of not being tied to my desk in order to know if anything important has come up.

It would be great if I could get the same functionality with my personal Outlook accounts. I have a mixture of IMAP and Exchange accounts on my personal Outlook and would love to be notified when I receive a new message (I have these accounts set up on my iPhone but since they are not push accounts I have to wait an agonising 15 minutes before I get the emails through). A cheeky little push notification would do just nicely.

Luckily I already have an app which allows me to send push notifications to my iPhone via a CURL request. All I need to do is build a C# application that sits on my home PC and scans my Outlook for new mails and sends a push notification when it sees new mails.

Here’s what I did.

Step 1- Add references.
Add a reference to Microsoft.Office.Interop.Outlook

Step 2 – Import required libraries.

using Microsoft.Office.Interop.Outlook;

Step 3 – Get a list of all my accounts set up in Outlook.

app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);
Stores stores = ns.Stores;
foreach (Store store in stores)
{
    cmbAccount.Items.Add(store.DisplayName);
}

And then get a list of all the folders in the selected account. This is used to populate the GUI so that I can select which account and folder I want to monitor.

cmbFolder.Items.Clear();
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);
Stores stores = ns.Stores;
foreach (Store store in stores)
{
    if (store.DisplayName == cmbAccount.Text)
    {
        Microsoft.Office.Interop.Outlook.MAPIFolder rootFolder = store.GetRootFolder();
        Microsoft.Office.Interop.Outlook.Folders folders = rootFolder.Folders;
        foreach (Microsoft.Office.Interop.Outlook.Folder f in folders)
        {
            cmbFolder.Items.Add(f.Name);
        }
        cmbFolder.Text = "";
        break;
    }
}

Step 4 – Scan my selected folder for new emails and send notifications when required.

try
{
    app = new Microsoft.Office.Interop.Outlook.Application();
    ns = app.GetNamespace("MAPI");
    ns.Logon(null, null, false, false);
 
    Stores stores = ns.Stores;
    foreach (Store store in stores)
    {
        if (store.DisplayName == Properties.Settings.Default.Account)
        {
            Microsoft.Office.Interop.Outlook.MAPIFolder rootFolder = store.GetRootFolder();
            Microsoft.Office.Interop.Outlook.Folders folders = rootFolder.Folders;
            foreach (Microsoft.Office.Interop.Outlook.Folder f in folders)
            {
                if (f.Name == Properties.Settings.Default.Folder)
                {
                    inboxFolder = f;
                    break;
                }
            }
        }
    }
 
    if (inboxFolder == null)
    {
        inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
    }
    items = inboxFolder.Items;
    items.Sort("[ReceivedTime]", true);
 
    for (int i = 1; i <= items.Count; i++)
    {
        try
        {
            item = (Microsoft.Office.Interop.Outlook.MailItem)items[i];
            if (item.ReceivedTime < DateTime.Now.AddMinutes(-5))
            {
                break;
            }
            if (sendNotification && item.UnRead == true && !ProcessedItems.Contains(item.EntryID))
            {
                string EntryID = item.EntryID;
                string Subject = item.Subject;
                string Body = item.Body;
                string SenderName = item.SenderName;
                string ReceivedTime = item.ReceivedTime.ToString("yyyy-MM-dd");
                ProcessedItems.Add(EntryID);
                string alert = "From: " + SenderName + Environment.NewLine + Subject;
                string Username = textBox3.Text;
                string Password = textBox4.Text;
                bool result = intellishare.SendAlertToUser(Username, Password, alert);
                if (result == false)
                {
                    stopLooking();
                    MessageBox.Show("Username or Password incorrect.", "Outlook Notifier");
                    break;
                }
            }
        }
        catch { }
    }
}
catch (System.Runtime.InteropServices.COMException ex)
{
    Console.WriteLine(ex.ToString());
}
finally
{
    ns = null;
    app = null;
    inboxFolder = null;
}

And boom we’re done. Obviously there’s other non Outlook-y type code needed like selecting the account and folder I want to monitor and code for the GUI but this is the shows the fun stuff.

This does require my PC to be switched on and Outlook to be running to work but this is always the case in my house so no problem there.

Coding is such fun.


So, what do you think ?