C++에서 Outlook 연락처 생성, 저장 및 로드
**Aspose.Email for C++**를 사용하면 Microsoft Outlook 연락처를 프로그래밍 방식으로 생성하고 관리할 수 있습니다. MapiContact 해당 클래스는 이름, 직업 정보, 주소, 이메일 계정, 전화 번호, 카테고리, 사용자 정의 필드 및 사진을 포함한 모든 연락처 관련 속성을 제공합니다.
이 문서에서는 MSG 및 vCard (VCF) 형식으로 Outlook 연락처를 생성, 저장 및 읽는 방법을 보여줍니다.
이 문서의 샘플은 다음 헤더와 네임스페이스를 사용합니다:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <MhtSaveOptions.h>
#include <MhtFormatOptions.h>
#include <MsgLoadOptions.h>
#include <ContactFieldsSet.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactNamePropertySet.h>
#include <Mapi/MapiContactProfessionalPropertySet.h>
#include <Mapi/MapiContactPersonalInfoPropertySet.h>
#include <Mapi/MapiContactPhysicalAddressPropertySet.h>
#include <Mapi/MapiContactPhysicalAddress.h>
#include <Mapi/MapiContactElectronicAddressPropertySet.h>
#include <Mapi/MapiContactElectronicAddress.h>
#include <Mapi/MapiContactTelephonePropertySet.h>
#include <Mapi/MapiContactOtherPropertySet.h>
#include <Mapi/MapiContactPhoto.h>
#include <Mapi/MapiContactPhotoImageFormat.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiDistributionList.h>
#include <Mapi/MapiDistributionListSaveOptions.h>
#include <Mapi/MapiDistributionListMemberCollection.h>
#include <Mapi/ContactSaveFormat.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MailConversionOptions.h>
#include <PersonalInfo/vCard/VCardContact.h>
#include <PersonalInfo/vCard/VCardIdentificationInfo.h>
#include <PersonalInfo/vCard/VCardLoadOptions.h>
#include <PersonalInfo/vCard/VCardSaveOptions.h>
#include <PersonalInfo/vCard/VCardVersion.h>
#include <system/io/memory_stream.h>
#include <system/text/encoding.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::PersonalInfo::VCard;
Outlook 연락처 생성 및 저장
Outlook 연락처를 생성하고 디스크에 저장하려면 아래 단계를 따르세요:
- 인스턴스화 MapiContact.
- 이름, 직업, 개인 및 주소 세부 정보를 채웁니다.
- 전자 주소와 전화 번호를 추가합니다.
- 카테고리, 마일리지, 청구 및 사용자 정의 필드를 할당합니다.
- 선택적으로 연락처 사진을 삽입합니다.
- 연락처를 MSG 또는 vCard (VCF) 형식으로 저장합니다.
연락처 세부 정보는 속성 집합으로 그룹화됩니다: MapiContactNamePropertySet 이름에 대해, MapiContactProfessionalPropertySet 회사 및 직함에 대해, MapiContactPhysicalAddressPropertySet 우편 주소에 대해, MapiContactElectronicAddressPropertySet 이메일 및 팩스 주소에 대해, MapiContactTelephonePropertySet 전화번호에 대해 MapiContactOtherPropertySet 리마인더 및 사용자 필드와 같은 나머지 필드에 대해. MapiContact::Save 연락처를 MSG 또는 vCard로 저장합니다, 이는 ContactSaveFormat.
SharedPtr<MapiContact> contact = MakeObject<MapiContact>();
contact->set_NameInfo(MakeObject<MapiContactNamePropertySet>(u"Bertha", u"A.", u"Buell"));
contact->set_ProfessionalInfo(MakeObject<MapiContactProfessionalPropertySet>(u"Awthentikz", u"Social work assistant"));
contact->get_PersonalInfo()->set_PersonalHomePage(u"B2BTies.com");
contact->get_PhysicalAddresses()->get_WorkAddress()->set_Address(u"Im Astenfeld 59 8580 EDELSCHROTT");
contact->get_ElectronicAddresses()->set_Email1(MakeObject<MapiContactElectronicAddress>(u"Experwas", u"SMTP", u"BerthaABuell@armyspy.com"));
contact->set_Telephones(MakeObject<MapiContactTelephonePropertySet>(u"06605045265"));
contact->get_PersonalInfo()->set_Children(MakeArray<String>({u"child1", u"child2", u"child3"}));
contact->set_Categories(MakeArray<String>({u"category1", u"category2", u"category3"}));
contact->set_Mileage(u"Some test mileage");
contact->set_Billing(u"Test billing information");
contact->get_OtherFields()->set_Journal(true);
contact->get_OtherFields()->set_Private(true);
contact->get_OtherFields()->set_ReminderTime(DateTime(2014, 1, 1, 0, 0, 55));
contact->get_OtherFields()->set_ReminderTopic(u"Test topic");
contact->get_OtherFields()->set_UserField1(u"ContactUserField1");
contact->get_OtherFields()->set_UserField2(u"ContactUserField2");
// a photo is stored with the format of its data
contact->set_Photo(MakeObject<MapiContactPhoto>(IO::File::ReadAllBytes(u"portrait.jpg"), MapiContactPhotoImageFormat::Jpeg));
contact->Save(u"Contact_out.msg", ContactSaveFormat::Msg);
contact->Save(u"Contact_out.vcf", ContactSaveFormat::VCard);
MapiContact를 사용하여 연락처 읽기
다음은 MapiContact 해당 클래스는 MSG와 VCF 형식으로 저장된 Outlook 연락처를 모두 로드할 수 있습니다.
MSG에서 연락처 로드
연락처를 포함한 MSG 파일은 다른 Outlook 항목과 동일하게 로드됩니다, 다음과 함께 MapiMessage::Load. ToMapiMessageItem 그런 다음 항목을 MapiContact. 검사 중 get_SupportedType 먼저 파일이 실제로 연락처를 포함하고 있는지 확인합니다 - DynamicCast 항목 타입이 다르면 예외가 발생합니다.
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"Contact_out.msg");
if (msg->get_SupportedType() == MapiItemType::Contact)
{
SharedPtr<MapiContact> mapiContact = DynamicCast<MapiContact>(msg->ToMapiMessageItem());
Console::WriteLine(u"Name: " + mapiContact->get_NameInfo()->get_DisplayName());
Console::WriteLine(u"Company: " + mapiContact->get_ProfessionalInfo()->get_CompanyName());
Console::WriteLine(u"E-mail: " + mapiContact->get_ElectronicAddresses()->get_Email1()->get_EmailAddress());
}
vCard (VCF)에서 연락처 로드
vCard 파일은 두 가지 방법으로 읽을 수 있습니다. VCardContact::Load 제공합니다 VCardContact vCard 구조를 그대로 반영합니다 - 데이터가 vCard 형태로 유지될 때 적합합니다(see vCard 파일 작업). MapiContact::FromVCard vCard를 Outlook 연락처로 바로 변환하여 MSG로 저장하거나 PST 파일에 추가할 수 있습니다.
// the vCard object model
SharedPtr<VCardContact> vcard = VCardContact::Load(u"Contact_out.vcf");
Console::WriteLine(u"vCard display name: " + vcard->get_IdentificationInfo()->get_DisplayName());
// the same file as an Outlook contact
SharedPtr<MapiContact> contact = MapiContact::FromVCard(u"Contact_out.vcf");
Console::WriteLine(u"MapiContact display name: " + contact->get_NameInfo()->get_DisplayName());
vCard 읽기 및 쓰기 제어
MapiContact::FromVCard 다양한 오버로드가 있으며 VCardLoadOptions 객체 또는 인코딩, 이는 레거시 코드 페이지로 기록된 파일에 영향을 줍니다. 출력 시, VCardSaveOptions 선택합니다 vCard 버전 그리고 다음을 통해 결정합니다 UseExtensions 확장된 여부 X- 속성이 기록될 수 있습니다 - 대상 애플리케이션이 표준 필드만 이해하는 경우 이를 끄세요.
// load a vCard with explicit options
SharedPtr<VCardLoadOptions> loadOptions = MakeObject<VCardLoadOptions>();
loadOptions->set_PreferredEncoding(System::Text::Encoding::get_UTF8());
SharedPtr<MapiContact> contact = MapiContact::FromVCard(u"Contact.vcf", loadOptions);
Console::WriteLine(contact->get_NameInfo()->get_DisplayName());
// write it back as vCard 3.0, without the extended X- properties
SharedPtr<VCardSaveOptions> saveOptions = MakeObject<VCardSaveOptions>();
saveOptions->set_Version(VCardVersion::V30);
saveOptions->set_UseExtensions(false);
contact->Save(u"ContactV3.vcf", saveOptions);
배포 목록 및 다중 연락처 VCF 파일
여러 연락처를 보유한 VCF 파일은 MapiDistributionList: FromVCF 그러한 파일을 읽고, 그리고 MapiDistributionListSaveOptions 와 함께 ContactSaveFormat::VCard 하나를 다시 씁니다.
// a multi-contact VCF file becomes a distribution list
SharedPtr<MapiDistributionList> dlist = MapiDistributionList::FromVCF(u"contacts.vcf");
Console::WriteLine(u"Members: " + Convert::ToString(dlist->get_Members()->get_Count()));
// and a distribution list can be written back as a multi-contact VCF file
SharedPtr<MapiDistributionListSaveOptions> options = MakeObject<MapiDistributionListSaveOptions>(ContactSaveFormat::VCard);
options->set_Version(VCardVersion::V30);
dlist->Save(u"distribution_list.vcf", options);
연락처를 MHTML로 렌더링
연락처 카드는 MHTML로 게시될 수 있습니다 - Outlook처럼 필드를 표시하는 이메일 친화적 페이지입니다. 연락처는 MSG 스트림에 저장되고, 다시 읽어올 때는 MapiMessage, 로 변환됩니다 MailMessage 및 저장됩니다 MhtFormatOptions::RenderVCardInfo. RenderedContactFields 필드 그룹을 선택합니다, 여기서 ContactFieldsSet.
SharedPtr<MapiContact> contact = MapiContact::FromVCard(u"Contact.vcf");
// the contact travels through an MSG stream into a MailMessage
SharedPtr<IO::MemoryStream> stream = MakeObject<IO::MemoryStream>();
contact->Save(stream, ContactSaveFormat::Msg);
stream->set_Position(0);
SharedPtr<MapiMessage> msg = MapiMessage::Load(stream, MakeObject<MsgLoadOptions>());
SharedPtr<MailMessage> eml = msg->ToMailMessage(MakeObject<MailConversionOptions>());
// RenderVCardInfo writes the contact card into the body
SharedPtr<MhtSaveOptions> mhtSaveOptions = MakeObject<MhtSaveOptions>();
mhtSaveOptions->set_CheckBodyContentEncoding(true);
mhtSaveOptions->set_PreserveOriginalBoundaries(true);
mhtSaveOptions->set_MhtFormatOptions(MhtFormatOptions::WriteHeader | MhtFormatOptions::RenderVCardInfo);
// and these are the field groups it contains
mhtSaveOptions->set_RenderedContactFields(ContactFieldsSet::NameInfo | ContactFieldsSet::PersonalInfo |
ContactFieldsSet::Telephones | ContactFieldsSet::Events);
eml->Save(u"ContactCard.mhtml", mhtSaveOptions);
연락처의 제품 식별자 읽기 및 설정
vCard 파일은 만든 제품을 다음에 기록합니다 PRODID 필드. Aspose.Email은 vCard와 MSG 간에 연락처를 변환할 때 이 식별자를 유지하므로 언제든지 연락처의 출처를 추적할 수 있습니다. 다음 멤버를 통해 식별자에 접근합니다:
MapiContact::get_ProductId()및MapiContact::set_ProductId()- 해당 항목을 만든 제품의 식별자를 가져오거나 설정합니다 MapiContact.ContactSaveOptions::set_ProductId()- 연락처를 저장할 때 기록되는 식별자를 설정합니다. 모든 연락처 저장 옵션에 이 메서드가 포함되어 있습니다, 예를 들어 MapiContactSaveOptions 및 VCardSaveOptions.- VCardExplanatoryInfo::get_ProdId() - 로드된 연락처의 식별자를 반환합니다 VCardContact 클래스.
다음 코드 샘플은 vCard 파일에서 제품 식별자를 읽고, 연락처를 MSG로 변환한 뒤 새 식별자로 저장하는 과정을 보여줍니다:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactSaveOptions.h>
#include <Mapi/ContactSaveFormat.h>
#include <Mapi/MapiItemType.h>
#include <PersonalInfo/vCard/VCardContact.h>
#include <PersonalInfo/vCard/VCardExplanatoryInfo.h>
#include <system/io/memory_stream.h>
#include <system/console.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::PersonalInfo::VCard;
// Read the product identifier from a vCard file
SharedPtr<VCardContact> vcard = VCardContact::Load(u"contact.vcf");
Console::WriteLine(u"vCard product ID: " + vcard->get_ExplanatoryInfo()->get_ProdId());
// The identifier is kept when the contact is converted to MSG
auto msgStream = MakeObject<IO::MemoryStream>();
vcard->Save(msgStream, ContactSaveFormat::Msg);
msgStream->set_Position(0);
SharedPtr<MapiMessage> msg = MapiMessage::Load(msgStream);
if (msg->get_SupportedType() == MapiItemType::Contact)
{
auto contact = System::DynamicCast<MapiContact>(msg->ToMapiMessageItem());
Console::WriteLine(u"MapiContact product ID: " + contact->get_ProductId());
// Save the contact to MSG with a different product identifier
auto options = MakeObject<MapiContactSaveOptions>();
options->set_ProductId(u"-//Contoso//Address Book 2.0//EN");
contact->Save(u"contact_out.msg", options);
}
새 연락처의 식별자를 설정하려면 다음을 호출합니다 set_ProductId() 또는 식별자를 통해 전달하거나 VCardSaveOptions 연락처를 vCard로 저장할 때:
#include <Mapi/MapiContact.h>
#include <Mapi/ContactSaveFormat.h>
#include <PersonalInfo/vCard/VCardSaveOptions.h>
using namespace System;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::PersonalInfo::VCard;
auto contact = MakeObject<MapiContact>(u"John Smith", String(u"john.smith@example.com"));
contact->set_ProductId(u"-//Contoso//Address Book 2.0//EN");
contact->Save(u"contact_out.vcf", ContactSaveFormat::VCard);
// Override the identifier for a single save operation
auto options = MakeObject<VCardSaveOptions>();
options->set_ProductId(u"-//Contoso//Export Tool 1.0//EN");
contact->Save(u"contact_export.vcf", options);
관련 항목
- Work with vCard (VCF) Files - 형식에 구애받지 않는
VCardContact클래스. - PST 파일에서 연락처 작업 - PST에 저장된 연락처.