Working with Contacts in PST File

Adding Contact to PST

With Aspose.Email you can add a MapiContact to the Contacts subfolder of a PST file that you have created or loaded. Below are the steps to add MapiContact to a PST:

  1. Create a MapiContact object.
  2. Set the MapiContact properties using different constructors and methods.
  3. Create a PST using the PersonalStorage.Create() method.
  4. Create a pre-defined folder (Contacts) at the root of the PST file by accessing the root folder and then calling the AddMapiMessageItem() method.

The following code snippet shows you how to create a MapiContact and then add it to the contacts folder of a newly created PST file.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
System::String dataDir = RunExamples::GetDataDir_Outlook();
// Create three Contacts
System::SharedPtr<MapiContact> contact1 = System::MakeObject<MapiContact>(L"Sebastian Wright", L"SebastianWright@dayrep.com");
System::SharedPtr<MapiContact> contact2 = System::MakeObject<MapiContact>(L"Wichert Kroos", L"WichertKroos@teleworm.us", L"Grade A Investment");
System::SharedPtr<MapiContact> contact3 = System::MakeObject<MapiContact>(L"Christoffer van de Meeberg", L"ChristoffervandeMeeberg@teleworm.us", L"Krauses Sofa Factory", L"046-630-4614046-630-4614");
// Contact #4
System::SharedPtr<MapiContact> contact4 = System::MakeObject<MapiContact>();
contact4->set_NameInfo(System::MakeObject<MapiContactNamePropertySet>(L"Margaret", L"J.", L"Tolle"));
contact4->get_PersonalInfo()->set_Gender(Aspose::Email::Outlook::MapiContactGender::Female);
contact4->set_ProfessionalInfo(System::MakeObject<MapiContactProfessionalPropertySet>(L"Adaptaz", L"Recording engineer"));
contact4->get_PhysicalAddresses()->get_WorkAddress()->set_Address(L"4 Darwinia Loop EIGHTY MILE BEACH WA 6725");
contact4->get_ElectronicAddresses()->set_Email1(System::MakeObject<MapiContactElectronicAddress>(L"Hisen1988", L"SMTP", L"MargaretJTolle@dayrep.com"));
contact4->get_Telephones()->set_BusinessTelephoneNumber(L"(08)9080-1183");
contact4->get_Telephones()->set_MobileTelephoneNumber(L"(925)599-3355(925)599-3355");
// Contact #5
System::SharedPtr<MapiContact> contact5 = System::MakeObject<MapiContact>();
contact5->set_NameInfo(System::MakeObject<MapiContactNamePropertySet>(L"Matthew", L"R.", L"Wilcox"));
contact5->get_PersonalInfo()->set_Gender(Aspose::Email::Outlook::MapiContactGender::Male);
contact5->set_ProfessionalInfo(System::MakeObject<MapiContactProfessionalPropertySet>(L"Briazz", L"Psychiatric aide"));
contact5->get_PhysicalAddresses()->get_WorkAddress()->set_Address(L"Horner Strasse 12 4421 SAASS");
contact5->get_Telephones()->set_BusinessTelephoneNumber(L"0650 675 73 300650 675 73 30");
contact5->get_Telephones()->set_HomeTelephoneNumber(L"(661)387-5382(661)387-5382");
// Contact #6
System::SharedPtr<MapiContact> contact6 = System::MakeObject<MapiContact>();
contact6->set_NameInfo(System::MakeObject<MapiContactNamePropertySet>(L"Bertha", L"A.", L"Buell"));
contact6->set_ProfessionalInfo(System::MakeObject<MapiContactProfessionalPropertySet>(L"Awthentikz", L"Social work assistant"));
contact6->get_PersonalInfo()->set_PersonalHomePage(L"B2BTies.com");
contact6->get_PhysicalAddresses()->get_WorkAddress()->set_Address(L"Im Astenfeld 59 8580 EDELSCHROTT");
contact6->get_ElectronicAddresses()->set_Email1(System::MakeObject<MapiContactElectronicAddress>(L"Experwas", L"SMTP", L"BerthaABuell@armyspy.com"));
contact6->set_Telephones(System::MakeObject<MapiContactTelephonePropertySet>(L"06605045265"));
// Load the Outlook file
System::String path = dataDir + L"SampleContacts_out.pst";
if (System::IO::File::Exists(path))
{
System::IO::File::Delete(path);
}
{
System::SharedPtr<PersonalStorage> personalStorage = PersonalStorage::Create(dataDir + L"SampleContacts_out.pst", Aspose::Email::Outlook::Pst::FileFormatVersion::Unicode);
System::SharedPtr<FolderInfo> contactFolder = personalStorage->CreatePredefinedFolder(L"Contacts", Aspose::Email::Outlook::Pst::StandardIpmFolder::Contacts);
contactFolder->AddMapiMessageItem(contact1);
contactFolder->AddMapiMessageItem(contact2);
contactFolder->AddMapiMessageItem(contact3);
contactFolder->AddMapiMessageItem(contact4);
contactFolder->AddMapiMessageItem(contact5);
contactFolder->AddMapiMessageItem(contact6);
}

Save contacts information from PST file in MSG Format

This article explains how to access contact information from an Outlook PST file and save the contact to disk in MSG format. The PersonalStorage and MapiContact classes to get and display the contact information. The steps to get the contact information are:

  1. Load the PST file in the PersonalStorage class.
  2. Browse the Contacts folder.
  3. Get the contents of the Contacts folder to get the message collection.
  4. Loop through the message collection.
  5. Call the PersonalStorage.ExtractContactInfo() method to get the contact information in the MapiContact class. Use the MapiContact class properties to access the contact information
  6. Call the PersonalStorage.ExtractMessage() method to get the contact information in the MapiMessage class.
  7. Call the MapiMessage.Save() method to save the contact to disk in MSG format.

The following code snippet shows you how to retrieve all the contact information from the PST file and save to disk in MSG format.

For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// Load the Outlook file
System::String dataDir = Examples::CSharp::Email::RunExamples::GetDataDir_Outlook();
// Load the Outlook PST file
System::SharedPtr<PersonalStorage> personalStorage = PersonalStorage::FromFile(dataDir + L"SampleContacts.pst");
// Get the Contacts folder
System::SharedPtr<FolderInfo> folderInfo = personalStorage->get_RootFolder()->GetSubFolder(L"Contacts");
// Loop through all the contacts in this folder
System::SharedPtr<MessageInfoCollection> messageInfoCollection = folderInfo->GetContents();
{
auto messageInfo_enumerator = (messageInfoCollection)->GetEnumerator();
decltype(messageInfo_enumerator->get_Current()) messageInfo;
while (messageInfo_enumerator->MoveNext() && (messageInfo = messageInfo_enumerator->get_Current(), true))
{
// Get the contact information
System::SharedPtr<MapiMessage> mapi = personalStorage->ExtractMessage(messageInfo);
System::SharedPtr<MapiContact> contact = System::DynamicCast<Aspose::Email::Outlook::MapiContact>(mapi->ToMapiMessageItem());
// Display some contents on screen
System::Console::WriteLine(System::String(L"Name: ") + contact->get_NameInfo()->get_DisplayName());
// Save to disk in MSG format
if (contact->get_NameInfo()->get_DisplayName() != nullptr)
{
System::SharedPtr<MapiMessage> message = personalStorage->ExtractMessage(messageInfo);
// Get rid of illegal characters that cannot be used as a file name
System::String messageName = message->get_Subject().Replace(L":", L" ").Replace(L"\\", L" ").Replace(L"?", L" ").Replace(L"/", L" ");
message->Save(dataDir + L"Contacts\\" + messageName + L"_out.msg");
}
}
}

Save Contacts information from PST file in VCF Format

This article shows how to access contact information from a Microsoft Outlook PST file and save the contact to disk in vCard (VCF) format. Use the PersonalStorage and MapiContact classes to get contact information from the PST file. To get the contact information:

  1. Load the PST file in the PersonalStorage class.
  2. Browse the Contacts folder.
  3. Get the contents of the Contacts folder to get the message collection.
  4. Loop through the message collection.
  5. Call the PersonalStorage.ExtractMessage() method to get the contact information in the MapiContact class.
  6. Use the MapiContact class different properties to access the contact information.

The program below loads a PST file from disk and saves all the contacts to vCard (VCF) format. The VCF files can then be used in any other program that can load the standard vCard contact file. If you open any VCF file in Microsoft Outlook, it looks like the one in the below screenshot.

todo:image_alt_text
The following code snippet shows you how to export contacts from Outlook PST to vCard (VCF) format.
For complete examples and data files, please go to https://github.com/kashifiqb/Aspose.Email-for-C
// Load the Outlook file
System::String dataDir = RunExamples::GetDataDir_Outlook();
// Load the Outlook PST file
System::SharedPtr<PersonalStorage> personalStorage = PersonalStorage::FromFile(dataDir + L"Outlook.pst");
// Get the Contacts folder
System::SharedPtr<FolderInfo> folderInfo = personalStorage->get_RootFolder()->GetSubFolder(L"Contacts");
// Loop through all the contacts in this folder
System::SharedPtr<MessageInfoCollection> messageInfoCollection = folderInfo->GetContents();
{
auto messageInfo_enumerator = (messageInfoCollection)->GetEnumerator();
decltype(messageInfo_enumerator->get_Current()) messageInfo;
while (messageInfo_enumerator->MoveNext() && (messageInfo = messageInfo_enumerator->get_Current(), true))
{
// Get the contact information
System::SharedPtr<MapiContact> contact = System::DynamicCast<Aspose::Email::Outlook::MapiContact>(personalStorage->ExtractMessage(messageInfo)->ToMapiMessageItem());
// Display some contents on screen
System::Console::WriteLine(System::String(L"Name: ") + contact->get_NameInfo()->get_DisplayName() + L" - " + messageInfo->get_EntryIdString());
// Save to disk in vCard VCF format
contact->Save(dataDir + L"Contacts\\" + contact->get_NameInfo()->get_DisplayName() + L".vcf", Aspose::Email::Outlook::ContactSaveFormat::VCard);
}
}

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.

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.

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