Working with Outlook Distribution Lists

A distribution list groups several recipients under one name, so a message can be addressed to the whole group at once. Outlook keeps such lists among the contacts, and Aspose.Email for C++ represents them with the MapiDistributionList class. This article works with standalone lists - lists saved as their own MSG or VCF file. Lists stored inside a PST file are covered in Managing Distribution Lists in PST Files.

Each entry of a list is a MapiDistributionListMember, kept in a MapiDistributionListMemberCollection. A member created from a name and an address is a one-off member: it carries its own details instead of pointing at a contact in a store, which is what a standalone file needs.

The samples below use these headers and namespaces:

#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

The MapiDistributionList constructor takes the name of the list and its members. Save writes the list in the format chosen through MapiDistributionListSaveOptions: ContactSaveFormat Msg produces an Outlook item, VCard a VCF file that other address books can import.

// 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

An MSG file is opened with MapiMessage::Load whatever it contains. SupportedType reports MapiItemType::DistList for a distribution list, and ToMapiMessageItem returns the item that is cast to MapiDistributionList; its Members collection lists who belongs to it.

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">");
    }
}

A VCF file that holds several contacts can be turned into a distribution list as well - see Distribution Lists and Multi-Contact VCF Files.

Change the Members of a Saved List

To change a saved list, read its members, build the new member collection from them, create a new MapiDistributionList with the original name, subject and body, and save it over the old file. The sample removes one member, adds another and reads the file again to show the result.

// 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">");
}

See Also