Jan Rothkegel
Jan Rothkegel

Reputation: 760

How to access header of mail in Outlook 2019 using VSTO?

In order to read the header a mail item in Outlook, I found the following solution for Outlook 2010 but it does not work with Outlook 2019 (and probably also not with Outlook 365).

var mailItem = item as MailItem;
if (mailItem != null)
{
    var header = mailItem.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x007D001E/");
...

Unfortunately I receive this error message:

System.ArgumentException: 'The property "http://schemas.microsoft.com/mapi/proptag/0x007D001E/" cannot be parsed or has an invalid format.'

So, how to read the header of an e-mail using C# and VSTO? What is considered to be best practice?

Upvotes: 0

Views: 692

Answers (2)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66276

The DASL name is invalid - you have an extra trailing "/". Use "http://schemas.microsoft.com/mapi/proptag/0x007D001E".

Be prepared to handle "not found" errors - no MAPI property is guaranteed to be present.

If you want to see DASL property names for existing properties, take a look at a message with that property set with OutlookSpy (I am its author) - click IMessage button, select the property, copy the value of the DASL edit box.

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49435

First of all, make sure that such property exists on the item. Composed items and in-house Exchange items may not have this property set up. Use the MFCMAPI or OutlookSpy for exploring internals.

Typically you need to use the following code:

Outlook.PropertyAccessor oPA = mailItem.PropertyAccessor;
string headers= (string)oPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS);

where the PR_TRANSPORT_MESSAGE_HEADERS constant stands for the string http://schemas.microsoft.com/mapi/proptag/0x007D001E .

Handling the exception is the only way to check whether such property exists on the item - older versions of Outlook used to return null, but the latest versions always throw the exception.

Upvotes: 0

Related Questions