การจัดการรายการจัดส่งในไฟล์ PST

รายชื่อแจกจ่าย Outlook คือรายการในโฟลเดอร์ผู้ติดต่อที่รวมผู้รับหลายคนภายใต้ชื่อหนึ่ง ในไฟล์ PST อยู่ในโฟลเดอร์ Contacts ร่วมกับผู้ติดต่อที่อ้างอิงถึง และ Aspose.Email สำหรับ C++ แทนที่ด้วย MapiDistributionList คลาส บทความนี้แสดงวิธีสร้างรายการดังกล่าวใน PST, อ่านรายการที่ที่เก็บมี, และเปลี่ยนสมาชิกของพวกมัน รายการที่บันทึกเป็นไฟล์ MSG หรือ VCF แยกต่างหากครอบคลุมใน ทำงานกับรายการแจกจ่าย.

สมาชิกรายการคือ MapiDistributionListMember. ส่วน EntryIdType ระบุประเภทของสมาชิก:

| MapiDistributionListEntryIdType | สมาชิกคือ | | — | — | | Contact | ลิงก์ไปยังรายการผู้ติดต่อ; EntryId เก็บ Entry 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 เก็บรายการและคืนค่า Entry 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 ร่วมกัน 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 ของมันไว้ ดังนั้นโฟลเดอร์จะยังคงมีรายการเดียวแทนที่จะเป็นสำเนาเก่าและใหม่

สมาชิกแต่ละคนที่ถ่ายโอนมาจะถูกสร้างใหม่จากชื่อและที่อยู่; สมาชิกที่แทนผู้ติดต่อจะได้รับ 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();

ดูเพิ่มเติม