Working with Distribution Lists

It is possible to create a Distribution list using Aspose.Email API that is a collection of multiple contacts. A distribution list can be saved to disc in Outlook MSG format and can be viewed/manipulated by opening it in MS Outlook.

Create and Save Distribution Lists

The following code snippet shows you how to create and save a distribution list.

Read Distribution Lists from PST Files

The following code snippet shows you how to read a distribution list from a PST file.

import aspose.email as ae
import aspose.pycore as pycore

pst = ae.storage.pst.PersonalStorage.from_file( "your_pst_file.pst")
folder = pst.root_folder.get_sub_folder("Contacts")
for message_info in folder.enumerate_messages():
    msg = pst.extract_message(message_info)

    if msg.supported_type == ae.mapi.MapiItemType.DIST_LIST:
        dlist = pycore.cast(ae.mapi.MapiDistributionList, msg.to_mapi_message_item())
        for member in dlist.members:
            print("Member Display Name:", member.display_name)
            print("Member Email Address:", member.email_address)

Update Distribution Lists in PST Files

To update a distribution list in a PST file, for example to add a new member, use the following code sample:

import aspose.email as ae
import aspose.pycore as pycore

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

folder = pst.get_predefined_folder(ae.storage.pst.StandardIpmFolder.CONTACTS)

# add a new member to each distribution list in pst
for msg in folder.enumerate_messages():
    # Check if the message has the "IPM.DistList" message class
    if msg.message_class == "IPM.DistList":
        dist_list = pycore.cast(ae.mapi.MapiDistributionList, pst.extract_message(msg).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 = ae.mapi.MapiDistributionListMemberCollection()
        for existing_member in dist_list.members:
            members.append(ae.mapi.MapiDistributionListMember(existing_member.display_name, existing_member.email_address))

        # Create new member to add
        members.append(ae.mapi.MapiDistributionListMember("Edward R. Manuel", "EdwardRManuel@example.com"))

        # update DL in PST
        folder.update_message(msg.entry_id_string, ae.mapi.MapiDistributionList(dist_list.display_name, members))