Working with MAPI Properties

Setting and Accessing Outlook MAPI Properties

Aspose.Email for Java provides the MapiProperty class that represents a MAPI property:

  • Name: the property name.
  • Tag: the property tag.
  • Data: the property data.

This topic also discusses how to set and access an Outlook Message (MSG) file MAPI properties using Aspose.Email for Java. In addition, a sample code has been provided about how to remove properties from MSGs and Attachments.

Read Properties

To read data of MAPI properties from an MSG file:

  1. Create an instance of the MapiMessage class to load an MSG file using the Load() static method.
  2. Set a reference to the MapiMessage object getProperties() method to get the MapiPropertyCollection.
  3. Get the MapiProperty object from the MapiPropertyCollection by the MapiPropertyTag keys.
  4. Get the property data using an appropriate getXXX() method.  
    // For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
    //Instantiate an MSG file to load an MSG file from disk
    MapiMessage outlookMessageFile = MapiMessage.fromFile(dataDir + "messageMapi.msg");
    //Get the MapiProperties collection
    MapiPropertyCollection coll = outlookMessageFile.getProperties();
    //Access the MapiPropertyTag.PR_SUBJECT property
    MapiProperty prop = (MapiProperty) coll.get_Item((Object) MapiPropertyTag.PR_SUBJECT);
    //If the MapiProperty is not found, check the MapiProperty.PR_SUBJECT_W
    //which is a unicode peer of MapiPropertyTag.PR_SUBJECT
    if (prop == null) {
    prop = (MapiProperty) coll.get_Item(MapiPropertyTag.PR_SUBJECT_W);
    }
    //If it cannot be found
    if (prop == null) {
    System.out.println("Mapi property could not be found.");
    } else {
    //Get the property data as string
    String strSubject = prop.getString();
    System.out.println("Subject: " + strSubject);
    }
    //Read internet code page property
    prop = (MapiProperty) coll.get_Item(MapiPropertyTag.PR_INTERNET_CPID);
    if (prop != null) {
    System.out.println("Code page: " + prop.getLong());
    }

Set Additional Properties

The following code sample can be used to set additional properties of an Outlook MapiMessage.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// PT_MV_FLOAT, PT_MV_R4, mv.float
MapiMessage msg = new MapiMessage();
IList values = (IList) new ArrayList();
values.addItem((float) 1);
values.addItem((float) 2);
msg.setProperty(new MapiProperty(0x23901004, values));
//PT_MV_DOUBLE, PT_MV_R8
values = (IList) new ArrayList();
values.addItem((double) 1);
values.addItem((double) 2);
msg.setProperty(new MapiProperty(0x23901005, values));
//PT_MV_APPTIME
values = (IList) new ArrayList();
values.addItem(30456.34);
values.addItem(40655.45);
msg.setProperty(new MapiProperty(0x23901007, values));
//PT_MV_I8, PT_MV_LONGLONG
values = (IList) new ArrayList();
values.addItem((long) 30456);
values.addItem((long) 40655);
msg.setProperty(new MapiProperty(0x23901014, values));
//PT_MV_SHORT, PT_MV_I2, mv.i2
values = (IList) new ArrayList();
values.addItem((short) 1);
values.addItem((short) 2);
msg.setProperty(new MapiProperty(0x23901002, values));
//PT_MV_BOOLEAN
values = (IList) new ArrayList();
values.addItem(true);
values.addItem(false);
msg.setProperty(new MapiProperty(0x2390100b, values));
//PT_NULL
msg.setProperty(new MapiProperty(0x67400001, new byte[1]));
//PT_MV_LONG
values = (IList) new ArrayList();
values.addItem((int) 4);
UUID uuid = UUID.randomUUID();
MapiProperty property = new MapiProperty(msg.getNamedPropertyMapping().getNextAvailablePropertyId(MapiPropertyType.PT_MV_LONG), values);
msg.getNamedPropertyMapping().addNamedPropertyMapping(property, 0x00008028, uuid);
msg.setProperty(property);
//OR you can set the custom property (with the custom name)
MapiMessage message = new MapiMessage("sender@test.com", "recipient@test.com", "subj", "Body of test msg");
values = (IList) new ArrayList();
values.addItem((int) 4);
property = new MapiProperty(message.getNamedPropertyMapping().getNextAvailablePropertyId(MapiPropertyType.PT_MV_LONG), values);
message.addCustomProperty(property, "customProperty");
//PT_FLOAT
//Please note that you need explicit cast to float value for this to work
float floatValue = 123.456F;
MapiMessage newMsg = new MapiMessage();
long floatTag = newMsg.getNamedPropertyMapping().getNextAvailablePropertyId(MapiPropertyType.PT_FLOAT);
UUID guid = UUID.randomUUID();
MapiProperty newMapiProperty = new MapiProperty(floatTag, BitConverter.getBytesSingle(floatValue));
newMsg.getNamedPropertyMapping().addNamedPropertyMapping(newMapiProperty,(long) 12, guid);
newMsg.setProperty(newMapiProperty);
boolean propertyIsOk = false;
for (MapiNamedProperty prop : (Iterable<MapiNamedProperty>) newMsg.getNamedProperties().getValues())
{
if (prop.getGuid().equals(guid))
{
float val = prop.getFloat();
propertyIsOk = val == floatValue;
}
}

Remove Properties

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MapiMessage mapi = new MapiMessage("from@doamin.com", "to@domain.com", "subject", "body");
mapi.setBodyContent("<html><body><h1>This is the body content</h1></body></html>", BodyContentType.Html);
MapiMessage attachment = MapiMessage.fromFile(dataDir + "message.msg");
mapi.getAttachments().add(dataDir + "Outlook2 Test subject.msg", attachment);
System.out.println("Before removal = " + mapi.getAttachments().get_Item(mapi.getAttachments().size() - 1).getProperties().size());
mapi.getAttachments().get_Item(mapi.getAttachments().size() - 1).removeProperty(923467779);//Delete anyone property
System.out.println("After removal = " + mapi.getAttachments().get_Item(mapi.getAttachments().size() - 1).getProperties().size());
mapi.save(dataDir + "EMAIL_589265.msg");
MapiMessage mapi2 = MapiMessage.fromFile(dataDir + "EMAIL_589265.msg");
System.out.println("Reloaded = " + mapi2.getAttachments().get_Item(mapi2.getAttachments().size() - 1).getProperties().size());

Reading Named Mapi Properties from Email Messages

Microsoft Outlook supports adding named MAPI properties to an MSG file. These properties are added by the user. Developers can add a named property, for example “MyProp”, to an MSG file using Aspose.Email.

This article illustrates Aspose.Email MapiMessage getNamedProperties() collection to read named MAPI properties from an MSG file.

Read Named MAPI Property

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Load MSG file
MapiMessage message = MapiMessage.fromFile(dataDir + "message.msg");
// Get all named MAPI properties
MapiPropertyCollection properties = message.getNamedProperties();
// Read all properties in for loop
for (MapiNamedProperty mapiNamedProp : (Iterable<MapiNamedProperty>) properties.getValues()) {
// Read any specific property
switch (mapiNamedProp.getNameId()) {
case "TEST":
System.out.println(mapiNamedProp.getNameId() + " equals " + mapiNamedProp.getString());
break;
case "MYPROP":
System.out.println(mapiNamedProp.getNameId() + " equals " + mapiNamedProp.getString());
break;
default:
break;
}
}

Read Named Mapi Property from Attachment

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
MailMessage mail = MailMessage.load(dataDir + "test.eml");
MapiMessage mapi = MapiMessage.fromMailMessage(mail);
for (MapiNamedProperty namedProperty : (Iterable<MapiNamedProperty>) mapi.getAttachments().get_Item(0).getNamedProperties().getValues()) {
if (namedProperty.getNameId().equalsIgnoreCase("CustomAttGuid")) {
System.out.println("Equal..");
}
}