We have a requirement to generate a Mail Message and add a Custom MAPI property to the message, that will persist when the Mail Message is saved as an MSG file.
I have tried adding a custom property using the SetStringProperty, which appears to work
Aspose.Network.Outlook.MapiMessage mapiMessage = Aspose.Network.Outlook.MapiMessage.FromMailMessage(newMessage);
mapiMessage.SetStringPropertyValue(0xA000001E, “PropertyValue”);
mapiMessage.Save(“c:\temp\testmessage.msg”);
However when I open the msg file in Outlook the property does not appear on the message.
Any ideas as to why ?
Hi,
Thank you for inquiry.
Please see the following example for adding custom properties to an MSG file and reading them.
// Load MSG file
MapiMessage msg = MapiMessage.FromFile(“test.msg”);
// Adds 2 mapi properties
MapiPropertyCollection namedProperties = msg.NamedProperties;
MapiNamedPropertyMappingStorage mappingStorage = msg.NamedPropertyMapping;
Guid PS_MAPI = new Guid(“00020328-0000-0000-C000-000000000046”);
Guid PS_PUBLIC_STRINGS = new Guid(“00020329-0000-0000-C000-000000000046”);
// 1st property
MapiProperty numericProperty =
new MapiProperty(mappingStorage.GetNextAvailablePropertyId(MapiPropertyType.PT_BOOLEAN),
new byte[8] { 1, 0, 0, 0, 0, 0, 0, 0 });
msg.SetProperty(numericProperty);
int numericNameId = 667;
mappingStorage.AddNamedPropertyMapping(numericProperty, numericNameId, PS_MAPI);
// 2nd property
MapiProperty stringProperty =
new MapiProperty(mappingStorage.GetNextAvailablePropertyId(MapiPropertyType.PT_LONG),
Encoding.Unicode.GetBytes(“test value - hello 123”));
msg.SetProperty(stringProperty);
string stringNameId = “test property”;
mappingStorage.AddNamedPropertyMapping(stringProperty, stringNameId, PS_PUBLIC_STRINGS);
// Save the message and load again to read
// Get all named MAPI properties
MapiPropertyCollection properties = msg.NamedProperties;
// Read all properties in foreach loop
foreach (MapiNamedProperty mapiNamedProp in properties.Values)
{
// Read any specific property
switch (mapiNamedProp.NameId)
{
case “TEST”:
Console.WriteLine("{0} = {1}", mapiNamedProp.NameId, mapiNamedProp.GetString());
break;
case “test property”:
Console.WriteLine("{0} = {1}", mapiNamedProp.NameId, mapiNamedProp.GetString());
break;
default: break;
}
}
This does not work if you create a new MapiMessage from scratch.
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);
Also, it is a read-only storage.
How am I supposed to create a MapiMessage from scratch, and then set a custom property?
MapiMessage mapi = new MapiMessage();
MapiNamedPropertyMappingStorage mappingStorage = mapi.NamedPropertyMapping; // null
mapi.NamedPropertyMapping = new MapiNamedPropertyMappingStorage(); // error
// what do i do now?
I have finally successfully(?) created a custom property on a MapiMessage… then when I load the saved .msg back into a MapiMessage and call GetPropertyString(), my custom property is not there. Also, OutlookSpy does not show the property either…
MapiMessage mapi = MapiMessage.FromMailMessage(new MailMessage());
MapiProperty property = new MapiProperty(0x00000000F001001f, Encoding.Unicode.GetBytes(“this is a test property unicode string value”));
MapiNamedPropertyMappingStorage storage = mapi.NamedPropertyMapping;
storage.AddNamedPropertyMapping(property, “TestProperty”, new Guid());
mapi.SetProperty(property);
// … save …
// load into MapiMessage loaded;
string myprop = loaded.GetPropertyString(0x00000000F001001f);
// the variable myprop is null
Please tell me how to create a custom property, so I can read it back out with a my own tagid. The answer code posted above does not help us, because we are not iterating through a collection of named properties. We need to read a specific one by tagid.
Hi,
Thank you for considering Aspose.
I have worked with your scenario. Unfortunately, I am unable to set the Named Property for a newly created message. I am still looking into this, and if required I will log an investigation in our bug tracking system to probe further into this matter.
As a workaround, you may create a message in Outlook application, save it to disk with some name, load it in an instance of MapiMessage and set the Named Property for it. Please also check the source code as given below,
C#
var message = MapiMessage.FromFile(“blank.msg”);
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);
This workaround is working fine on my end and I hope it will help you out for the time being.
Regards,