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:
- 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}");
}
}
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:
- Load the PST file using
PersonalStorage.FromFile. - Retrieve the predefined “Contacts” folder with
GetPredefinedFolder(StandardIpmFolder.Contacts). - Enumerate all messages in the folder using
EnumerateMessages(). - For each message, check if its
MessageClassproperty is"IPM.DistList"to identify it as a distribution list. - Extract the full message using
ExtractMessageand convert it to aMapiDistributionListobject.
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:
- 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));
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:
- Create or load a PST and get the “Contacts” folder.
- Create
MapiContactobjects for the list members and add them to the “Contacts” folder usingAddMapiMessageItem. This returns a unique entry ID for each contact. - Create
MapiDistributionListMemberobjects for each contact. Set theirEntryIdTypetoMapiDistributionListEntryIdType.Contactand assign the contact’s entry ID. - Add these members to a
MapiDistributionListMemberCollection. - Create a
MapiDistributionListobject, providing a name and the member collection. - Set additional properties like
BodyandSubject. - 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: