Working with Contacts in PST File
Outlook keeps contacts in the Contacts folder of a PST file as MapiContact items. This article shows how to add contacts to a storage and how to export the stored contacts as MSG or vCard (VCF) files. Standalone contacts saved as MSG or VCF files are covered in Create, Save, and Load Outlook Contacts.
The samples in this article use these headers and namespaces:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactGender.h>
#include <Mapi/MapiContactNamePropertySet.h>
#include <Mapi/MapiContactPersonalInfoPropertySet.h>
#include <Mapi/MapiContactProfessionalPropertySet.h>
#include <Mapi/MapiContactPhysicalAddress.h>
#include <Mapi/MapiContactPhysicalAddressPropertySet.h>
#include <Mapi/MapiContactElectronicAddress.h>
#include <Mapi/MapiContactElectronicAddressPropertySet.h>
#include <Mapi/MapiContactTelephonePropertySet.h>
#include <Mapi/ContactSaveFormat.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/MessageInfo.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Pst;
Add Contacts to a PST
A contact that needs only a name, an address and perhaps a company or a phone number can be created with one of the short MapiContact constructors. For anything more, the details are grouped into property sets - NameInfo, PersonalInfo, ProfessionalInfo, PhysicalAddresses, ElectronicAddresses and Telephones - that mirror the tabs of a contact form in Outlook.
Contacts belong in a folder with the IPF.Contact container class. CreatePredefinedFolder with StandardIpmFolder::Contacts creates the standard Contacts folder, and FolderInfo::AddMapiMessageItem stores each contact in it.
// the short constructors: name and email, plus company, plus phone
SharedPtr<MapiContact> contact1 = MakeObject<MapiContact>(u"Sebastian Wright", u"SebastianWright@dayrep.com");
SharedPtr<MapiContact> contact2 = MakeObject<MapiContact>(u"Wichert Kroos", u"WichertKroos@teleworm.us", u"Grade A Investment");
SharedPtr<MapiContact> contact3 = MakeObject<MapiContact>(u"Christoffer van de Meeberg",
u"ChristoffervandeMeeberg@teleworm.us", u"Krauses Sofa Factory", u"046-630-4614");
// a contact filled in property set by property set
SharedPtr<MapiContact> contact4 = MakeObject<MapiContact>();
contact4->set_NameInfo(MakeObject<MapiContactNamePropertySet>(u"Margaret", u"J.", u"Tolle"));
contact4->get_PersonalInfo()->set_Gender(MapiContactGender::Female);
contact4->set_ProfessionalInfo(MakeObject<MapiContactProfessionalPropertySet>(u"Adaptaz", u"Recording engineer"));
contact4->get_PhysicalAddresses()->get_WorkAddress()->set_Address(u"4 Darwinia Loop EIGHTY MILE BEACH WA 6725");
contact4->get_ElectronicAddresses()->set_Email1(
MakeObject<MapiContactElectronicAddress>(u"Margaret J. Tolle", u"SMTP", u"MargaretJTolle@dayrep.com"));
contact4->get_Telephones()->set_BusinessTelephoneNumber(u"(08)9080-1183");
contact4->get_Telephones()->set_MobileTelephoneNumber(u"(925)599-3355");
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"contacts.pst", FileFormatVersion::Unicode);
SharedPtr<FolderInfo> contacts = pst->CreatePredefinedFolder(u"Contacts", StandardIpmFolder::Contacts);
for (SharedPtr<MapiContact> contact : { contact1, contact2, contact3, contact4 })
{
contacts->AddMapiMessageItem(contact);
}
Console::WriteLine(u"Contacts stored: " + Convert::ToString(contacts->get_ContentCount()));
pst->Dispose();
Export Contacts as MSG Files
PersonalStorage::ExtractMessage returns each stored item as a MapiMessage, which is already the complete Outlook item - saving it produces an MSG file that Outlook opens as a contact. The Contacts folder may hold distribution lists as well, so the sample checks SupportedType first, and it converts the item with ToMapiMessageItem only to print the name. The file names are built from a counter, because a display name may contain characters that are not allowed in a file name.
SharedPtr<PersonalStorage> pst = PersonalStorage::FromFile(u"contacts.pst", false);
SharedPtr<FolderInfo> contacts = pst->GetPredefinedFolder(StandardIpmFolder::Contacts);
int32_t index = 0;
for (SharedPtr<MessageInfo> messageInfo : contacts->EnumerateMessages())
{
SharedPtr<MapiMessage> message = pst->ExtractMessage(messageInfo);
// the Contacts folder may also hold distribution lists
if (message->get_SupportedType() != MapiItemType::Contact)
{
continue;
}
SharedPtr<MapiContact> contact = DynamicCast<MapiContact>(message->ToMapiMessageItem());
String fileName = u"contact_" + Convert::ToString(index++) + u".msg";
// the extracted message is the contact item itself, so it is saved as it is
message->Save(fileName);
Console::WriteLine(contact->get_NameInfo()->get_DisplayName() + u" -> " + fileName);
}
pst->Dispose();
Export Contacts as vCard Files
vCard is the format address books exchange contacts in, so a VCF file can be imported into almost any mail program or phone. MapiContact::Save with ContactSaveFormat VCard writes one. As in the previous sample, the type is checked before the cast: DynamicCast throws an InvalidCastException when the item is not a MapiContact, for example when it is a distribution list.
SharedPtr<PersonalStorage> pst = PersonalStorage::FromFile(u"contacts.pst", false);
SharedPtr<FolderInfo> contacts = pst->GetPredefinedFolder(StandardIpmFolder::Contacts);
int32_t index = 0;
for (SharedPtr<MessageInfo> messageInfo : contacts->EnumerateMessages())
{
SharedPtr<MapiMessage> message = pst->ExtractMessage(messageInfo);
// skip distribution lists and anything else that is not a contact
if (message->get_SupportedType() != MapiItemType::Contact)
{
continue;
}
SharedPtr<MapiContact> contact = DynamicCast<MapiContact>(message->ToMapiMessageItem());
String fileName = u"contact_" + Convert::ToString(index++) + u".vcf";
contact->Save(fileName, ContactSaveFormat::VCard);
Console::WriteLine(contact->get_NameInfo()->get_DisplayName() + u" -> " + fileName);
}
pst->Dispose();
An exported vCard file opened in Microsoft Outlook looks like this:

See Also
- Managing Distribution Lists in PST Files - create, read and update distribution lists made of these contacts.
- Create, Save, and Load Outlook Contacts - standalone contacts in MSG and VCF files.