none
How to get an x-header from an email message using EWS managed API RRS feed

  • Question

  • I'm using Exchange 2010 SP1 (in case it has a know bug that I'm not aware of).

    Everything I've read so far leads me to believe that all I need to do to get an x-header value from an email is something like this:

            static public readonly ExtendedPropertyDefinition TESTHeaderProperty = new ExtendedPropertyDefinition( DefaultExtendedPropertySet.InternetHeaders, "x-myheader", MapiPropertyType.String );
    ...
    
                PropertySet ps = new PropertySet();
                ps.Add( TESTHeaderProperty );
    
                ItemView itvw = new ItemView( 1 );
                itvw.PropertySet = ps;
    
                SearchFilter.IsEqualTo filt = new SearchFilter.IsEqualTo( my search criteria );
    
                FindItemsResults<Item> res = service.FindItems( WellKnownFolderName.Inbox, filt, itvw );
    
                foreach ( Item it in res )
                {
                    object obj;
                    if ( it.TryGetProperty( TESTHeaderProperty, out obj ) )
                    {
                    }
                }
    

    But TryGetProperty always fails.

    I know the x-header exists on the message.

    If I use the diagnostic tracing and also request the full headers using this property:

    static public readonly ExtendedPropertyDefinition PR_TRANSPORT_MESSAGE_HEADERS = new ExtendedPropertyDefinition( 0x007D, MapiPropertyType.String );
    

    Then I can see that a truncated form of the headers are returned.

    If I use Item.Bind or item.Load, then the full headers are returned and I can see that my x-header is present, but it's still not accessible directly by using TryGetProperty.

    Am I missing something, or doing something wrong?

    I should add that I'm also testing using EWSEditor and I can't get that to retrieve the x-header directly either.

    Tuesday, February 10, 2015 5:31 PM

Answers

All replies

  • Because the PR_TRANSPORT_MESSAGE_HEADERS property is larger the 512 Bytes it won't be returned fully in a FindItem Operation (only the first 512 Bytes will be returned). Also with InternetMessageHeaders collection this is only returned when you use GetItem operation see https://msdn.microsoft.com/en-us/library/bb508824.aspx . So you need to include a Load or better LoadPropertiesForItems in your code.

    With using the X-Header you should just be able to use InternetHeadersCollection after the GetItem request otherwise to use the Extended Property you need to provision the X-Header for every Mailbox you want to use it on see https://msdn.microsoft.com/en-us/library/office/dn596091%28v=exchg.150%29.aspx . This is due to changes made around name property quotas https://technet.microsoft.com/en-us/library/bb851492(v=EXCHG.80).aspx

    Cheers
    Glen

    Wednesday, February 11, 2015 4:43 AM
  • Hi Glen,

    It seems I have fallen foul of the "provisioning" requirement mentioned in that document...

    "Exchange adds incoming x-headers to the mailbox schema as a named property the first time it receives an email with that x-header. The x-header value is not saved on that first email; however, it is saved on all subsequent emails that include the x-header."

    It's unbelievable that someone could design a system that way. I recall reading that now, but felt it was so inherently wrong that the documentation must be wrong. If there had been some special operation that you had to do to "provision" the mailbox, that would have made sense, but to do it on normal message handling and then discard it for the first message seems like a mistake that's now been made official by documenting the bad implementation.

    Anyhow, now I've added another message with my custom x-header, the second occurrence results in the requested x-header being returned.

    Another point that people should be aware of is mentioned here: https://msdn.microsoft.com/en-us/library/office/hh545614(v=exchg.140).aspx

    "To get the message headers, the EWS schema exposes the InternetMessageHeaders first-class property. Unfortunately, this property does not appear to return a complete set of message headers. For example, in Exchange 2010 Service Pack 1 (SP1), the InternetMessageHeaders property doesn't return address headers such as From and To. "

    ... so when I was testing with standard non-x header headers, that would explain why they couldn't be retrieved.

    Thanks for making me look again.
    Dave

    Wednesday, February 11, 2015 10:02 AM