C++에서 vCard(VCF) 파일 작업
vCard는 전화기, 메일 클라이언트 및 디렉터리 서비스가 이해하는 연락처 데이터 교환 형식입니다. Aspose.Email for C++는 이를 VCardContact 클래스는 Aspose::Email::PersonalInfo::VCard 네임스페이스 - Outlook이나 MAPI가 필요 없는 순수 vCard 객체 모델입니다. 연락처가 MSG 또는 PST 파일에 있다면 MapiContact 대신; VCF에서 가져오고 내보낼 수도 있으며, 아래에 표시된 대로 Outlook 연락처 생성, 저장 및 로드.
아래 샘플은 다음 헤더와 네임스페이스를 사용합니다:
#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;
VCardContact와 MapiContact 선택
| | VCardContact | MapiContact | | — | — | — | | 기본 형식 | vCard 2.1 / 3.0 / 4.0 | Outlook MSG, PST 항목 | | 일반 소스 | .vcf 파일, 전화 내보내기, LDAP 덤프 | Outlook, Exchange, PST 아카이브 | | 객체 모델 | vCard 속성 (N, FN, ADR, TEL, GEO, KEY …)을 반영 | Outlook 연락처 양식 및 MAPI 속성을 반영 | | 다중 연락처 파일 | 예, 통해 LoadAsMultiple | 파일당 하나의 연락처; 다중 연락처 VCF는 MapiDistributionList |
이 기사 나머지 부분은 VCardContact.
연락처 생성 및 VCF로 저장
A VCardContact 비어 있습니다; 데이터는 그룹화된 속성 객체를 통해 추가됩니다. VCardIdentificationInfo 이름 관련 필드를 보유합니다 (그것의 전체이름 구성원은 구조화된 N 속성), VCardOrganization 회사와 직책, 그리고 이메일 이메일 주소를 수집합니다. 저장 기본적으로 vCard 2.1 형식으로 파일을 씁니다.
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");
모델의 나머지 부분 채우기
아래 스니펫은 위에서 만든 연락처를 로드하고, 하나의 속성 그룹을 추가한 뒤 다시 저장합니다. 주소 유형, 전화 유형, 이메일 유형과 같은 vCard 유형 매개변수를 나타내는 열거형은 플래그 열거형이므로 여러 값을 |.
우편 주소
A VCardDeliveryAddress 하나에 매핑됩니다 ADR 줄. 그 AddressType 받습니다 VCardDeliveryAddressType 플래그: 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");
전화 번호
각 VCardTelephoneNumber 가 됩니다 TEL 줄; VCardTelephoneType 제공합니다 VOICE, WORK, HOME, CELL, FAX, PAGER, PREF 그리고 더 많은 내용.
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");
지리적 위치
VCardGeo 보관합니다 GEO 위도/경도 쌍으로서의 속성 float 값들을.
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");
사진
그 사진은 사진 식별 블록의 속성으로서 VCardPhoto. ValueLocation 바이트를 삽입하는 것 중 선택합니다 (INLINE) 및 외부 파일을 가리키는 (URL); PhotoType 이미지 형식을 명명합니다.
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");
모델 다시 읽기
로드 후, 동일한 속성 그룹이 파싱된 값을 노출합니다. ObjectExt::ToString 플래그 열거형을 읽기 쉬운 목록으로 렌더링합니다 예를 들어 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()));
}
공개 키 또는 인증서
다음은 KEY vCard의 속성 - 공개 키 또는 X.509 인증서 - 는 통해 노출됩니다 VCardSecurity. 키 인코딩된 값을 보유합니다, 유형 선언된 종류와 SaveToPEM 일반적인 BEGIN CERTIFICATE / END CERTIFICATE 펜스.
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");
}
vCard 로드
정적 Load 메서드는 경로나 스트림에서 단일 연락처를 읽습니다. 파일이 레거시 코드 페이지로 작성된 경우, 해당 VCardLoadOptions 해당 객체는 PreferredEncoding 그 인코딩을 이름으로 지정합니다.
// 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());
하나의 파일에서 여러 연락처 읽기
주소록 내보내기는 종종 많은 BEGIN:VCARD ... END:VCARD 하나의 파일에 블록을 저장합니다. IsMultiContacts 해당 경우를 감지하고, LoadAsMultiple 모든 연락처를 리스트로 반환합니다. 두 메서드 모두 파일, 스트림 및 로드 옵션 오버로드를 제공합니다. 스트림 오버로드를 결합할 경우, 확인과 로드 사이에 스트림을 다시 위치시킵니다.
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();
저장 옵션
VCardSaveOptions 출력을 제어합니다:
- Version - VCardVersion
V21(기본값),V30또는V40; - PreferredTextEncoding - 기록된 텍스트의 인코딩;
- UseExtensions - 여부
X-확장 속성이 기록됩니다 (true기본값); - ProductId -
PRODID값.
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);
관련 항목
- Outlook 연락처 생성, 저장 및 로드 -
MapiContact, VCF 가져오기 및 내보내기 포함. - PST 파일에서 연락처 작업 - PST에 저장된 연락처.