Extract Email Addresses from Outlook
Finally, we are going to look at messages! First thing is that we need the items in the folder. The code we are sharing is only looking at mail items. Of course, calendar invites, etc. also have email addresses that you may want to capture. We use LINQ to gather the mail items so that we can do away with loops. Notice the note in the code below about garbage collection. Someday when we revisit this, we will look at better methods to eliminate the root problem.
Take note of the try..catch statements. We do not want the program to crash and sometimes, Outlook will mangle an email. We capture the sender address and all the recipient addresses and send them over to the AddAddressToDictionary method to store. Why use a method to store an item in a dictionary you may ask. After the code block below, we will discuss why.
The initial reason for using a method, since we all know that a dictionary will not let you add the same key twice, was for displaying progress to the user as the program was running. Remember, we had some giant mailboxes at this client! The program runs much faster without the screen update but the techs running it thought it had crashed since nothing was happening. Therefore, we made something happen by updating the screen with email addresses!
Then things got complicated. Someone figured that if a display name was the same, they did not want that email address added either. We disagreed, lost, and started adding in code looking for duplicate display names. Then we found a perfect case of why this was a bad idea with the display name and convinced the client that we will only check for address duplicates.
Notice the copious use of comments in the code. This not only helped me slim the code down for the web, but also makes it very easy for someone else to see what the logic, or illogic, of what particular lines and sections are doing. That someone else could be me! I needed some code written in 2010 for a migration and I had to run some translator modules on it to understand what was going on. You do know that now, that code has comments all over it.
The last three methods are pretty basic so we will not cover them in detail. GetAccountEmailAddress is just a fail back method on getting the user’s address. We were going from Exchange to O365 so it really was not necessary but I have no clue what your system is or the next system where we will use this program. SaveAddressesToFile just saves the sorted addresses to file. SortEmailAddresses uses the laziest sort method I could find, letting C# do it for me! I also left the link in the comments on how to sort by display name. Something I no longer had to do. Lastly, GetFolder simply, with a little magic, takes in the string path of a folder and returns an Outlook.Folder object! That came from the MSDN forums and I apologize as I lost the link to the original post. That code is all over the place now.
The output file is a straightforward CSV. I highly advise that you look at this file before simply importing into something else. Display name standards are vastly different amongst companies and I took the time to split display name if possible or the email addresses into first and last names for our client. That way the client had more robust information for their contact files.
I hope this helps you in your work! I have a text copy of the code attached to this post, as I did not want to put up the entire 325+ lines of code! Soon, I’ll also compile it and store so you only have to download the program.
Until next time!
Pages : 1 2