Extract Email Addresses from Outlook
Recently, we had a migration for a customer that did not want to bring some older email accounts into the new email system. This occurs often and we recommend removing these inactive accounts if there are not any compelling reasons to keep them. The customer initially just wanted all the emails saved onto disk so that they could search them if desired.
A follow on request to extract all the email addresses from the mailboxes was new for us. After a bit of scoping and examining the Outlook object model, we saw that this should be fairly easy. Fairly easy usually means that we can go to TechNet and get a solution to solve our problem. We did find solutions but they did not work or failed us in a few areas. Therefore, we wrote our own solution to the problem.
This post will cover a considerably pared down version of that solution so that you can accomplish the base task without much difficulty. In this version we are only looking at emails in and under the Inbox root folder.
Create a console project and add a reference to your installed version of the Outlook interop via Microsoft.Office.Interop.Outlook. We also need something to store the email addresses before we save them to a CSV. I like dictionary objects in situations like this because I can be lazy and not need to write code to check for duplicates.
This is what we have so far.
Note that instead of typing out Microsoft.Office.Interop.Outlook each time we created an alias of Outlook on line 5.
We are now going to look at the heart of the program, the Main method.
We want to tell our users what is happening by changing the display periodically and so you will notice a copious amount of Console.WriteLines in the code to do just that. I personally hate programs that sit there, appearing to be idle, because they fail to provide some type of feedback to the user.
The first thing we will do is grab the currently running Outlook instance as an application. If Outlook is not running, then this will start a new application.
Next, we are first going to check that the application variable is not null. Then we are going to pull all the accounts from the profile and create a way cool, ah, simple menu to choose from. If this was a GUI application, then we could make a cool menu.
We will then wait for the user to make a selection. Notice that again, we keep checking for null values. You should always try to make your application crash proof. Well, reasonably crash proof. We will analyze the selection to determine which account the user selected. We also ensure that their selection matched one of the selections presented.
In the snippet above, notice that after we validated that the user selected a proper account, we get the display name of the account and send that to a method called GetFolders. In addition, you see that we will call methods to sort, save, and then display messages to the user about the results. We will briefly cover those later.
The GetFolders method is a doozy! It calls itself repeatedly in the middle of the method. What is going on here? We of course know that this is recursion. Recursion simply means that the method or function calls itself. It is great for dealing with situations like this. We do not know how many child folders a user may have in their inbox or how many folders those folders may have. Instead of just counting folders and keeping a list of them, we use recursion. If a folder has a child folder, then we call GetFolders with that folder’s name. Eventually, we get to the bottom of the tree and at that point, we start getting messages by using the GetMessages method. As each child folder completes, the folder above it gets focus again. Once all the child folders in that folder are processed, then it is that folders turn.
So let us say that our user has the following folder tree.
- Fishing Tips
- Vacation Ideas
What would be the expected processing order of the folders? If you said, Signed, Contracts, Fishing Tips, Vacation Ideas, Personal, and then Inbox, pat yourself on the back as you understand how recursion works! Yeah!
After all that, now you see why the first thing the method does is look for child folders. You may ask why we are not using the term subfolder instead of child folder. Simple, Outlook does not. We are trying to use the names that the Outlook object model uses. The method is below and I hope that it is easy to understand now.
Jump to the next page so that we can continue.
Pages : 1 2