Lavorare con le liste di distribuzione Outlook
Una lista di distribuzione raggruppa diversi destinatari sotto un unico nome, così un messaggio può essere indirizzato all’intero gruppo in una volta. Outlook conserva tali liste tra i contatti, e Aspose.Email per C++ le rappresenta con il MapiDistributionList classe. Questo articolo lavora con liste autonome - liste salvate come proprio file MSG o VCF. Le liste archiviate dentro un file PST sono trattate in Gestire le Liste di Distribuzione nei File PST.
Ogni voce di una lista è un MapiDistributionListMember, mantenuto in un MapiDistributionListMemberCollection. Un membro creato da nome e indirizzo è un membro una tantum: contiene i propri dettagli invece di puntare a un contatto in un archivio, che è ciò che richiede un file autonomo.
Gli esempi seguenti usano queste intestazioni e namespace:
#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;
Create a Distribution List and Save It
Il MapiDistributionList il costruttore prende il nome della lista e i suoi membri. Salva scrive la lista nel formato scelto tramite MapiDistributionListSaveOptions: ContactSaveFormat Msg produce un elemento Outlook, VCard un file VCF che altri rubrici possono importare.
// 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));
Read a Distribution List from an MSG File
Un file MSG è aperto con MapiMessage::Load qualunque cosa contenga. SupportedType segnala MapiItemType::DistList per una lista di distribuzione, e ToMapiMessageItem restituisce l’elemento che è cast a MapiDistributionList; il suo Members raccolta che elenca chi vi appartiene.
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">");
}
}
Un file VCF che contiene più contatti può essere trasformato anche in una lista di distribuzione - vedi Liste di distribuzione e file VCF multi-contatto.
Change the Members of a Saved List
Per modificare una lista salvata, leggi i suoi membri, costruisci la nuova collezione di membri da essi, crea una nuova MapiDistributionList con lo stesso nome, oggetto e corpo originali, e salvalo sul vecchio file. L’esempio rimuove un membro, ne aggiunge un altro e legge nuovamente il file per mostrare il risultato.
// 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">");
}
Vedi anche
- Gestire le Liste di Distribuzione nei File PST - crea, leggi e aggiorna le liste memorizzate in un PST, con membri collegati ai contatti.
- Crea, salva e carica contatti Outlook - gli elementi di contatto a cui una lista può fare riferimento.