Wednesday, May 30, 2007

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

 

As part of my job I am required to connect to a third party VPN. The security on that network requires, among other things, all major updates. So, when I tried to login today and could not I decided to force an update. That update took me 3 hours in all. The reason being that SvcHost decided to hog my computer. Steadily mocking me at 99% CPU utilization.

After some head banging I decided to try out my support options. Google was my first stop. I found this site. A very handy description about how to solve this "rare" problem where SvcHost pegs your CPU and Windows Update never gets anywhere. Worked like a charm for me. Thank you Ken!

This also gave me a chance to browse the web a little. A dangerous prospect as it can be hours before I resurface, trying to recall what I was doing in the first place. I came across two things of note.

ZoomIt

The ZoomIt utility from the sysinternals folks. Excellent utility for when you give presentations. A feature I particularly like is being able to wipe the screen blank and write text on it.

Microsoft Surface

The other site I came across was Microsoft Surface. Wow! I want one now! I do think they missed the boat a little with respect to Silverlight. Why not have 2 demonstrations? One done in Flash and another done with fancy interaction in Silverlight?

I like the idea of rotating photos, but seriously, can Microsoft Surface not detect where you are and orient the photos correctly the first time?

The drink demo with the bubbles was neat. What about the Surface detecting you are close to the end of your drink and asking if you would like another?

What about games? It would be cool to see a poker demo complete with digital playing cards and virtual chips.

I would also have liked to see integrated fingerprint/retina reading in the demo. That way, I would not feel so hesitant about putting my PDA or my credit card on one of these Surfaces.

Lastly, this opens up a whole new class of exploits. I love the idea of hacking my way to free desert at a restaurant! Yummmy!