Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data
Mike B. asked me to show how to get header information from emails using VSTO. So here is a step-by-step tutorial on how to create a Form Region that will show header info:
- Open up the New Project dialog, choose Office...2007 under Project Types (either language is fine), and click on Outlook 2007 Add-in
- Name the project HeaderStuffCS or HeaderStuffVB, depending on which language you use, then click on OK
- Now let's add our Task Pane. Right-click on the project in Solution Explorer, choose Add... New Item...
- Under Categories choose Office then under Template select Outlook Form Region
- For the Name, call this ShowHeaderInfo and click on Add
- This will start a series of dialogs to create the Form Region.
- The first dialog just let's us choose to create a new form or use an existing one. Just go with the default (new form) and click Next.
- Now we get to choose our region type. For this one we will go with Adjoining then click on Next
- For the descriptive text and display messages, deselect the Inspectors that are in compose mode option and click Next
- Finally, we get to choose which of the eight standard classes we want to use this region with or we can specify a custom class. We will leave the default setting here (mail message) and click on Finish
- You will see ShowHeaderInfo.cs or ShowHeaderInfo.vb in Solution Explorer and a design surface should be visible. Resize the surface to make room for a couple of controls.
- In this case we will throw on a button and a textbox. Set the multiline property of the textbox to true and resize it a little. Also, press F4 to bring up the properties and set its ScrollBars property to Vertical.
- Now comes the fun part :) Double-click the button to crank some code for its click event.
- To get to the header information that we may want we will use the PropertyAccessor. Essentially this will give us a way to dig into a variety of different information available in the message.
- Type the following inside the click event:
CS
// get a reference to our mail item
Outlook.MailItem curMail = (Outlook.MailItem)this.OutlookItem; // use the property accessor to get info into our textBox
textBox1.Text = (string) curMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E");
VB
' get a reference to our mail item
Dim curMail As Outlook.MailItem = Me.OutlookItem
' use the property accessor to get info into our textbox
TextBox1.Text = curMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E")
- Press F5 to run your project. It should start up Outlook and when you read any mail you should see your region at the bottom. Click on the button to see the header information.
- Close Outlook when you are done.
- That's it! Congratulations you can now get header information!
At this point you are probably pissed because you don't know what the heck that whole GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E") thing did. Well, basically there are a variety of ways you can get info using the PropertyAccessor. Take a look at http://msdn.microsoft.com/en-us/library/bb147567.aspx and you will see a listing of these ways.
You accessed the information described in RFC 822. Specifically, you dumped out the entire SMTP message envelope. To do this, I used the "mapi/proptag/{some hex value}" option. But I needed to know the hex value so where did it come from? Here is where you can get the values that can be used: http://msdn.microsoft.com/en-us/library/ms530451.aspx
If you click on the link for PR_TRANSPORT_MESSAGE_HEADERS it will take you to a description of the value(s) you will get and the values you need to get them. You will note there is a Property Tag entry where we can get the hex value we use to get our information:
If we plug the hex into our code, we get: GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E") and we are in business
Now it's up to you to play with the different values to see what you can get back. Enjoy! :)