Extracting Email Addresses from Outlook Mailboxes using C#


Wednesday, 18 November 2015

Share with: 
 

Introduction

Sometimes it's handy to be able to easily extract a list of email addresses from your Outlook PST or OST file.

The 'old fashioned' way of doing this is to export the mailbox to a CSV file, and only include email addresses as a field.

But with large mailboxes, this can be time consuming and cumbersome, and outlook will often include internal sender ID's instead of email addresses, which are not overly useful.

There are also a number of commercial applications to extract email addresses, but why pay money when you can just use a few lines of code? :-)

The Application

This application perform a few simple tasks required to extract a listing of email addresses (and a counter for how often each is used).

  1. Find the root folder in the Outlook Datastore
  2. Iterate recursively through the folder structure
  3. Iterate through each email message in each folder
  4. Parse each message, saving its sender address and all recipients

Which email addresses does it use?

Currently the application extracts each message's sender email address, and it also iterates through all CC'd addresses.

One limitation is that your email address is likely to be in the list of recipients (how else would you have received the message!), so it is likely your address will have the highest counter value.

It is relatively trivial to exclude your own email address - simply ammend the add_address_to_list() function to check for your address and not add it if found.

Prerequisites

adding the Microsoft.Office.Interop.Outlook assembly reference in visual studio

Firstly, create a C# console application in Visual Studio, targeting the .NET 4.5 or higher framework.

The application makes use of the Microsoft.Office.Interop.Outlook assembly, so you'll need to add this as a reference in your project.

The Outlook Primary Interop Assembly (PIA) Reference provides help for developing managed applications for Outlook 2013 and 2016. It extends the Outlook 2013 and 2016 Developer Reference from the COM environment to the managed environment, allowing to you interact with Outlook from a .NET application.

You also need to have Microsoft Outlook installed on your PC - otherwise the Interop assembly has nothing to talk to.

Learn more on MSDN.


Iterating through Outlook Accounts

Before we can go through each folder and email in Outlook, we need to find an actual account, and build the root folder from this.

The root folder is in the format \\foldername\, and the inbox is located one level below this, at \\foldername\Inbox\.

To do this, we simply iterate through the Outlook.Application.Session.Accounts collection.


Outlook.Application Application = new Outlook.Application();
Outlook.Accounts accounts = Application.Session.Accounts;
foreach (Outlook.Account account in accounts)
    {
        Console.WriteLine(account.DisplayName);
    }
    

From these, we can derive the root folder name.

Recursing through folders

Using the function below, we initially pass it the root folder. It then looks for any child (sub) folders, and passes this to itself recursively, following the folder structure until it reaches the end.


static void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders = folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // We only want Inbox folders - ignore Contacts and others
            if (childFolder.FolderPath.Contains("Inbox"))
            {
                Console.WriteLine(childFolder.FolderPath);
                // Call EnumerateFolders using childFolder, to see if there are any sub-folders within this one
                EnumerateFolders(childFolder);
            }
        }
    }
}

Iterating through Emails and retreiving email addresses

Using the function below, we retrieve the sender address, and then iterate through the recipients collection, passing all the addresses found to a function add_address_to_list. The function simply searches a dynamic array to see whether the address has been found previously, and if so, it increments it's counter. This keeps track of how many times an email address has been found.

folder.

If the function can't find the address in the array, it adds it.


string senderAddress = mailitem.Sender.Address; 
add_address_to_list(senderAddress);      
                    
Outlook.Recipients recipients = olMailItem.Recipients;
foreach (Outlook.Recipient recipient in recipients) 
{
    add_address_to_list(recipient.Address); 
}


static void add_address_to_list(string emailAddress)
{
    if (emailAddress.Contains("@") && emailAddress.Contains("."))
    {
        bool found = false;
        for (int i = 0; i < emailAddresses.Count; i++)
        {
            if (emailAddresses[i] == emailAddress)
            {
                // email address was found, so just increment it's counter
                found = true;
                emailAddressesCounter[i]++;
            }
        }
        if (!found)
        {
            // email address wasn't found, so add it to the array
            emailAddresses.Add(emailAddress);
            emailAddressesCounter.Add(1); //starts with a count of 1
            Console.WriteLine(emailAddresses.Count + ": Added " + emailAddress);
        }
    }
}

Download

You can download the code to this project from GitHub, or check out the code below.

Download Follow @matthewproctor

The Full Code

Testing

I've tested this code on mailboxes hosted with an on-premises Exchange 2013 environment, Office 365 and a POP3/IMAP mailbox as well - all functioning exactly the same.

Further reading

The links below provide more information on how to use the Outlook Interop service.

Tags

Email, Outlook, Exchange, C#, PST, OST, CodeProject
Sometimes it's handy to be able to easily extract a list of email addresses from your Outlook PST or OST file.  This application uses C# to extract email addresses from Microsoft Outlook.
 
 

Popular Articles

What is Kutamo?
Kilimanjaro 2015
Exploring Lync and IoT
Exchange 2013 in 60 minutes
Monitoring Lync with MRTG
Lync UCWA Tutorial - Introduction
Tutorial Parts 1 | 2 | 3 | 4 | 5

Recent Articles

Australian Postal Codes
Skype Web SDK
Using the Skype Web SDK from any language or framework
Building a Skype for Business Auto Responder using the Skype Web SDK
Exporting Lync or Skype for Business Contacts with the Skype Web SDK

Favourite Links

Kutamo Studios
ErrLog.IO
Kutamo
Telco Together Foundation
Cloud on Kilimanjaro