在 PST 文件中管理分发列表
Outlook 通讯组列表是联系人文件夹中的一项,用于将多个收件人归为一个名称。在 PST 文件中,它位于联系人文件夹中,与其引用的联系人并列,Aspose.Email for C++ 使用 MapiDistributionList 类。本文展示了如何在 PST 中创建此类列表,读取存储包含的列表,并更改其成员。以独立 MSG 或 VCF 文件保存的列表在 处理通讯组列表.
列表成员是一个 MapiDistributionListMember。它的 EntryIdType 指明成员的类型:
| MapiDistributionListEntryIdType | 成员是 | | — | — | | Contact | 链接到联系人项; EntryId 保存该联系人的条目 ID,因此 Outlook 显示该联系人的当前详细信息 | | OneOff | 使用名称和地址存储在列表本身的成员,没有对应的联系人——这是仅使用名称和地址创建的成员的默认方式 | | DistributionList | 另一个嵌套在此列表中的通讯组列表 |
本文中的示例使用了以下头文件和名称空间:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactNamePropertySet.h>
#include <Mapi/MapiContactElectronicAddress.h>
#include <Mapi/MapiContactElectronicAddressPropertySet.h>
#include <Mapi/MapiDistributionList.h>
#include <Mapi/MapiDistributionListMember.h>
#include <Mapi/MapiDistributionListMemberCollection.h>
#include <Mapi/MapiDistributionListEntryIdType.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/MessageInfo.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Pst;
在 PST 中创建通讯组列表
要将成员链接到联系人,必须先在存储中存在这些联系人。 FolderInfo::AddMapiMessageItem 存储项目并返回其条目 ID(Base64 字符串); Convert::FromBase64String 将其转换为成员期望的字节。仅使用姓名和地址创建的成员不需要此步骤——它将成为一次性成员。
完成的成员集合将传递给 MapiDistributionList 使用构造函数并提供列表名称,列表将存储在同一联系人文件夹中。
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"lists.pst", FileFormatVersion::Unicode);
SharedPtr<FolderInfo> contacts = pst->CreatePredefinedFolder(u"Contacts", StandardIpmFolder::Contacts);
// the contacts the list will point to; AddMapiMessageItem returns the entry ID of each stored item
SharedPtr<MapiContact> susanne = MakeObject<MapiContact>(u"Susanne Nielsen", u"SusanneNielsen@example.com");
SharedPtr<MapiContact> lars = MakeObject<MapiContact>(u"Lars Rosing", u"LarsRosing@example.com");
String susanneId = contacts->AddMapiMessageItem(susanne);
String larsId = contacts->AddMapiMessageItem(lars);
// members linked to those contacts: the entry ID string is Base64, the member needs the raw bytes
SharedPtr<MapiDistributionListMember> first = MakeObject<MapiDistributionListMember>(
susanne->get_NameInfo()->get_DisplayName(), susanne->get_ElectronicAddresses()->get_Email1()->get_EmailAddress());
first->set_EntryIdType(MapiDistributionListEntryIdType::Contact);
first->set_EntryId(Convert::FromBase64String(susanneId));
SharedPtr<MapiDistributionListMember> second = MakeObject<MapiDistributionListMember>(
lars->get_NameInfo()->get_DisplayName(), lars->get_ElectronicAddresses()->get_Email1()->get_EmailAddress());
second->set_EntryIdType(MapiDistributionListEntryIdType::Contact);
second->set_EntryId(Convert::FromBase64String(larsId));
// a one-off member: just a name and an address, with no contact behind it
SharedPtr<MapiDistributionListMember> external = MakeObject<MapiDistributionListMember>(
u"Tilly Bates", u"TillyBates@example.com");
SharedPtr<MapiDistributionListMemberCollection> members = MakeObject<MapiDistributionListMemberCollection>();
members->Add(first);
members->Add(second);
members->Add(external);
SharedPtr<MapiDistributionList> list = MakeObject<MapiDistributionList>(u"Project team", members);
list->set_Body(u"Everyone working on the project");
list->set_Subject(u"Project team");
contacts->AddMapiMessageItem(list);
Console::WriteLine(u"Items in the Contacts folder: " + Convert::ToString(contacts->get_ContentCount()));
pst->Dispose();
读取 PST 中的通讯组列表
联系人和通讯组共享联系人文件夹。该 MessageClass 列表的 IPM.DistList,可在轻量级 MessageInfo 在提取项目之前。 ToMapiMessageItem 随后返回该对象并强制转换为 MapiDistributionList,以及它的 成员 集合列出其成员。
SharedPtr<PersonalStorage> pst = PersonalStorage::FromFile(u"lists.pst", false);
SharedPtr<FolderInfo> contacts = pst->GetPredefinedFolder(StandardIpmFolder::Contacts);
for (SharedPtr<MessageInfo> messageInfo : contacts->EnumerateMessages())
{
// contacts and distribution lists share the folder; the message class tells them apart
if (messageInfo->get_MessageClass() != u"IPM.DistList")
{
continue;
}
SharedPtr<MapiDistributionList> list =
System::DynamicCast<MapiDistributionList>(pst->ExtractMessage(messageInfo)->ToMapiMessageItem());
Console::WriteLine(u"List: " + list->get_DisplayName());
for (SharedPtr<MapiDistributionListMember> member : list->get_Members())
{
String kind = member->get_EntryIdType() == MapiDistributionListEntryIdType::Contact
? String(u"contact") : String(u"one-off");
Console::WriteLine(u" " + member->get_DisplayName() + u" <" + member->get_EmailAddress() + u">, " + kind);
}
}
pst->Dispose();
对于上面创建的列表,输出显示 Susanne Nielsen 和 Lars Rosing 为联系人成员,Tilly Bates 为一次性成员。
更新通讯组列表
写回已存储的项目。 MapiDistributionList 从中读取,然后使用 FolderInfo::UpdateMessage。列表保留其条目 ID,因此文件夹仍然只包含一个列表,而不是旧的和新的副本。
每个被保留下来的成员都会根据其姓名和地址重新创建;代表联系人的成员还会恢复联系人的条目 ID,从而在更新后链接仍然有效。示例删除一个成员,添加另一个成员,并再次读取存储的列表以展示结果。
SharedPtr<PersonalStorage> pst = PersonalStorage::FromFile(u"lists.pst", true);
SharedPtr<FolderInfo> contacts = pst->GetPredefinedFolder(StandardIpmFolder::Contacts);
for (SharedPtr<MessageInfo> messageInfo : contacts->EnumerateMessages())
{
if (messageInfo->get_MessageClass() != u"IPM.DistList")
{
continue;
}
SharedPtr<MapiDistributionList> stored =
System::DynamicCast<MapiDistributionList>(pst->ExtractMessage(messageInfo)->ToMapiMessageItem());
// the new member list: everyone except Lars, plus Edward
SharedPtr<MapiDistributionListMemberCollection> members = MakeObject<MapiDistributionListMemberCollection>();
for (SharedPtr<MapiDistributionListMember> member : stored->get_Members())
{
if (member->get_EmailAddress() == u"LarsRosing@example.com")
{
continue;
}
SharedPtr<MapiDistributionListMember> copy =
MakeObject<MapiDistributionListMember>(member->get_DisplayName(), member->get_EmailAddress());
if (member->get_EntryIdType() == MapiDistributionListEntryIdType::Contact)
{
// keep the link to the contact item
copy->set_EntryIdType(MapiDistributionListEntryIdType::Contact);
copy->set_EntryId(member->get_EntryId());
}
members->Add(copy);
}
members->Add(MakeObject<MapiDistributionListMember>(u"Edward R. Manuel", u"EdwardRManuel@example.com"));
SharedPtr<MapiDistributionList> updated = MakeObject<MapiDistributionList>(stored->get_DisplayName(), members);
updated->set_Subject(stored->get_Subject());
updated->set_Body(stored->get_Body());
// replace the stored list, keeping its entry ID
contacts->UpdateMessage(messageInfo->get_EntryIdString(), updated);
}
pst->Dispose();
// check the stored result
SharedPtr<PersonalStorage> check = PersonalStorage::FromFile(u"lists.pst", false);
SharedPtr<FolderInfo> folder = check->GetPredefinedFolder(StandardIpmFolder::Contacts);
for (SharedPtr<MessageInfo> messageInfo : folder->EnumerateMessages())
{
if (messageInfo->get_MessageClass() == u"IPM.DistList")
{
SharedPtr<MapiDistributionList> list =
System::DynamicCast<MapiDistributionList>(check->ExtractMessage(messageInfo)->ToMapiMessageItem());
for (SharedPtr<MapiDistributionListMember> member : list->get_Members())
{
Console::WriteLine(u" " + member->get_DisplayName() + u" <" + member->get_EmailAddress() + u">");
}
}
}
Console::WriteLine(u"Items in the Contacts folder: " + Convert::ToString(folder->get_ContentCount()));
check->Dispose();
另见
- 处理通讯组列表 - MSG 和 VCF 文件中的独立通讯组列表。
- 在 PST 文件中处理联系人 - 通讯组指向的联系人。