Working with Distribution Lists
A distribution list is a collection of contacts that lets you address several recipients at once. With Aspose.Email you can build a standalone distribution list, save it to disk in Outlook MSG format and open it in Microsoft Outlook, where it can be viewed and edited like any other item.
This article covers standalone distribution lists stored as MSG files. To create, read and update distribution lists that live inside a PST file, see Managing Outlook Distribution Lists in PST Files.
Create and Save Distribution Lists
A distribution list is represented by the MapiDistributionList class. Its members are MapiDistributionListMember objects collected in a MapiDistributionListMemberCollection. The following code snippet shows you how to create a distribution list and save it as an MSG file:
import aspose.email as ae
from aspose.email.mapi import (MapiDistributionList, MapiDistributionListMember,
MapiDistributionListMemberCollection)
members = MapiDistributionListMemberCollection()
members.append(MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com"))
members.append(MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com"))
distribution_list = MapiDistributionList("Simple list", members)
distribution_list.subject = "Sample distribution list created with Aspose.Email"
distribution_list.body = "Distribution list body"
distribution_list.save(data_dir + "DistributionList_out.msg")
Read Distribution Lists from MSG Files
A distribution list loaded from disk is represented by a MapiMessage object. Cast the item returned by its to_mapi_message_item() method to MapiDistributionList to access the list-specific properties:
import aspose.pycore as pycore
from aspose.email.mapi import MapiMessage, MapiDistributionList
message = MapiMessage.load(data_dir + "DistributionList_out.msg")
distribution_list = pycore.cast(MapiDistributionList, message.to_mapi_message_item())
print("Display name:", distribution_list.display_name)
for member in distribution_list.members:
print(member.display_name, "-", member.email_address)
Update Distribution Lists
The collection returned by the members property is detached from the message, so adding an item to it does not change the saved list. To add or remove a member, rebuild the member collection and save a new MapiDistributionList built from it:
import aspose.pycore as pycore
from aspose.email.mapi import (MapiMessage, MapiDistributionList, MapiDistributionListMember,
MapiDistributionListMemberCollection)
message = MapiMessage.load(data_dir + "DistributionList_out.msg")
distribution_list = pycore.cast(MapiDistributionList, message.to_mapi_message_item())
# The collection returned by the members property is detached from the message,
# so rebuild it to make the change persistent
members = MapiDistributionListMemberCollection()
for existing_member in distribution_list.members:
members.append(MapiDistributionListMember(existing_member.display_name,
existing_member.email_address))
# Add a new member
members.append(MapiDistributionListMember("Edward R. Manuel", "EdwardRManuel@example.com"))
updated_list = MapiDistributionList(distribution_list.display_name, members)
updated_list.save(data_dir + "DistributionList_out.msg")
See Also
- Managing Outlook Distribution Lists in PST Files — create, read and update distribution lists stored in a PST file.
- Working with Outlook Contacts — create and manage the
MapiContactitems a distribution list refers to.