Working with Distribution Lists
Contents
[
Hide
]
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.
- Create a
MapiDistributionListMemberfor each member, providing their display name and email address. - Add these members to a
MapiDistributionListMemberCollection. - Instantiate a
MapiDistributionListobject, providing a name for the list and the member collection. - (Optional) Set other properties like
BodyandSubject. - Save the list to a file using the
Savemethod.
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.
- Load the
.msgfile into aMapiMessageobject usingMapiMessage.Load. - Check if the message type is a distribution list by verifying
MapiItemType.DistList. - Cast the message to a
MapiDistributionListobject usingToMapiMessageItem(). - Access the list’s properties (like
DisplayName) and iterate through itsMemberscollection.
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
- Managing Distribution Lists in PST Files — store and read distribution lists inside a PST.
- Outlook Contacts Management — create and manage the
MapiContactitems that make up a distribution list.