Working with vCard (VCF) Files in C++
vCard is the interchange format for contact data understood by phones, mail clients and directory services. Aspose.Email for C++ handles it through the VCardContact class in the Aspose::Email::PersonalInfo::VCard namespace - a pure vCard object model that needs neither Outlook nor MAPI. If your contacts live in MSG or PST files, use MapiContact instead; it can also import from and export to VCF, as shown in Create, Save, and Load Outlook Contacts.
The samples below use the following headers and namespaces:
#include <PersonalInfo/vCard/VCardContact.h>
#include <PersonalInfo/vCard/VCardIdentificationInfo.h>
#include <PersonalInfo/vCard/VCardFullName.h>
#include <PersonalInfo/vCard/VCardOrganization.h>
#include <PersonalInfo/vCard/VCardEmail.h>
#include <PersonalInfo/vCard/VCardEmailCollection.h>
#include <PersonalInfo/vCard/VCardEmailType.h>
#include <PersonalInfo/vCard/VCardDeliveryAddress.h>
#include <PersonalInfo/vCard/VCardDeliveryAddressCollection.h>
#include <PersonalInfo/vCard/VCardDeliveryAddressType.h>
#include <PersonalInfo/vCard/VCardTelephoneNumber.h>
#include <PersonalInfo/vCard/VCardTelephoneNumberCollection.h>
#include <PersonalInfo/vCard/VCardTelephoneType.h>
#include <PersonalInfo/vCard/VCardGeo.h>
#include <PersonalInfo/vCard/VCardPhoto.h>
#include <PersonalInfo/vCard/VCardPhotoType.h>
#include <PersonalInfo/vCard/VCardValueLocation.h>
#include <PersonalInfo/vCard/VCardSecurity.h>
#include <PersonalInfo/vCard/VCardLoadOptions.h>
#include <PersonalInfo/vCard/VCardSaveOptions.h>
#include <PersonalInfo/vCard/VCardVersion.h>
#include <system/io/file.h>
#include <system/text/encoding.h>
#include <system/object_ext.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::PersonalInfo::VCard;
Choosing Between VCardContact and MapiContact
| VCardContact | MapiContact | |
|---|---|---|
| Native format | vCard 2.1 / 3.0 / 4.0 | Outlook MSG, PST items |
| Typical source | .vcf files, phone exports, LDAP dumps |
Outlook, Exchange, PST archives |
| Object model | Mirrors the vCard properties (N, FN, ADR, TEL, GEO, KEY …) | Mirrors the Outlook contact form and MAPI properties |
| Multi-contact files | Yes, through LoadAsMultiple |
One contact per file; a multi-contact VCF loads as a MapiDistributionList |
The rest of this article is about VCardContact.
Create a Contact and Save It as VCF
A VCardContact starts empty; data is added through grouped property objects. VCardIdentificationInfo holds the name-related fields (its FullName member stores the structured N property), VCardOrganization the company and title, and the Emails collection the e-mail addresses. Save writes the file in vCard 2.1 by default.
SharedPtr<VCardContact> contact = MakeObject<VCardContact>();
// name block
SharedPtr<VCardIdentificationInfo> identification = MakeObject<VCardIdentificationInfo>();
identification->set_DisplayName(u"Bertha Buell");
identification->set_Nickname(u"Bertie");
identification->set_Birthday(DateTime(1980, 5, 20));
identification->get_FullName()->set_GivenName(u"Bertha");
identification->get_FullName()->set_MiddleName(u"A.");
identification->get_FullName()->set_FamilyName(u"Buell");
contact->set_IdentificationInfo(identification);
// company block
SharedPtr<VCardOrganization> organization = MakeObject<VCardOrganization>();
organization->set_Organization(u"Awthentikz");
organization->set_Title(u"Social work assistant");
contact->set_Organization(organization);
// e-mail block
SharedPtr<VCardEmail> email = MakeObject<VCardEmail>();
email->set_EmailAddress(u"BerthaABuell@example.com");
email->set_EmailType(VCardEmailType::INTERNET);
contact->get_Emails()->Add(email);
// write the vCard (VCF) file
contact->Save(u"bertha.vcf");
Fill in the Rest of the Model
The snippets below load the contact created above, add one group of properties and save it again. Enumerations that represent vCard type parameters (address type, phone type, e-mail type) are flag enumerations, so several values can be combined with |.
Postal Addresses
A VCardDeliveryAddress maps to one ADR line. Its AddressType takes VCardDeliveryAddressType flags: HOME, WORK, POSTAL, PARCEL, DOM, INTL, PREF.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardDeliveryAddress> address = MakeObject<VCardDeliveryAddress>();
address->set_AddressType(VCardDeliveryAddressType::WORK | VCardDeliveryAddressType::PREF);
address->set_Street(u"4 Darwinia Loop");
address->set_Locality(u"Eighty Mile Beach");
address->set_Region(u"WA");
address->set_PostalCode(u"6725");
address->set_CountryName(u"Australia");
contact->get_DeliveryAddresses()->Add(address);
contact->Save(u"bertha.vcf");
Telephone Numbers
Each VCardTelephoneNumber becomes a TEL line; VCardTelephoneType supplies VOICE, WORK, HOME, CELL, FAX, PAGER, PREF and more.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardTelephoneNumber> work = MakeObject<VCardTelephoneNumber>();
work->set_TelephoneNumber(u"(08) 9080 1183");
work->set_TelephoneType(VCardTelephoneType::WORK | VCardTelephoneType::VOICE);
contact->get_TelephoneNumbers()->Add(work);
SharedPtr<VCardTelephoneNumber> mobile = MakeObject<VCardTelephoneNumber>();
mobile->set_TelephoneNumber(u"(925) 599 3355");
mobile->set_TelephoneType(VCardTelephoneType::CELL);
contact->get_TelephoneNumbers()->Add(mobile);
contact->Save(u"bertha.vcf");
Geographic Position
VCardGeo stores the GEO property as a latitude/longitude pair of float values.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardGeo> geo = MakeObject<VCardGeo>();
geo->set_Latitude(48.858844f);
geo->set_Longitude(2.294351f);
contact->set_Geo(geo);
contact->Save(u"bertha.vcf");
Photo
The picture is assigned through the Photo property of the identification block as a VCardPhoto. ValueLocation chooses between embedding the bytes (INLINE) and pointing to an external file (URL); PhotoType names the image format.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
// embed the image bytes inline
SharedPtr<VCardPhoto> photo = MakeObject<VCardPhoto>();
photo->set_Data(IO::File::ReadAllBytes(u"portrait.jpg"));
photo->set_PhotoType(VCardPhotoType::JPEG);
photo->set_ValueLocation(VCardValueLocation::INLINE);
contact->get_IdentificationInfo()->set_Photo(photo);
contact->Save(u"contact_with_photo.vcf");
// or reference an external image by URL
SharedPtr<VCardPhoto> linkedPhoto = MakeObject<VCardPhoto>();
linkedPhoto->set_Uri(u"https://example.com/portrait.jpg");
linkedPhoto->set_PhotoType(VCardPhotoType::JPEG);
linkedPhoto->set_ValueLocation(VCardValueLocation::URL);
contact->get_IdentificationInfo()->set_Photo(linkedPhoto);
contact->Save(u"contact_with_photo_url.vcf");
Read the Model Back
After loading, the same property groups expose the parsed values. ObjectExt::ToString renders a flag enumeration as a readable list such as PREF, WORK.
SharedPtr<VCardContact> loaded = VCardContact::Load(u"bertha.vcf");
if (loaded->get_IdentificationInfo() != nullptr)
{
Console::WriteLine(loaded->get_IdentificationInfo()->get_DisplayName());
}
for (SharedPtr<VCardDeliveryAddress> address : loaded->get_DeliveryAddresses())
{
Console::WriteLine(ObjectExt::ToString(address->get_AddressType()) + u": " + address->get_Street() + u", " + address->get_Locality());
}
for (SharedPtr<VCardTelephoneNumber> phone : loaded->get_TelephoneNumbers())
{
Console::WriteLine(ObjectExt::ToString(phone->get_TelephoneType()) + u": " + phone->get_TelephoneNumber());
}
if (loaded->get_Geo() != nullptr && !loaded->get_Geo()->get_IsEmpty())
{
Console::WriteLine(u"Geo: " + Convert::ToString(loaded->get_Geo()->get_Latitude()) + u", " + Convert::ToString(loaded->get_Geo()->get_Longitude()));
}
Public Key or Certificate
The KEY property of a vCard - a public key or an X.509 certificate - is exposed through VCardSecurity. Key holds the encoded value, Type its declared kind, and SaveToPEM writes it out as a PEM file with the usual BEGIN CERTIFICATE / END CERTIFICATE fence.
SharedPtr<VCardContact> contact = VCardContact::Load(u"signed.vcf");
if (contact->get_Security() != nullptr && !String::IsNullOrEmpty(contact->get_Security()->get_Key()))
{
Console::WriteLine(u"Key type: " + contact->get_Security()->get_Type());
// export the embedded public key / certificate to a PEM file
contact->get_Security()->SaveToPEM(u"public-key.pem");
}
Load a vCard
The static Load method reads a single contact from a path or from a stream. When the file was written in a legacy code page, pass a VCardLoadOptions object whose PreferredEncoding names that encoding.
// load from a file
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
Console::WriteLine(contact->get_IdentificationInfo()->get_DisplayName());
// load from a stream
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"bertha.vcf");
SharedPtr<VCardContact> fromStream = VCardContact::Load(stream);
stream->Close();
// control the text encoding used while reading
SharedPtr<VCardLoadOptions> loadOptions = MakeObject<VCardLoadOptions>();
loadOptions->set_PreferredEncoding(System::Text::Encoding::get_UTF8());
SharedPtr<VCardContact> decoded = VCardContact::Load(u"bertha.vcf", loadOptions);
Console::WriteLine(decoded->get_IdentificationInfo()->get_DisplayName());
Read Several Contacts from One File
Address-book exports frequently place many BEGIN:VCARD ... END:VCARD blocks in one file. IsMultiContacts detects that case, and LoadAsMultiple returns all contacts as a list. Both have file, stream and load-options overloads. When the stream overloads are combined, rewind the stream between the check and the load.
if (VCardContact::IsMultiContacts(u"contacts.vcf"))
{
// read every contact stored in the file
auto contacts = VCardContact::LoadAsMultiple(u"contacts.vcf");
for (SharedPtr<VCardContact> contact : contacts)
{
Console::WriteLine(contact->get_IdentificationInfo()->get_DisplayName());
}
}
// the same check and load work on a stream
SharedPtr<IO::FileStream> stream = IO::File::OpenRead(u"contacts.vcf");
if (VCardContact::IsMultiContacts(stream))
{
stream->Seek(0, IO::SeekOrigin::Begin);
Console::WriteLine(u"Contacts in stream: " + Convert::ToString(VCardContact::LoadAsMultiple(stream)->get_Count()));
}
stream->Close();
Save Options
VCardSaveOptions controls the output:
- Version - VCardVersion
V21(default),V30orV40; - PreferredTextEncoding - encoding of the written text;
- UseExtensions - whether
X-extension properties are written (trueby default); - ProductId - the
PRODIDvalue.
SharedPtr<VCardContact> contact = VCardContact::Load(u"bertha.vcf");
SharedPtr<VCardSaveOptions> saveOptions = MakeObject<VCardSaveOptions>();
saveOptions->set_Version(VCardVersion::V30); // write vCard 3.0 (default is V21)
saveOptions->set_PreferredTextEncoding(System::Text::Encoding::get_UTF8()); // encoding used when writing
saveOptions->set_UseExtensions(true); // allow extended (X-) properties
saveOptions->set_ProductId(u"My Company"); // PRODID value
contact->Save(u"contact_v3.vcf", saveOptions);
See Also
- Create, Save, and Load Outlook Contacts -
MapiContact, including VCF import and export. - Working with Contacts in PST File - contacts stored inside a PST.