Working with Outlook Distribution Lists using Email C++ Library
Contents
[
Hide
]
Working with Distribution Lists
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 with C++ Email Library API.
This file contains 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/kashifiqb/Aspose.Email-for-C | |
System::String dataDir = RunExamples::GetDataDir_Outlook(); | |
System::String displayName1 = L"Sebastian Wright"; | |
System::String email1 = L"SebastianWright@dayrep.com"; | |
System::String displayName2 = L"Wichert Kroos"; | |
System::String email2 = L"WichertKroos@teleworm.us"; | |
System::String strEntryId1; | |
System::String strEntryId2; | |
System::String path = dataDir + L"CreateDistributionListInPST_out.pst"; | |
if (System::IO::File::Exists(path)) | |
{ | |
System::IO::File::Delete(path); | |
} | |
// Create distribution list from contacts | |
{ | |
System::SharedPtr<PersonalStorage> personalStorage = PersonalStorage::Create(dataDir + L"CreateDistributionListInPST_out.pst", Aspose::Email::Outlook::Pst::FileFormatVersion::Unicode); | |
// Add the contact folder to pst | |
System::SharedPtr<FolderInfo> contactFolder = personalStorage->CreatePredefinedFolder(L"Contacts", Aspose::Email::Outlook::Pst::StandardIpmFolder::Contacts); | |
// Create contacts | |
strEntryId1 = contactFolder->AddMapiMessageItem(System::MakeObject<MapiContact>(displayName1, email1)); | |
strEntryId2 = contactFolder->AddMapiMessageItem(System::MakeObject<MapiContact>(displayName2, email2)); | |
// Create distribution list on the base of the created contacts | |
System::SharedPtr<MapiDistributionListMember> member1 = System::MakeObject<MapiDistributionListMember>(displayName1, email1); | |
member1->set_EntryIdType(Aspose::Email::Outlook::MapiDistributionListEntryIdType::Contact); | |
member1->set_EntryId(System::Convert::FromBase64String(strEntryId1)); | |
System::SharedPtr<MapiDistributionListMember> member2 = System::MakeObject<MapiDistributionListMember>(displayName2, email2); | |
member2->set_EntryIdType(Aspose::Email::Outlook::MapiDistributionListEntryIdType::Contact); | |
member2->set_EntryId(System::Convert::FromBase64String(strEntryId2)); | |
System::SharedPtr<MapiDistributionListMemberCollection> members = System::MakeObject<MapiDistributionListMemberCollection>(); | |
members->Add(member1); | |
members->Add(member2); | |
System::SharedPtr<MapiDistributionList> distributionList = System::MakeObject<MapiDistributionList>(L"Contact list", members); | |
distributionList->set_Body(L"Distribution List Body"); | |
distributionList->set_Subject(L"Sample Distribution List using Aspose.Email"); | |
// Add distribution list to PST | |
contactFolder->AddMapiMessageItem(distributionList); | |
} | |
System::String path1 = dataDir + L"CreateDistributionListInPST_OneOffmembers_out.pst"; | |
if (System::IO::File::Exists(path1)) | |
{ | |
System::IO::File::Delete(path1); | |
} | |
// Create one-off distribution list members (for which no separate contacts were created) | |
{ | |
System::SharedPtr<PersonalStorage> personalStorage = PersonalStorage::Create(dataDir + L"CreateDistributionListInPST_OneOffmembers_out.pst", Aspose::Email::Outlook::Pst::FileFormatVersion::Unicode); | |
// Add the contact folder to pst | |
System::SharedPtr<FolderInfo> contactFolder = personalStorage->CreatePredefinedFolder(L"Contacts", Aspose::Email::Outlook::Pst::StandardIpmFolder::Contacts); | |
System::SharedPtr<MapiDistributionListMemberCollection> oneOffmembers = System::MakeObject<MapiDistributionListMemberCollection>(); | |
oneOffmembers->Add(System::MakeObject<MapiDistributionListMember>(L"John R. Patrick", L"JohnRPatrick@armyspy.com")); | |
oneOffmembers->Add(System::MakeObject<MapiDistributionListMember>(L"Tilly Bates", L"TillyBates@armyspy.com")); | |
System::SharedPtr<MapiDistributionList> oneOffMembersList = System::MakeObject<MapiDistributionList>(L"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 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/kashifiqb/Aspose.Email-for-C | |
System::SharedPtr<MapiMessage> message = MapiMessage::FromFile(fileName); | |
System::SharedPtr<MapiDistributionList> dlist = System::DynamicCast<Aspose::Email::Outlook::MapiDistributionList>(message->ToMapiMessageItem()); |