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:

 

  1. Open up the New Project dialog, choose Office...2007 under Project Types (either language is fine), and click on Outlook 2007 Add-in 

     image

  2. Name the project HeaderStuffCS or HeaderStuffVB, depending on which language you use, then click on OK

  3. Now let's add our Task Pane.  Right-click on the project in Solution Explorer, choose Add... New Item...

     image

  4. Under Categories choose Office then under Template select Outlook Form Region

     image

  5. For the Name, call this ShowHeaderInfo and click on Add

  6. This will start a series of dialogs to create the Form Region

  7. 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.

    image

  8. Now we get to choose our region type.  For this one we will go with Adjoining then click on Next

    image

  9. For the descriptive text and display messages, deselect the Inspectors that are in compose mode option and click Next

    image

  10. 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

    image

  11. 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.

    image

  12. 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.

    image

  13. Now comes the fun part :)  Double-click the button to crank some code for its click event.

    image

    image

  14. 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.

  15. 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")

  16. 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.

    image

  17. Close Outlook when you are done.

  18. 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:

image

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! :)