Arbeiten mit vCard (VCF)-Dateien in C++
vCard ist das Austauschformat für Kontaktdaten, das von Telefonen, Mail‑Clients und Verzeichnisdiensten verstanden wird. Aspose.Email für C++ verarbeitet es über das VCardContact Klasse im Aspose::Email::PersonalInfo::VCard Namespace – ein reines vCard‑Objektmodell, das weder Outlook noch MAPI benötigt. Wenn Ihre Kontakte in MSG‑ oder PST‑Dateien leben, verwenden Sie MapiContact stattdessen; es kann auch aus VCF importieren und zu VCF exportieren, wie gezeigt in Outlook‑Kontakte erstellen, speichern und laden.
Die nachfolgenden Beispiele verwenden die folgenden Header und Namensräume:
#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;
Auswahl zwischen VCardContact und MapiContact
| | VCardContact | MapiContact | | — | — | — | | Natives Format | vCard 2.1 / 3.0 / 4.0 | Outlook‑MSG, PST‑Einträge | | Typische Quelle | .vcf Dateien, Telefon‑Exporte, LDAP‑Dumps | Outlook, Exchange, PST‑Archive | | Objektmodell | Spiegelt die vCard‑Eigenschaften (N, FN, ADR, TEL, GEO, KEY …) wider | Spiegelt das Outlook‑Kontaktformular und MAPI‑Eigenschaften wider | | Mehrfach‑Kontakt‑Dateien | Ja, über LoadAsMultiple | Ein Kontakt pro Datei; ein Mehrfach‑Kontakt‑VCF lädt als ein MapiDistributionList |
Der Rest dieses Artikels behandelt VCardContact.
Einen Kontakt erstellen und als VCF speichern
Ein VCardContact beginnt leer; Daten werden durch gruppierte Eigenschaftsobjekte hinzugefügt. VCardIdentificationInfo enthält die namensbezogenen Felder (seine VollständigerName Mitglied speichert das strukturierte N Eigenschaft), VCardOrganization die Firma und Titel sowie das E‑Mails sammelt die E‑Mail‑Adressen. Speichern schreibt die Datei standardmäßig im vCard‑2.1‑Format.
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");
Den Rest des Modells ausfüllen
Die nachfolgenden Snippets laden den oben erstellten Kontakt, fügen eine Gruppe von Eigenschaften hinzu und speichern ihn erneut. Aufzählungen, die vCard‑Typ‑Parameter (Adressart, Telefonart, E‑Mail‑Typ) darstellen, sind Flag‑Aufzählungen, sodass mehrere Werte kombiniert werden können mit |.
Postadressen
Ein VCardDeliveryAddress entspricht einem ADR Zeile. Sein AddressType nimmt 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");
Telefonnummern
Jedes VCardTelephoneNumber wird zu einem TEL Zeile; VCardTelephoneType liefert VOICE, WORK, HOME, CELL, FAX, PAGER, PREF und mehr.
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");
Geografische Position
VCardGeo speichert das GEO Eigenschaft als Breitengrad‑/Längengrad‑Paar von float Werte.
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");
Foto
Das Bild wird über die Foto Eigenschaft des Identifikationsblocks als ein VCardPhoto. ValueLocation wählt zwischen dem Einbetten der Bytes (INLINE) und verweist auf eine externe Datei (URL); PhotoType benennt das Bildformat.
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");
Das Modell wieder einlesen
Nach dem Laden geben dieselben Eigenschaftsgruppen die geparsten Werte frei. ObjectExt::ToString rendert eine Flag‑Aufzählung als lesbare Liste wie 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()));
}
Öffentlicher Schlüssel oder Zertifikat
Die KEY Eigenschaft einer vCard – ein öffentlicher Schlüssel oder ein X.509‑Zertifikat – wird über VCardSecurity. Schlüssel enthält den codierten Wert, Typ seiner deklarierten Art, und SaveToPEM schreibt es als PEM‑Datei mit dem üblichen BEGIN CERTIFICATE / END CERTIFICATE Zaun.
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");
}
Eine vCard laden
Die statische Load Methode liest einen einzelnen Kontakt aus einem Pfad oder einem Stream. Wenn die Datei in einer veralteten Code‑Page geschrieben wurde, übergeben Sie eine VCardLoadOptions Objekt, dessen PreferredEncoding namen diese Kodierung.
// 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());
Mehrere Kontakte aus einer Datei lesen
Adressbuch‑Exporte platzieren häufig viele BEGIN:VCARD ... END:VCARD blöcke in einer Datei. IsMultiContacts Erkennt diesen Fall und LoadAsMultiple Gibt alle Kontakte als Liste zurück. Beide haben Überladungen für Datei, Stream und Lade‑Optionen. Wenn die Stream‑Überladungen kombiniert werden, spulen Sie den Stream zwischen Prüfung und Laden zurück.
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();
Speicheroptionen
VCardSaveOptions Steuert die Ausgabe:
- Version - VCardVersion
V21(Standard),V30oderV40; - PreferredTextEncoding - Kodierung des geschriebenen Textes;
- UseExtensions - ob
X-Erweiterungseigenschaften werden geschrieben (truestandardmäßig); - ProductId - das
PRODIDWert.
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);
Siehe auch
- Outlook‑Kontakte erstellen, speichern und laden -
MapiContact, einschließlich VCF‑Import und -Export. - Arbeiten mit Kontakten in PST‑Datei - Kontakte, die in einem PST gespeichert sind.