Please see this previous post on how to get started using LINQ with Outlook. I will assume you have added the appropriate references to the Outlook object model.

In this post I am going to show you how to find emails from people that you don't have in your address book. We will achieve this by cross referencing the information in your contacts with the emails in your inbox.

Now, inside the Main method, we need to add the following code:

Outlook._Application outlookObject = new Outlook.Application();

This code references the Outlook application. We now need to specify which Outlook folders we wish to use:

Outlook.MAPIFolder contactsFolder = outlookObject.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

Outlook.MAPIFolder inboxfolder = outlookObject.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

The email addresses for contacts are contained in the Email1Address, Email2Address and Email3Address. These are three separate properties on a contact. We can get that information like so:

var contactsEmail = from contact in contactsFolder.Items.OfType<Outlook.ContactItem>()

select new List<string>() { contact.Email1Address, contact.Email2Address, contact.Email3Address };

This will give us a collection of a list of email addresses. Each contact may have up to three email addresses specified. This means that the list may contain null entries. In addition it is not very useful to have an embedded collection. If we kept it this way we would have to have two foreach loops to iterate through the data. Fortunately, LINQ provides the SelectMany operator:

var allContactEmails = contactsEmail.SelectMany(sel => sel.Where(e => e != null));

This single line of code transforms the collection of lists into a single collection of strings with no null entries. To help us compare the email addresses we now convert them to lower case using the Select operator and remove duplicates with the Distinct operator. Note that we could have done that on the previous line after the Where:

var allContactEmailsLower = allContactEmails.Select(sel => sel.ToLower()).Distinct();

Now, let's get the sender's email address from each email in the inbox:

var emailItems = (from email in inboxfolder.Items.OfType<Outlook.MailItem>()

where email.SenderEmailType == "SMTP"

select email.SenderEmailAddress.ToLower()).Distinct();

We now have a collection of sender email addresses and a collection of email addresses from our address book. We would like to find out the email addresses that are in the senders list but not in our address book. We do that with the Except operator:

var exceptions = emailItems.Except(allContactEmails);

The Except operator will return items in the first sequence that are not in the second. All that is left is to output our results:

foreach (string str in exceptions)

{

Console.WriteLine("{0}", str);

}

That's all there is to it. Now you can keep your address book up to date. It would be relatively trivial to update the program to update your address book J