Zarządzanie listami dystrybucyjnymi w plikach PST

Lista dystrybucyjna Outlook to element folderu kontaktów, który grupuje kilku odbiorców pod jedną nazwą. W pliku PST znajduje się w folderze Kontakty obok kontaktów, do których się odnosi, a Aspose.Email dla C++ reprezentuje ją jako MapiDistributionList klasą. Ten artykuł pokazuje, jak utworzyć taką listę wewnątrz PST, odczytać listy przechowywane oraz zmienić ich członków. Listy zapisane jako samodzielne pliki MSG lub VCF są opisane w Praca z listami dystrybucyjnymi.

Członek listy jest MapiDistributionListMember. Jej EntryIdType określa, jakiego rodzaju członkiem jest:

| MapiDistributionListEntryIdType | Członek jest | | — | — | | Contact | odnośnik do elementu kontaktu; EntryId przechowuje identyfikator wpisu tego kontaktu, więc Outlook wyświetla aktualne szczegóły kontaktu | | OneOff | nazwa i adres przechowywane w samej liście, bez kontaktu po ich stronie – domyślne dla członka utworzonego z nazwy i adresu | | DistributionList | inna lista dystrybucyjna, zagnieżdżona w tej |

Przykłady w tym artykule używają następujących nagłówków i przestrzeni nazw:

#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;

Utwórz listę dystrybucyjną w PST

Aby połączyć członków z kontaktami, kontakty muszą najpierw znajdować się w przechowywaniu. FolderInfo::AddMapiMessageItem przechowuje element i zwraca jego identyfikator wpisu jako ciąg Base64; Convert::FromBase64String przekształca ją w bajty, które członek oczekuje. Członek utworzony jedynie z nazwy i adresu nie wymaga takiego kroku – staje się jednorazowym członkiem.

Gotowa kolekcja członków jest przekazywana do MapiDistributionList konstruktor razem z nazwą listy, i lista jest przechowywana w tym samym folderze Kontakty.

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

Odczytaj listy dystrybucyjne z PST

Kontakty i listy dystrybucyjne współdzielą folder Kontakty. MessageClass listy jest IPM.DistList, które można sprawdzić w lekki MessageInfo przed wyciągnięciem elementu. ToMapiMessageItem następnie zwraca obiekt rzutowany na MapiDistributionList, i jej Members listy kolekcji, do których należy.

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

Dla listy utworzonej powyżej wyjściowe nazwy to Susanne Nielsen i Lars Rosing jako członkowie kontaktów oraz Tilly Bates jako jednorazowy członek.

Zaktualizuj listę dystrybucyjną

Aby zmienić członków przechowywanej listy, zbuduj nową kolekcję członków, utwórz MapiDistributionList z niej, i zapisz nad nią przechowywany element przy użyciu FolderInfo::UpdateMessage. Lista zachowuje swój identyfikator wpisu, więc folder nadal zawiera jedną listę, a nie starą i nową kopię.

Każdy przeniesiony członek jest tworzony od nowa z jego nazwy i adresu; członek reprezentujący kontakt otrzymuje również identyfikator wpisu kontaktu, więc połączenie przetrwa aktualizację. Przykład usuwa jednego członka, dodaje innego i ponownie odczytuje przechowywaną listę, aby pokazać wynik.

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

Zobacz także