I need to access a custom MAPI property on existing mail messages in Exchange. The NamedProperties collection data store only appears to be populated after a MailMessage is saved to disk, read back in and converted to MAPI format (I believe this is a known workaround). However, the custom property I need is not persisted when saving to an MSG file (either manually in Outlook, or via Aspose) so the workaround doesn’t work in this situation. Is ther eanother way to access MAPI properties for existing items?
Hi Clark,
var message = MapiMessage.FromMailMessage(MailMessage.Load((“testing.msg”)));
foreach (MapiNamedProperty namedProperty in message.NamedProperties.Values)
{
if (namedProperty.Oom == “PropertyName”)
{
string propertyValue = namedProperty.GetString();
Console.WriteLine(“Named Property :” + propertyValue);
}
}
Thanks for your response. My MailMessage seems to be missing all of it’s named properties (except one) for some reason, ie. after using
mapiExchangeMsg = MapiMessage.FromMailMessage(exchangeMsg);
the NamedProperties.Values collection is empty except for one entry. If I write the same Exchange MailMessage to disk (MSG) with MailMessage.Save and open with OutLookSpy this also verifies named properties are missing. If I save the email as an MSG file using OutlookSpy or Outlook then I can open it with MAPIMessage.FromFile and everything is there.
The issue appears to be with MailMessage rather than Exchange. If I load the MSG file (verified to have all properties as above) into a MailMessage using MailMessage.Load, then save using
mailWorkMsg.Save(@"c:\temp\OutlookSpy Record Retention MailMessage.msg", MailMessageSaveType.OutlookMessageFormatUnicode);
then read this back into a MAPI message using MapiMessage.FromFile the properties are gone. I'll send you the example MSG file containing all the properties.
Attached is the sample MSG file with all properties…
Also, is it possible that the following earlier support issue could be relavent?:
If you create a new MapiMessage (not load from file), then MapiMessage.NamedPropertyMapping is null.
MapiMessage mapi = new MapiMessage();
MapiPropertyCollection namedProperties = mapi.NamedProperties;
MapiNamedPropertyMappingStorage mappingStorage = mapi.NamedPropertyMapping;
MapiProperty property = new MapiProperty(WaveMapiPropertyTag.PR_WAVESOFT_FROM, Encoding.Unicode.GetBytes("test value - hello 123"));
mappingStorage.AddNamedPropertyMapping(property, WaveMapiPropertyTag.GetPropertyName(WaveMapiPropertyTag.PR_WAVESOFT_FROM), new Guid()); // <-- will crash because 'mappingStorage' is null
mapi.SetProperty(property);
Hi Clark,
var message = new MapiMessage("from@domain.com", "to@domain.com", “subject”, “body”);
var storage = message.NamedPropertyMapping;
var stringProperty = new MapiProperty(0x00000000F001001f, Encoding.Unicode.GetBytes(“this is a test property unicode string value”));
message.SetProperty(stringProperty);
storage.AddNamedPropertyMapping(stringProperty, “testProperty”, new Guid());
message.Save(“property.msg”);
var loadedMessage = MapiMessage.FromFile(“property.msg”);
var propertyValue = message.GetPropertyString(0x00000000F001001f);
Console.WriteLine(propertyValue);
Thanks for your efforts to resolve my issue. The results I noted earlier were just diagnostic to determine the root cause of the issue. The basic scenario is that I am reading Exchange and need to check a particular custom MAPI property value for each message. For this reason I am starting with a MailMessage collection with property values already set. I know the property is set in the Exchange message but can’t get to it via MailMessage named property collection to check it. If I save to disk and read it back in as a MAPI message it isn’t there either. My intuition is that MailMessage doesn’t preload all custom MAPI properties to the named attribute by default, but perhaps there is a way to request the specific value I need (PTDOCREF). Hope this helps to clarify and thanks for the assistance.
Hi Clark,
The property in question was set by an email archiving system and contains a key value for lookup in the archive (the message itself, in Exchange, is unchanged other than having the property set). You will see this property value in the test email I provided.
In this situation, I must drive off of Exchange - I'm reading it with Exchange Web Service Client, EWS.ListMessages for each folder then EWS.FetchMessage with unique id to get each specific message in detail (MailMessage format). I am under the impression that one must use MailMessage for direct Exchange access.
There is a FetchMessage overload that also provides an Extended Properties array - might this be used? Wasn't sure if this was for Exchange custom properties versus MAPI extended properties...
Hi,
Hi Clark,