Get email Headers from an Outlook MailItem

While working on a VSTO add-in for Microsoft Outlook, I came across the need to access the headers of an email, to parse out X-CustomProperty tags that were put in by an application that sent the emails. However, I couldn’t find a Headers property in the Outlook object model, so below is an attempt to create one.

The Regex used is courtesy of John Gietzen’s answer on this stackoverflow question.

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Office.Interop.Outlook;

public static class MailItemExtensions
    private const string HeaderRegex =
        @"^(?<header_key>[-A-Za-z0-9]+)(?<seperator>:[ \t]*)" +
            "(?<header_value>([^\r\n]|\r\n[ \t]+)*)(?<terminator>\r\n)";
    private const string TransportMessageHeadersSchema =

    public static string[] Headers(this MailItem mailItem, string name)
        var headers = mailItem.HeaderLookup();
        if (headers.Contains(name))
            return headers[name].ToArray();
        return new string[0];

    public static ILookup<string, string> HeaderLookup(this MailItem mailItem)
        var headerString = mailItem.HeaderString();
        var headerMatches = Regex.Matches
            (headerString, HeaderRegex, RegexOptions.Multiline).Cast<Match>();
        return headerMatches.ToLookup(
            h => h.Groups["header_key"].Value,
            h => h.Groups["header_value"].Value);

    public static string HeaderString(this MailItem mailItem)
        return (string)mailItem.PropertyAccessor

Sample usage:

string[] preparedByArray = mailItem.Headers("X-PreparedBy");
string preparedBy;
if (preparedByArray.Length == 1)
    preparedBy = preparedByArray[0];
    preparedBy = "";

string allHeaders = mailItem.HeaderString();

Edit 2011/08/05:
Thanks to Rob for the heads-up. I updated the code to return a Lookup instead of a Dictionary – this handles the case of multiple headers with the same name.

Registry settings to add Uninstall as a shell option for VSTO manifest files

vsto logo

Visual Studio Tools for the Microsoft Office System (VSTO)

I have been working on some VSTO add-ins and on deploying them, and have something to share that I didn’t find anywhere else.

First of all, let me just say that despite certain hurdles, deploying using ClickOnce has been a very pleasant experience. After following some tutorials on making an MSI, we eventually ended up simply deploying the few needed registry entries to all users, which take care of installing and trusting each add-in. Then, once the add-in is installed, it takes care of updating itself every so often.

Unfortunately, during the period of figuring this out a few users got an older version then a newer version with a new signing certificate, which is I suspect the reason why they get the following error message:

Specified argument was out of the range of valid values.
Parameter name: entryValue

Exception Text
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: entryValue
  at Microsoft.VisualStudio.Tools.Applications.Deployment.RegistryStore.Retrieve(String entryName, Object entryValue, CompareDelegate compareMethod)
  at Microsoft.VisualStudio.Tools.Applications.Deployment.MetadataStore.UpdateLastCheckedTime(String subscriptionID, DateTime newLastCheckedTime)
  at Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.InstallAddIn()

There is some discussion of this error here.

Anyway, I tried removing the folder: C:\Documents and Settings\%username%\Local Settings\Apps\2.0 which causes the add-ins to re-install themselves, but that didn’t fix the problem.

The second solution I tried worked, which was to uninstall the add-in and then re-install it again. One way to uninstall is to do so via Add/Remove programs (still using XP lingo), but I came across another way from this blog post at ChristophDotNet: simply run this command:

C:\Program Files\Common Files\Microsoft Shared\VSTO\9.0\VSTOinstaller.exe
     /uninstall &lt;name-of-manifest-file.vsto&gt;

I find this way much easier than waiting for the Add/Remove dialog to populate (takes a while on my bloated box). You can read more about the VSTOInstaller on MSDN.

After using this technique for a while, I realized one thing – to install an MSI file, you right-click on it and choose Install, and you can also right-click to Uninstall. However, with .vsto files, you can only install. Why not add the uninstall capability right there using a shell command? And now that I knew what the command was, it was trivial. First, to find out where in the registry the open command is defined, I came across this post on Nikhil’s blog. Then, I simply added an uninstall component to it:

Windows Registry Editor Version 5.00


@="\"C:\\Program Files\\Common Files\\microsoft shared\\VSTO\\9.0\\VSTOInstaller.exe\" /uninstall \"%1\""

Put that in a .reg file and run it, and you’ll be able to uninstall VSTO add-ins with ease!

This post applies to VSTO version 3.0.