Tuesday, January 08, 2008

Adding your own X-header when sending a message via Exchange Web Services

Thought this was worth a post its something that pretty easy to do with EWS but not something that maybe initially obvious. If you want to add an X-header to a message your sending via Exchange you need to add this via the PS_INTERNET_HEADERS namespace. With EWS you do this when using the MessageType class by adding an Extended property. Eg if you take the basic send message via EWS example from MSDN http://msdn2.microsoft.com/en-us/library/bb408521.aspx and all you need to do is add the following to add your own custom X-header


message.Sensitivity = SensitivityChoicesType.Normal;

// Start X-header Code

PathToExtendedFieldType epExPath1 = new PathToExtendedFieldType();
epExPath1.DistinguishedPropertySetId = DistinguishedPropertySetType.InternetHeaders;
epExPath1.DistinguishedPropertySetIdSpecified = true;
epExPath1.PropertyName = "x-myheader";
epExPath1.PropertyType = MapiPropertyTypeType.String;

ExtendedPropertyType xhXheaderProp = new ExtendedPropertyType();
xhXheaderProp.ExtendedFieldURI = epExPath1;
xhXheaderProp.Item = "blah";
message.ExtendedProperty = new ExtendedPropertyType[] { xhXheaderProp };

// End X-header Code

// Add the message to the array of items to be created.
createItemRequest.Items.Items = new ItemType[1];

7 comments:

Anonymous said...

Hi Glen,

Thanks for your good work, I find it invaluable.

I am just getting to grips with customising OWA 2007, with an HttpModule which picks out custom properties in the request body <params>...</params> XML and creates an email message using Exchange Web Services, ahead of Exchange Server 2007.

I can see from your example how to create custom X-header properties. Is it possible to also create MAPI or named properties using this technique. I find the MSDN documentation unhelpful in this area, for example, what are Common, Public String and Unified Messaging types?

Thanks a lot,
MikeC

Glen said...

Yeah the TNEF writter is a little tricky to use you really need to get a TNEF viewer to help you write something like this. I dont really have any good samples for this i have a half working one that i was playing around with a couple of months ago but haven't really look at since it doesn't really work at the moment but if you want a look let me know. (i think I had it working but lost it becuase i didn't save :(

Cheers
Glen

VasekB said...

Hi Glen,
I have many events in my Exch2k3 EventLog like this:

Event Type: Error
Event Source: MSExchangeIS
Event Category: General
Event ID: 9667
User: N/A
Computer: EXCHANGE
Description:
Failed to create a new named property for database "First Storage Group\Mailbox Store (EXCHANGE)" because the number of named properties reached the quota limit (8192).
User attempting to create the named property: "user"
Named property GUID: 00020386-0000-0000-c000-000000000046
Named property name/id: "X-mailed-to"

I found solution = increase NAMED PROPERTY limit:
http://technet.microsoft.com/en-us/library/bb851493(EXCHG.80).aspx
http://technet.microsoft.com/en-us/library/bb851495(EXCHG.80).aspx

Do you have idea where can I find NAMED PROPERTIES already saved by Exchange ? I did explore Exchange by ADSIEdit, PFDavAdmin, MFCMapi, MBBVU and MSDN/Google and no informations how to list them. Only how to add to message.

Jose said...

Hi Glen,

Thanks for your great work.
Adding x-headers in the way you say here, those headers are not recognized as internet headers by Outlook ( message options ). Is there any additional attribute needed to do that?
Thanks,

Jose

Glen said...

Which message are you looking at if your looking at the message that in the sent items folder then no you wont see this added as a X-header it will only do this when the Hub Transport processes the message so you need to look at the the message the message that the recpient gets. To test it try to send a message to an external account and look at the headers.

Cheers
Glen

Jose said...

I have tried updating a received email. X-headers are added and can be recovered using a EWS query. But when trying to retrieve those headers usig MAPI ( Redemption ) or checking the message options in Outlook those headers do not appear. Any idea why this could be happening?
Thanks,

Jose

Glen said...

This method only works when you send a mail via EWS it wont work if you just update the properties on a received message. When the Hub process the messsages it will promote these properites to the header.

Cheers
Glen