Managing Distribution Lists in PST Files
An Outlook distribution list is a contact-folder item that groups several recipients under one name. In a PST file it lives in the Contacts folder next to the contacts it refers to, and Aspose.Email for C++ represents it with the MapiDistributionList class. This article shows how to create such a list inside a PST, read the lists a storage contains, and change their members. Lists saved as standalone MSG or VCF files are covered in Working with Distribution Lists.
A list member is a MapiDistributionListMember. Its EntryIdType says what kind of member it is:
| MapiDistributionListEntryIdType | The member is |
|---|---|
Contact |
a link to a contact item; EntryId holds the entry ID of that contact, so Outlook shows the current details of the contact |
OneOff |
a name and an address stored in the list itself, with no contact behind them - the default for a member created from a name and an address |
DistributionList |
another distribution list, nested in this one |
The samples in this article use these headers and namespaces:
#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;
Create a Distribution List in a PST
To link members to contacts, the contacts have to be in the storage first. FolderInfo::AddMapiMessageItem stores an item and returns its entry ID as a Base64 string; Convert::FromBase64String turns it into the bytes a member expects. A member created from just a name and an address needs no such step - it becomes a one-off member.
The finished member collection is passed to the MapiDistributionList constructor together with the name of the list, and the list is stored in the same Contacts folder.
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();
Read the Distribution Lists of a PST
Contacts and distribution lists share the Contacts folder. The MessageClass of a list is IPM.DistList, which can be checked on the lightweight MessageInfo before the item is extracted. ToMapiMessageItem then returns the object that is cast to MapiDistributionList, and its Members collection lists who belongs to it.
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();
For the list created above the output names Susanne Nielsen and Lars Rosing as contact members and Tilly Bates as a one-off member.
Update a Distribution List
To change the members of a stored list, build the new member collection, create a MapiDistributionList from it, and write it over the stored item with FolderInfo::UpdateMessage. The list keeps its entry ID, so the folder still holds one list rather than an old and a new copy.
Each member that is carried over is created anew from its name and address; a member that stands for a contact also gets the contact’s entry ID back, so the link survives the update. The sample removes one member, adds another, and reads the stored list again to show the result.
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();
See Also
- Working with Distribution Lists - standalone distribution lists in MSG and VCF files.
- Working with Contacts in PST File - the contacts a distribution list refers to.