Exception: property id can't be used for mapping

I have a set of Named Properties that I’m trying to use and set against a MapiMessage. When I attempt certain tag values I get exceptions. In all the forum sample code most of the time you are using the method </font><font size="2"><font size="2">getNextAvailablePropertyId and this works every time but I have a set of tags already that I’m trying to reuse. Are there restrictions against specific values of tags that I can set?

The sample code below demonstrates what my basic test looks like:

// Create a sample mapi message
MapiMessage msg = MapiMessage.fromMailMessage(new MailMessage());

// Get all named MAPI properties
MapiNamedPropertyMappingStorage mappingStorage = msg.getNamedPropertyMapping();
UUID PS_MAPI = UUID.fromString(“00020328-0000-0000-C000-000000000046”);

//Set property 1 – String – this one works
{
// Create a named property
long tag = mappingStorage.getNextAvailablePropertyId(MapiPropertyType.PT_STRING8);
MapiProperty stringProperty = new MapiProperty(tag, “IPM.Note”.getBytes());

// Set message property
msg.setProperty(stringProperty);
long propName = 26;
mappingStorage.addNamedPropertyMapping(stringProperty, propName, PS_MAPI);
}

//Set property 4 – Existing Property
{
// Create a named property
long tag = Long.decode(“0x00000000001A001E”).longValue();
MapiProperty stringProperty = new MapiProperty(tag, “test”.getBytes());

// Set message property
msg.setProperty(stringProperty);
long propName = 35;

// The below line throws an exception
mappingStorage.addNamedPropertyMapping(stringProperty, propName, PS_MAPI);
}


Thanks,
//Christopher

Solved my problem.

Since existing Named Property Mappings already exist within the system, you cannot “add” them. Instead you set them. The above code snippet would be augmented with an additional check to determine if the property already exists. If it does you set it, if it doesn’t then you add it:

{
// Create a named property
long tag = Long.decode(“0x000000000037001E”).longValue();
//long tag = mappingStorage.getNextAvailablePropertyId(MapiPropertyType.PT_STRING8);
MapiProperty stringProperty = new MapiProperty(tag, “test bitches”.getBytes());

if(msg.getProperties().get_Item(tag) != null) {
msg.setProperty(stringProperty);
} else {
// Set message property
msg.setProperty(stringProperty);
long propName = 35;
mappingStorage.addNamedPropertyMapping(stringProperty, propName, PS_MAPI);
}
}

Hi Christopher,


Thank you for contacting Aspose support and we are glad to know that your issue is resolved. Please feel free to write us back if you have any other query related to Aspose.Email.