Access and Manage Outlook MAPI Properties
MAPI properties are metadata items used in Microsoft Outlook messages, defining attributes such as sender, recipient, subject, attachments, and custom data.
Aspose.Email for C++ allows developers to access, modify, and remove these properties programmatically in MapiMessage objects, attachments, and named properties.
The MapiProperty class represents a MAPI property, which contains:
- Name – The property’s string identifier.
- Tag – A numeric identifier used to reference the property.
- Data – A byte array representing the property’s value.
The samples in this article use these headers and namespaces:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiMessageFlags.h>
#include <Mapi/BodyContentType.h>
#include <Mapi/MapiProperty.h>
#include <Mapi/MapiPropertyContainer.h>
#include <Mapi/MapiNamedProperty.h>
#include <Mapi/MapiNamedPropertyMappingStorage.h>
#include <Mapi/MapiPropertyCollection.h>
#include <Mapi/MapiPropertyTag.h>
#include <Mapi/MapiPropertyType.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/bit_converter.h>
#include <system/convert.h>
#include <system/guid.h>
#include <system/text/encoding.h>
using namespace System;
using namespace Aspose::Email::Mapi;
Read MAPI Properties
Every property of a message is kept in the collection returned by get_Properties, indexed by its tag; the MapiPropertyTag class names the standard tags. Looking up a tag that the message does not have returns nullptr.
Many text properties exist in two flavours - an ANSI tag such as PR_SUBJECT and a Unicode one such as PR_SUBJECT_W - and a message carries one or the other depending on the format it was saved in. The sample tries both. A MapiProperty then converts its raw data with methods such as GetString or GetInt32.
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"Message.msg");
SharedPtr<MapiProperty> prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_SUBJECT);
if (prop == nullptr)
{
// the Unicode counterpart of PR_SUBJECT
prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_SUBJECT_W);
}
if (prop != nullptr)
{
Console::WriteLine(u"Subject: " + prop->GetString());
}
// the code page of the internet message the item was created from
SharedPtr<MapiProperty> codePage = msg->get_Properties()->idx_get(MapiPropertyTag::PR_INTERNET_CPID);
if (codePage != nullptr)
{
Console::WriteLine(u"Code page: " + Convert::ToString(codePage->GetInt32()));
}
Set MAPI Properties
A property is added or replaced with SetProperty, which takes a MapiProperty built from a tag and the raw bytes of the value. Strings are stored in UTF-16 for the Unicode (_W) tags, and a date is stored as a FILETIME - the number of 100-nanosecond intervals since 1 January 1601 (UTC) - which DateTime::ToFileTimeUtc produces. Recipients carry properties of their own, set the same way on a MapiRecipient.
In C++, MapiMessage declares SetProperty overloads of its own, and these hide the general overload of its base class MapiPropertyContainer. The sample therefore calls it through a MapiPropertyContainer pointer to the message.
The sample turns the recipient of a new message into a fax address, marks the message as an unsent message from the current user and sets its modification time.
SharedPtr<MapiMessage> mapiMsg = MakeObject<MapiMessage>(u"user1@gmail.com", u"user2@gmail.com", u"This is subject", u"This is body");
// the general SetProperty overload is declared on the base class
SharedPtr<MapiPropertyContainer> container = mapiMsg;
container->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_SENDER_ADDRTYPE_W, Text::Encoding::get_Unicode()->GetBytes(u"EX")));
// properties of a recipient
SharedPtr<MapiRecipient> recipientTo = mapiMsg->get_Recipients()->idx_get(0);
recipientTo->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_RECEIVED_BY_ADDRTYPE_W, Text::Encoding::get_Unicode()->GetBytes(u"MYFAX")));
recipientTo->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_RECEIVED_BY_EMAIL_ADDRESS_W,
Text::Encoding::get_Unicode()->GetBytes(u"My Fax User@/FN=fax#/VN=voice#/CO=My Company/CI=Local")));
mapiMsg->SetMessageFlags(MapiMessageFlags::MSGFLAG_UNSENT | MapiMessageFlags::MSGFLAG_FROMME);
container->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_RTF_IN_SYNC, BitConverter::GetBytes(static_cast<int64_t>(1))));
// a date property holds a FILETIME value
DateTime modified(2013, 9, 11, 0, 0, 0, DateTimeKind::Utc);
container->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_LAST_MODIFICATION_TIME, BitConverter::GetBytes(modified.ToFileTimeUtc())));
mapiMsg->Save(u"MapiProperties.msg");
SharedPtr<MapiMessage> saved = MapiMessage::Load(u"MapiProperties.msg");
Console::WriteLine(u"Sender address type: " + saved->get_Properties()->idx_get(MapiPropertyTag::PR_SENDER_ADDRTYPE_W)->GetString());
Console::WriteLine(u"Modification time: " + saved->get_Properties()->idx_get(MapiPropertyTag::PR_LAST_MODIFICATION_TIME)->GetDateTime().ToString());
Read Named MAPI Properties
Named MAPI properties are custom properties added by users or applications. Instead of a fixed tag, each one is identified by a property set GUID together with a name or a numeric identifier, so properties of different applications never collide. The tag they are stored under is assigned when the property is added to the message.
Aspose.Email allows reading these properties from messages and attachments.
Reading Named MAPI Properties from MSG Files
get_NamedProperties returns the named properties of a message. Each value of the collection is a MapiNamedProperty, whose get_NameId gives the name the property was created with.
To have something to read, the sample first stores two named properties. A named property is created in three steps: GetNextAvailablePropertyId of the message’s NamedPropertyMapping supplies a free tag for the value type, AddNamedPropertyMapping binds that tag to the name and the property set GUID, and SetProperty stores the value.
// the property set the custom properties belong to
Guid propertySet = Guid(u"6ED8DA90-450B-101B-98DA-00AA003F1305");
SharedPtr<MapiMessage> source = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"Named properties", u"Body");
SharedPtr<MapiPropertyContainer> container = source;
for (String name : { String(u"TEST"), String(u"MYPROP") })
{
SharedPtr<MapiProperty> property = MakeObject<MapiProperty>(
source->get_NamedPropertyMapping()->GetNextAvailablePropertyId(MapiPropertyType::PT_UNICODE),
Text::Encoding::get_Unicode()->GetBytes(u"value of " + name));
source->get_NamedPropertyMapping()->AddNamedPropertyMapping(property, name, propertySet);
container->SetProperty(property);
}
source->Save(u"NamedProperties.msg");
// read the named properties back
SharedPtr<MapiMessage> message = MapiMessage::Load(u"NamedProperties.msg");
for (auto entry : message->get_NamedProperties())
{
// DynamicCast_noexcept gives nullptr for a value that is not a named property
SharedPtr<MapiNamedProperty> namedProperty = DynamicCast_noexcept<MapiNamedProperty>(entry.get_Value());
if (namedProperty == nullptr)
{
continue;
}
String name = namedProperty->get_NameId();
if (name == u"TEST" || name == u"MYPROP")
{
Console::WriteLine(name + u" = " + namedProperty->GetString());
}
}
Accessing Named MAPI Properties in Attachments
Attachments have named properties too - an application can, for instance, tag every file it attached with its own identifier. The names of all named properties of a message, including those of its attachments, are kept in the mapping of the message, so the property is registered there and then set on the MapiAttachment. The get_NamedProperties collection of the attachment returns it.
Guid propertySet = Guid(u"6ED8DA90-450B-101B-98DA-00AA003F1305");
SharedPtr<MapiMessage> source = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"Attachment properties", u"Body");
source->get_Attachments()->Add(u"report.txt", Text::Encoding::get_UTF8()->GetBytes(u"Report text"));
SharedPtr<MapiProperty> property = MakeObject<MapiProperty>(
source->get_NamedPropertyMapping()->GetNextAvailablePropertyId(MapiPropertyType::PT_UNICODE),
Text::Encoding::get_Unicode()->GetBytes(u"{1B2C3D4E-0000-0000-0000-000000000001}"));
source->get_NamedPropertyMapping()->AddNamedPropertyMapping(property, String(u"CustomAttGuid"), propertySet);
source->get_Attachments()->idx_get(0)->SetProperty(property);
source->Save(u"AttachmentNamedProperty.msg");
SharedPtr<MapiMessage> mapi = MapiMessage::Load(u"AttachmentNamedProperty.msg");
for (auto entry : mapi->get_Attachments()->idx_get(0)->get_NamedProperties())
{
SharedPtr<MapiNamedProperty> namedProperty = DynamicCast_noexcept<MapiNamedProperty>(entry.get_Value());
if (namedProperty != nullptr && String::Compare(namedProperty->get_NameId(), u"CustomAttGuid", StringComparison::OrdinalIgnoreCase) == 0)
{
Console::WriteLine(u"CustomAttGuid = " + namedProperty->GetString());
}
}
Removing MAPI Properties
RemoveProperty deletes a property by its tag - from a message as well as from an attachment or a recipient. The sample attaches an Outlook message to a new message, removes the PR_RENDERING_POSITION property of that attachment and checks that the property stays removed after the message is saved and loaded again. Saving can add other properties to the attachment, so the check looks for the tag itself rather than at the number of properties.
SharedPtr<MapiMessage> mapi = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"subject", u"body");
mapi->SetBodyContent(u"<html><body><h1>This is the body content</h1></body></html>", BodyContentType::Html);
mapi->get_Attachments()->Add(u"Attached message.msg", MapiMessage::Load(u"Message.msg"));
SharedPtr<MapiAttachment> attachment = mapi->get_Attachments()->idx_get(mapi->get_Attachments()->get_Count() - 1);
Console::WriteLine(u"Before removal: " + Convert::ToString(attachment->get_Properties()->ContainsKey(MapiPropertyTag::PR_RENDERING_POSITION)));
attachment->RemoveProperty(MapiPropertyTag::PR_RENDERING_POSITION);
Console::WriteLine(u"After removal: " + Convert::ToString(attachment->get_Properties()->ContainsKey(MapiPropertyTag::PR_RENDERING_POSITION)));
mapi->Save(u"PropertyRemoved.msg");
SharedPtr<MapiMessage> reloaded = MapiMessage::Load(u"PropertyRemoved.msg");
SharedPtr<MapiAttachment> reloadedAttachment = reloaded->get_Attachments()->idx_get(reloaded->get_Attachments()->get_Count() - 1);
Console::WriteLine(u"After reloading: " + Convert::ToString(reloadedAttachment->get_Properties()->ContainsKey(MapiPropertyTag::PR_RENDERING_POSITION)));