Distribution list support

Hi,
Support for distribution lists in contacts is available.

Hello @Anjali12,

Could you please clarify your request?
Are you trying to read, create, or modify a distribution list?

Thank you.

would like detailed guidance on both reading and creating a distribution list. Specifically, I’m looking to understand:

  • How to read/retrieve an existing distribution list , including its members and properties
  • How to create a new distribution list , along with adding members and setting relevant attributes

Could you please tell us if we can use the Aspose API to read and create a distribution list?”

Here is detailed guidance with code examples for working with Outlook distribution lists:

1. Read/Get Information from an Existing Mailing List

A. Reading a Distribution List from a File (.msg)

This method is used when you have a distribution list saved as a standalone .msg file.

Steps:

  1. Load the .msg file into a MapiMessage object using MapiMessage.Load.
  2. Check if the message type is a distribution list by verifying MapiItemType.DistList.
  3. Cast the message to a MapiDistributionList object using ToMapiMessageItem().
  4. Access the list’s properties (like DisplayName) and iterate through its Members collection.

Code Example:

using Aspose.Email.Mapi;

// Load the distribution list from a .msg file
var msg = MapiMessage.Load("dlist.msg");

// Check if the loaded message is a distribution list
if (msg.SupportedType == MapiItemType.DistList)
{
    // Convert to MapiDistributionList object
    var dlist = (MapiDistributionList)msg.ToMapiMessageItem();

    // Display distribution list details
    Console.WriteLine($"Name: {dlist.DisplayName}");

    // Iterate through members and display their details
    foreach (var member in dlist.Members)
    {
        Console.WriteLine($"Member: {member.DisplayName}, Email: {member.EmailAddress}");
    }
}

B. Reading Distribution Lists from a PST File

This method is used to find and read all distribution lists stored within the “Contacts” folder of a PST file.

Steps:

  1. Load the PST file using PersonalStorage.FromFile.
  2. Retrieve the predefined “Contacts” folder with GetPredefinedFolder(StandardIpmFolder.Contacts).
  3. Enumerate all messages in the folder using EnumerateMessages().
  4. For each message, check if its MessageClass property is "IPM.DistList" to identify it as a distribution list.
  5. Extract the full message using ExtractMessage and convert it to a MapiDistributionList object.

Code Example:

using Aspose.Email.Storage.Pst;
using Aspose.Email.Mapi;

// Load the PST file
using (var pst = PersonalStorage.FromFile("your.pst"))
{
    // Get the built-in Contacts folder
    var folder = pst.GetPredefinedFolder(StandardIpmFolder.Contacts);
    
    if (folder != null)
    {
        // Loop through all items in the Contacts folder
        foreach (var msgInfo in folder.EnumerateMessages())
        {
            // Check if the item is a distribution list by its message class
            if (msgInfo.MessageClass == "IPM.DistList")
            {
                // Extract the distribution list message
                var distList = (MapiDistributionList)pst.ExtractMessage(msgInfo).ToMapiMessageItem();

                // Access distribution list properties
                Console.WriteLine($"Name: {distList.DisplayName}");

                // Iterate through members
                foreach (var member in distList.Members)
                {
                    Console.WriteLine($"Member: {member.DisplayName}, Email: {member.EmailAddress}");
                }
            }
        }
    }
}

2. How to Create a New Mailing List, Add Members, and Set Attributes

A. Creating a Simple Distribution List (One-Off Members)

This method creates a distribution list with “one-off” members, meaning the members are not linked to existing contact entries in the PST.

Steps:

  1. Create a MapiDistributionListMember for each member, providing their display name and email address.
  2. Add these members to a MapiDistributionListMemberCollection.
  3. Instantiate a MapiDistributionList object, providing a name for the list and the member collection.
  4. (Optional) Set other properties like Body and Subject.
  5. Save the list to a file using the Save method.

Code Example:

using Aspose.Email.Mapi;

// Create one-off members (not linked to existing contacts)
var oneOffmembers = new MapiDistributionListMemberCollection();
oneOffmembers.Add(new MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com"));
oneOffmembers.Add(new MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com"));

// Create a new distribution list
var oneOffMembersList = new MapiDistributionList("Simple list", oneOffmembers);

// Set additional properties
oneOffMembersList.Body = "Distribution List Body";
oneOffMembersList.Subject = "Sample Distribution List using Aspose.Email";

// Save the distribution list to different formats
oneOffMembersList.Save("distribution_list.vcf", new MapiDistributionListSaveOptions(ContactSaveFormat.VCard));
oneOffMembersList.Save("distribution_list.msg", new MapiDistributionListSaveOptions(ContactSaveFormat.Msg));

B. Creating a Distribution List in a PST File (Linked to Existing Contacts)

This method is used when you want to create a distribution list inside a PST file, with its members linked to existing contact items within the same PST.

Steps:

  1. Create or load a PST and get the “Contacts” folder.
  2. Create MapiContact objects for the list members and add them to the “Contacts” folder using AddMapiMessageItem. This returns a unique entry ID for each contact.
  3. Create MapiDistributionListMember objects for each contact. Set their EntryIdType to MapiDistributionListEntryIdType.Contact and assign the contact’s entry ID.
  4. Add these members to a MapiDistributionListMemberCollection.
  5. Create a MapiDistributionList object, providing a name and the member collection.
  6. Set additional properties like Body and Subject.
  7. Add the distribution list to the “Contacts” folder using AddMapiMessageItem.

Code Example:

using Aspose.Email.Storage.Pst;
using Aspose.Email.Mapi;

// Load or create the PST file
using (var pst = PersonalStorage.FromFile("your.pst"))
{
    // Get the Contacts folder
    var folder = pst.GetPredefinedFolder(StandardIpmFolder.Contacts);

    if (folder != null)
    {
        // Create individual contact items
        var contact1 = new MapiContact("Susanne Nielsen", "SusanneNielsen@example.com");
        var contact2 = new MapiContact("Lars Rosing", "LarsRosing@example.com");

        // Add contacts to the folder and retrieve their entry IDs
        var idContact1 = folder.AddMapiMessageItem(contact1);
        var idContact2 = folder.AddMapiMessageItem(contact2);

        // Create distribution list members linked to the contacts
        var member1 = new MapiDistributionListMember(contact1.NameInfo.DisplayName, contact1.ElectronicAddresses.Email1.EmailAddress)
        {
            EntryIdType = MapiDistributionListEntryIdType.Contact,
            EntryId = Convert.FromBase64String(idContact1)
        };
        var member2 = new MapiDistributionListMember(contact2.NameInfo.DisplayName, contact2.ElectronicAddresses.Email1.EmailAddress)
        {
            EntryIdType = MapiDistributionListEntryIdType.Contact,
            EntryId = Convert.FromBase64String(idContact2)
        };

        // Create a collection of distribution list members
        var members = new MapiDistributionListMemberCollection
        {
            member1,
            member2
        };

        // Create the distribution list
        var distributionList = new MapiDistributionList("Contact list", members)
        {
            Body = "Distribution List Body",
            Subject = "Sample Distribution List using Aspose.Email"
        };

        // Add the distribution list to the Contacts folder in the PST
        folder.AddMapiMessageItem(distributionList);
    }
}

Links to related articles: