Syllogistic Group

Extract Email Addresses from Outlook

Home »  Migration Tasks »  Extract Email Addresses from Outlook

Extract Email Addresses from Outlook

On September 27, 2016, Posted by , In Migration Tasks, By ,, , With 1 Comment

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.

        /// <summary>
        /// Examine the emails in each folder to get their stuff! Er, email addresses
        /// </summary>
        /// <param name="folder"></param>
        static void GetMessages(Outlook.Folder folder)
        {
            // Lets get all the MAPI items in the folder
            var items = folder.Items;

            // You should always check to ensure something is there, else your program can crash
            if (items != null)
            {
                // We use try statements in another effort to prevent crashes
                try
                {
                    // This spiffy little LINQ statement takes only MailItems from our folder and puts them into a list
                    // If you plan to expand this to other items, such a calender invites, you will need to change it
                    List<Outlook.MailItem> email = folder.Items.OfType<Outlook.MailItem>().ToList();

                    // Lets go work with our emails!
                    foreach (Outlook.MailItem mail in email)
                    {
                        /* Perhaps overkill but in the original code we had to put this in as
                         * one user had a ginormous mailbox that would cause this little program to 
                         * crash. Once I forced garbage collection repeatedly, the crash went away.
                         */
                        GC.Collect();

                        
                        try
                        {
                            // Retriving and saving the sender's address
                            var senderAddress = mail.SenderEmailAddress;
                            AddAddressToDictionary(senderAddress, mail.SenderName);
                        }
                        catch (Exception e)
                        {

                            Console.WriteLine("An error occurred: '{0}'", e);
                        }
                        try
                        {
                            // Get all the recipients, To & CC: (BCC: if you decide to look at sent items as 
                            // well I think) and save them
                            Outlook.Recipients recipients = mail.Recipients;
                            foreach (Outlook.Recipient recipient in recipients)
                            {
                                AddAddressToDictionary(recipient.Address, recipient.Name);

                            }
                        }
                        catch (Exception e)
                        {

                            Console.WriteLine("An error occurred: '{0}'", e);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("An error occurred: '{0}'", e);
                }
            }
        }

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.

        /// <summary>
        /// Take the email address and display name and store them.
        /// I have dictionary in the name so I don't forget what we are working with
        /// </summary>
        /// <param name="emailAddress"></param>
        /// <param name="displayName"></param>
        private static void AddAddressToDictionary(string emailAddress, string displayName)
        {
            // C# does not ignore case so we have to ensure that the case is the same by making it all lower case
            emailAddress = emailAddress.ToLower();

            // The customer didn't want the internal Exchange addresses so we filter out anything that
            // doesn't have the normal email syntax. This is not user input so we don't need regex for checking the validity
            if (emailAddress.Contains("@") && emailAddress.Contains("."))
            {
                bool found = false;
                foreach (KeyValuePair<string, string> t in EmailAddresses)
                {
                    if (EmailAddresses.ContainsKey(emailAddress))
                    {
                        // We could avoid this but it could be harder to understand, so I left it
                        found = true;
                    }
                }
                if (!found)
                {
                    // We didn't find the address so we add it to the dictionary. Yeah!
                    EmailAddresses.Add(emailAddress, displayName);
                    Console.WriteLine(EmailAddresses.Count + ": Added " + emailAddress + "\t\t" + displayName);
                }
            }
        }

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.

Download the Code

Until next time!

Pages : 1 2

One Comment so far:

  1. […] decided to do just that. We compiled the email extraction program. It is the exact version in the article if you decide to code your own. We hope that you find it […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WP Facebook Auto Publish Powered By : XYZScripts.com