PST ファイルでの配布リスト管理

Outlook の配布リストは、複数の受信者を 1 つの名前でまとめる連絡先フォルダー項目です。PST ファイルでは、参照先の連絡先と同じ Contacts フォルダーに存在し、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 コンストラクターにリスト名を渡すと、同じ Contacts フォルダーにリストが保存されます。

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 の配布リストを読む

連絡先と配布リストは Contacts フォルダーを共有します。The MessageClass リストの IPM.DistList、軽量版で確認できます。 MessageInfo 項目が抽出される前に。 ToMapiMessageItem はオブジェクトにキャストされたものを返します。 MapiDistributionList、そしてその Members そのコレクションリストは誰が所属しているかを示します。

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 を保持するため、フォルダーは古いコピーと新しいコピーの両方ではなく、1 つのリストだけを保持します。

引き継がれる各メンバーは名前とアドレスから新たに作成されます。連絡先を表すメンバーは連絡先のエントリ ID も取得するため、リンクは更新後も残ります。サンプルは 1 人のメンバーを削除し、別のメンバーを追加し、結果を示すために保存されたリストを再度読み取ります。

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();

関連項目