Welcome to MSDN Blogs Sign in | Join | Help

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;
  16. // 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")

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

  18. Close Outlook when you are done.
  19. 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! :)

Published Tuesday, July 01, 2008 1:13 PM by zainnab

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Saturday, September 13, 2008 12:21 PM by nonsense

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

Hello!

I want to make a add-in that reads a string from a textBox and then filters the whole mail folder. And than it displays only emails that have this string in their header.

How can I do that? Do I need to store emails to a local database or is there a easy way.

Can somebody help? Please

Saturday, September 13, 2008 12:39 PM by zainnab

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

I think the technique above should get you started.  Once you get the header info you can use a RegEx (see my RegEx series of webcasts) to search for whatever you want.  As for returning the mail objects in a form container.  Check out http://www.outlookforms.org/ it will probably have some good samples for you.

Wednesday, October 08, 2008 8:51 AM by Nikolaj

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

Hi.

Is it also possible to use the PropertyAccessor to add Header Values to the email, so i in other tools can see that it is my addin, that sent the email.

Or should it be done in another way.

Wednesday, October 08, 2008 9:15 AM by zainnab

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

Sadly, no.  That information is read only if memory serves.  But most application add a little blub at the end of the email to indicate a tool was involved.  That would be my suggestion.  Here are the mailitem members to get you started:  http://msdn.microsoft.com/en-us/library/bb176688.aspx

Friday, October 10, 2008 3:04 AM by Nikolaj

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

Thanks for your answer. I am working on a addin where i want to add a number to the mail so another system can operate on the mail. Ofcouse i could just add it in the subject, but i thing the correct thing is to place the filenumber in the header. But i guess i have to use CDO then.

Friday, October 24, 2008 12:55 PM by enrique.prados@a-e.es

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

MIster, How can I download VSTO for VS 2008 ??

thanks.

Friday, October 24, 2008 1:32 PM by zainnab

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

it actually comes with VS2008 :)

Just choose the office templates in the New Project dialog and you should be good to go

Saturday, October 25, 2008 5:36 AM by enrique.prados@a-e.es

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

Thanks,

if I want use VSTO in my Winforms project ??

Greetings

Saturday, October 25, 2008 11:32 AM by zainnab

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

It's not really for that :)  You can do some interop stuff to get your winform app to communicate with office but VSTO is specifically designed around add-ins and toolbars that you use inside the various office applications.

Sunday, November 23, 2008 8:20 PM by Goyal

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

This is good for outlook 2007 but do you have any pointers on getting RFC 822 headers and Message ID for Outlook2003 ?

Goyal

Sunday, November 23, 2008 9:42 PM by zainnab

# re: Using Visual Studio 2008 / VSTO / Outlook to Pull Out RFC 822 Header Data

Goyal,

The only way to do it that I know of is though external API's.  Here is a thread that, ironically, mentions my post here but goes on to describe and explain some of the API's that are available.

http://www.outlookcode.com/threads.aspx?forumid=5&messageid=27183

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker