Working with Distribution Lists
Contents
[
Hide
]
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.
Creating and Saving a Distribution List
The following code snippet shows you how to create and save a distribution list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
string dataDir = RunExamples.GetDataDir_Outlook(); | |
string displayName1 = "Sebastian Wright"; | |
string email1 = "SebastianWright@dayrep.com"; | |
string displayName2 = "Wichert Kroos"; | |
string email2 = "WichertKroos@teleworm.us"; | |
string strEntryId1; | |
string strEntryId2; | |
string path = dataDir + "CreateDistributionListInPST_out.pst"; | |
if (File.Exists(path)) | |
{ | |
File.Delete(path); | |
} | |
// Create distribution list from contacts | |
using (PersonalStorage personalStorage = PersonalStorage.Create(dataDir + "CreateDistributionListInPST_out.pst", FileFormatVersion.Unicode)) | |
{ | |
// Add the contact folder to pst | |
FolderInfo contactFolder = personalStorage.CreatePredefinedFolder("Contacts", StandardIpmFolder.Contacts); | |
// Create contacts | |
strEntryId1 = contactFolder.AddMapiMessageItem(new MapiContact(displayName1, email1)); | |
strEntryId2 = contactFolder.AddMapiMessageItem(new MapiContact(displayName2, email2)); | |
// Create distribution list on the base of the created contacts | |
MapiDistributionListMember member1 = new MapiDistributionListMember(displayName1, email1); | |
member1.EntryIdType = MapiDistributionListEntryIdType.Contact; | |
member1.EntryId = Convert.FromBase64String(strEntryId1); | |
MapiDistributionListMember member2 = new MapiDistributionListMember(displayName2, email2); | |
member2.EntryIdType = MapiDistributionListEntryIdType.Contact; | |
member2.EntryId = Convert.FromBase64String(strEntryId2); | |
MapiDistributionListMemberCollection members = new MapiDistributionListMemberCollection(); | |
members.Add(member1); | |
members.Add(member2); | |
MapiDistributionList distributionList = new MapiDistributionList("Contact list", members); | |
distributionList.Body = "Distribution List Body"; | |
distributionList.Subject = "Sample Distribution List using Aspose.Email"; | |
// Add distribution list to PST | |
contactFolder.AddMapiMessageItem(distributionList); | |
} | |
string path1 = dataDir + "CreateDistributionListInPST_OneOffmembers_out.pst"; | |
if (File.Exists(path1)) | |
{ | |
File.Delete(path1); | |
} | |
// Create one-off distribution list members (for which no separate contacts were created) | |
using (PersonalStorage personalStorage = PersonalStorage.Create(dataDir + "CreateDistributionListInPST_OneOffmembers_out.pst", FileFormatVersion.Unicode)) | |
{ | |
// Add the contact folder to pst | |
FolderInfo contactFolder = personalStorage.CreatePredefinedFolder("Contacts", StandardIpmFolder.Contacts); | |
MapiDistributionListMemberCollection oneOffmembers = new MapiDistributionListMemberCollection(); | |
oneOffmembers.Add(new MapiDistributionListMember("John R. Patrick", "JohnRPatrick@armyspy.com")); | |
oneOffmembers.Add(new MapiDistributionListMember("Tilly Bates", "TillyBates@armyspy.com")); | |
MapiDistributionList oneOffMembersList = new MapiDistributionList("Simple list", oneOffmembers); | |
contactFolder.AddMapiMessageItem(oneOffMembersList); | |
} |
Reading a Distribution List from a PST
The following code snippet shows you how to read a distribution list from a PST.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
MapiMessage message = MapiMessage.FromFile(fileName); | |
MapiDistributionList dlist = (MapiDistributionList)message.ToMapiMessageItem(); |
Update Distribution List in PST
The following code snippet shows you how to update a distribution list in PST.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
using (PersonalStorage personalStorage = PersonalStorage.Create(dataDir + "UdateDistributionList.pst", FileFormatVersion.Unicode)) | |
{ | |
// Add the contact folder to PST | |
FolderInfo contactFolder = | |
personalStorage.CreatePredefinedFolder("Contacts", StandardIpmFolder.Contacts); | |
// Create contacts | |
string contactId1 = contactFolder.AddMapiMessageItem(new MapiContact("Test1", "test1@test.com")); | |
string contactId2 = contactFolder.AddMapiMessageItem(new MapiContact("Test2", "test2@test.com")); | |
// Create distribution list on the base of the created contacts | |
MapiDistributionListMember contact1 = | |
new MapiDistributionListMember("Test1", "test1@test.com") | |
{ | |
EntryIdType = MapiDistributionListEntryIdType.Contact, | |
EntryId = Convert.FromBase64String(contactId1) | |
}; | |
MapiDistributionListMember contact2 = | |
new MapiDistributionListMember("Test2", "test2@test.com") | |
{ | |
EntryIdType = MapiDistributionListEntryIdType.Contact, | |
EntryId = Convert.FromBase64String(contactId2) | |
}; | |
MapiDistributionList distributionList1 = new MapiDistributionList("Contact list1", | |
new MapiDistributionListMemberCollection { contact1, contact2 }) | |
{ | |
Body = "Distribution List Body1", | |
Subject = "Sample Distribution List1" | |
}; | |
// Add distribution list to PST | |
string distributionListId1 = contactFolder.AddMapiMessageItem(distributionList1); | |
// Create second distribution list | |
string contactId3 = contactFolder.AddMapiMessageItem(new MapiContact("Test3", "test3@test.com")); | |
MapiDistributionListMember contact3 = | |
new MapiDistributionListMember("Test3", "test3@test.com") | |
{ | |
EntryIdType = MapiDistributionListEntryIdType.Contact, | |
EntryId = Convert.FromBase64String(contactId3) | |
}; | |
MapiDistributionList distributionList2 = new MapiDistributionList("Contact list2", | |
new MapiDistributionListMemberCollection { contact3 }) | |
{ | |
Body = "Distribution List Body1", | |
Subject = "Distribution List1" | |
}; | |
// Add distribution list to PST | |
string distributionListId2 = contactFolder.AddMapiMessageItem(distributionList2); | |
// Add first distribution list to the second | |
MapiDistributionListMember dl = | |
new MapiDistributionListMember(distributionList1.DisplayName, null) | |
{ | |
EntryIdType = MapiDistributionListEntryIdType.DistributionList, | |
EntryId = Convert.FromBase64String(distributionListId1) | |
}; | |
distributionList2.Members.Add(dl); | |
// update DL in PST | |
contactFolder.UpdateMessage(distributionListId2, distributionList2); | |
} |