访问和管理 Outlook MAPI 属性
MAPI properties 是在 Microsoft Outlook 消息中使用的元数据项,定义诸如发件人、收件人、主题、附件和自定义数据等属性。
Aspose.Email for C++ 允许开发者以编程方式访问、修改和删除这些属性。 MapiMessage 对象、附件和具名属性。
该 MapiProperty 类表示一个 MAPI 属性,包含:
- Name – 属性的字符串标识符。
- Tag – 用于引用属性的数字标识符。
- Data – 表示属性值的字节数组。
本文中的示例使用了以下头文件和名称空间:
#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;
读取 MAPI 属性
消息的每个属性都保存在 获取_属性,按其标签索引;该 MapiPropertyTag 类为标准标签命名。查找消息不具备的标签会返回 nullptr.
许多文本属性都有两种形式——ANSI 标签,例如 PR_SUBJECT 以及一个 Unicode 版本,例如 PR_SUBJECT_W - 的方法将原始数据转换。消息根据保存的格式携带其中一种。示例尝试了两者。一个 MapiProperty 随后使用诸如 GetString 或 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()));
}
设置 MAPI 属性
使用以下方式添加或替换属性: SetProperty),这需要一个 MapiProperty 由标签和原始字节值构建。字符串以 UTF-16 存储用于 Unicode(_W) 标记中存储,日期以 FILETIME 存储——自 1601 年 1 月 1 日(UTC)起的 100 纳秒间隔数——它 DateTime::ToFileTimeUtc 产生。收件人携带自己的属性,以相同方式在 MapiRecipient.
在 C++ 中, MapiMessage 声明 SetProperty 它自己的重载,这些隐藏了基类的通用重载 MapiPropertyContainer。因此示例通过 MapiPropertyContainer 指向该消息的指针。
示例将新邮件的收件人设为传真地址,将邮件标记为当前用户的未发送邮件,并设置其修改时间。
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());
读取已命名的 MAPI 属性
命名 MAPI 属性是用户或应用程序添加的自定义属性。每个属性不是使用固定标签,而是通过属性集 GUID 加上名称或数字标识符来标识,从而不同应用的属性永不冲突。属性在添加到消息时会分配存储标签。
Aspose.Email 允许从消息和附件中读取这些属性。
从 MSG 文件读取具名 MAPI 属性
get_NamedProperties 返回消息的命名属性。集合的每个值是一个 MapiNamedProperty,其 获取_NameId 返回创建属性时使用的名称。
为了有可读取的内容,示例首先存储两个命名属性。命名属性通过三个步骤创建: 获取下一个可用属性ID 消息的 命名属性映射 为该值类型提供一个空闲标签, AddNamedPropertyMapping 将该标签绑定到名称和属性集 GUID,并 SetProperty 存储该值。
// 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());
}
}
访问附件中的具名 MAPI 属性
附件也有命名属性——应用程序可以为每个附加的文件标记自己的标识符。消息的所有命名属性(包括附件的)都保存在消息的映射中,因此属性在那里注册后再设置到 MapiAttachment。该 get_NamedProperties 附件的集合返回它。
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());
}
}
删除 MAPI 属性
RemoveProperty 通过标签删除属性——无论是消息、附件还是收件人。示例将 Outlook 消息附加到新消息,随后删除 PR_RENDERING_POSITION 该附件的属性报告,并检查在保存并重新加载消息后属性仍被删除。保存可能会向附件添加其他属性,因此检查关注的是标签本身而不是属性数量。
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)));