Working with Distribution Lists

This article covers standalone Outlook distribution lists (MapiDistributionList) saved as MSG or vCard (VCF). To store and read distribution lists inside a PST, see Managing Distribution Lists in PST Files.

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

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.

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

Reading a Distribution List from a File (.msg)

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

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

See Also