Práce s distribučními seznamy Outlook

Distribuční seznam sdružuje několik příjemců pod jedním názvem, takže zpráva může být adresována celé skupině najednou. Outlook uchovává takové seznamy mezi kontakty a Aspose.Email pro C++ je představuje pomocí MapiDistributionList třída. Tento článek pracuje se samostatnými seznamy – seznamy uložené jako vlastní MSG nebo VCF soubor. Seznamy uložené uvnitř PST souboru jsou pokryty v Správa distribučních seznamů v souborech PST.

Každý záznam v seznamu je MapiDistributionListMember, uložený v MapiDistributionListMemberCollection. Člen vytvořený z jména a adresy je jednorázový člen: nese své vlastní údaje místo odkazu na kontakt v úložišti, což je to, co samostatný soubor potřebuje.

Níže uvedené ukázky používají tyto hlavičky a jmenné prostory:

#include <Mapi/MapiMessage.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiDistributionList.h>
#include <Mapi/MapiDistributionListMember.h>
#include <Mapi/MapiDistributionListMemberCollection.h>
#include <Mapi/MapiDistributionListSaveOptions.h>
#include <Mapi/ContactSaveFormat.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;

Vytvořit distribuční seznam a uložit jej

The MapiDistributionList konstruktor přijímá název seznamu a jeho členy. Uložit zapíše seznam ve formátu zvoleném přes MapiDistributionListSaveOptions: ContactSaveFormat Msg vytvoří Outlook položku, VCard VCF soubor, který mohou importovat jiné adresáře.

// members that exist only inside this list
SharedPtr<MapiDistributionListMemberCollection> oneOffMembers = MakeObject<MapiDistributionListMemberCollection>();
oneOffMembers->Add(MakeObject<MapiDistributionListMember>(u"John R. Patrick", u"JohnRPatrick@armyspy.com"));
oneOffMembers->Add(MakeObject<MapiDistributionListMember>(u"Tilly Bates", u"TillyBates@armyspy.com"));

SharedPtr<MapiDistributionList> list = MakeObject<MapiDistributionList>(u"Simple list", oneOffMembers);
list->set_Body(u"Distribution List Body");
list->set_Subject(u"Sample Distribution List");

// the same list in both formats
list->Save(u"dlist.msg", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::Msg));
list->Save(u"dlist.vcf", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::VCard));

Načíst distribuční seznam z MSG souboru

MSG soubor se otevírá pomocí MapiMessage::Load cokoliv obsahuje. SupportedType hlášení MapiItemType::DistList pro distribuční seznam a ToMapiMessageItem vrací položku, která je přetypována na MapiDistributionList; jeho Members sbírka seznamů, kdo do něj patří.

SharedPtr<MapiMessage> msg = MapiMessage::Load(u"dlist.msg");

if (msg->get_SupportedType() == MapiItemType::DistList)
{
    SharedPtr<MapiDistributionList> loaded = DynamicCast<MapiDistributionList>(msg->ToMapiMessageItem());
    Console::WriteLine(u"List: " + loaded->get_DisplayName());

    for (SharedPtr<MapiDistributionListMember> member : loaded->get_Members())
    {
        Console::WriteLine(u"   " + member->get_DisplayName() + u" <" + member->get_EmailAddress() + u">");
    }
}

VCF soubor, který obsahuje několik kontaktů, lze také převést na distribuční seznam – viz Distribuční seznamy a VCF soubory s více kontakty.

Změnit členy uloženého seznamu

Pro změnu uloženého seznamu načtěte jeho členy, sestavte novou kolekci členů z nich, vytvořte nový MapiDistributionList s původním názvem, předmětem a tělem a uloží je přes starý soubor. Ukázka odstraní jednoho člena, přidá další a znovu soubor načte, aby ukázala výsledek.

// a standalone list to start from
{
    SharedPtr<MapiDistributionListMemberCollection> members = MakeObject<MapiDistributionListMemberCollection>();
    members->Add(MakeObject<MapiDistributionListMember>(u"John R. Patrick", u"JohnRPatrick@armyspy.com"));
    members->Add(MakeObject<MapiDistributionListMember>(u"Tilly Bates", u"TillyBates@armyspy.com"));
    SharedPtr<MapiDistributionList> list = MakeObject<MapiDistributionList>(u"Simple list", members);
    list->set_Subject(u"Simple list");
    list->Save(u"team.msg", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::Msg));
}

SharedPtr<MapiDistributionList> stored = DynamicCast<MapiDistributionList>(MapiMessage::Load(u"team.msg")->ToMapiMessageItem());

// the new member list: everyone except John, plus Edward
SharedPtr<MapiDistributionListMemberCollection> members = MakeObject<MapiDistributionListMemberCollection>();
for (SharedPtr<MapiDistributionListMember> member : stored->get_Members())
{
    if (member->get_EmailAddress() != u"JohnRPatrick@armyspy.com")
    {
        members->Add(MakeObject<MapiDistributionListMember>(member->get_DisplayName(), member->get_EmailAddress()));
    }
}
members->Add(MakeObject<MapiDistributionListMember>(u"Edward R. Manuel", u"EdwardRManuel@armyspy.com"));

SharedPtr<MapiDistributionList> updated = MakeObject<MapiDistributionList>(stored->get_DisplayName(), members);
updated->set_Subject(stored->get_Subject());
updated->set_Body(stored->get_Body());
updated->Save(u"team.msg", MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::Msg));

// read the file again
SharedPtr<MapiDistributionList> reloaded = DynamicCast<MapiDistributionList>(MapiMessage::Load(u"team.msg")->ToMapiMessageItem());
for (SharedPtr<MapiDistributionListMember> member : reloaded->get_Members())
{
    Console::WriteLine(u"   " + member->get_DisplayName() + u" <" + member->get_EmailAddress() + u">");
}

Viz také