Tạo, lưu và tải danh bạ Outlook trong C++

Aspose.Email cho C++ cho phép bạn tạo và quản lý danh bạ Microsoft Outlook một cách lập trình. The MapiContact lớp cung cấp tất cả các thuộc tính liên quan đến danh bạ bao gồm tên, thông tin chuyên môn, địa chỉ, tài khoản email, số điện thoại, danh mục, trường tùy chỉnh và ảnh.

Bài viết này minh họa cách tạo, lưu và đọc danh bạ Outlook ở định dạng MSG và vCard (VCF).

Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:

#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;

Tạo và lưu một danh bạ Outlook

Để tạo một danh bạ Outlook và lưu nó vào đĩa, hãy làm theo các bước dưới đây:

  1. Tạo thể hiện MapiContact.
  2. Điền tên, thông tin chuyên môn, cá nhân và địa chỉ.
  3. Thêm địa chỉ điện tử và số điện thoại.
  4. Gán các danh mục, số km, phí thanh toán và các trường tùy chỉnh.
  5. Tùy chọn, nhúng ảnh của danh bạ.
  6. Lưu danh bạ ở định dạng MSG hoặc vCard (VCF).

Chi tiết của một liên hệ được nhóm thành các bộ thuộc tính: MapiContactNamePropertySet cho tên, MapiContactProfessionalPropertySet cho công ty và chức danh, MapiContactPhysicalAddressPropertySet cho địa chỉ bưu điện, MapiContactElectronicAddressPropertySet cho địa chỉ email và fax, MapiContactTelephonePropertySet cho các số điện thoại và MapiContactOtherPropertySet cho các trường còn lại như lời nhắc và các trường người dùng. MapiContact::Save ghi liên hệ dưới dạng MSG hoặc vCard, tùy thuộc vào 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);

Đọc danh bạ bằng MapiContact

Cái MapiContact lớp có thể tải danh bạ Outlook được lưu ở cả định dạng MSG và VCF.

Tải một danh bạ từ MSG

Một tệp MSG chứa liên hệ được tải giống như bất kỳ mục Outlook nào khác, với MapiMessage::Load. ToMapiMessageItem sau đó trả về mục như một MapiContact. Kiểm tra get_SupportedType đầu tiên đảm bảo tệp thực sự chứa một liên hệ - DynamicCast ném ngoại lệ khi mục là kiểu khác.

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());
}

Tải danh bạ từ vCard (VCF)

Một tệp vCard có thể được đọc theo hai cách. VCardContact::Load cung cấp một VCardContact một cấu trúc phản ánh cấu trúc vCard - lựa chọn phù hợp khi dữ liệu ở dạng vCard (xem Làm việc với Tệp vCard). MapiContact::FromVCard chuyển vCard trực tiếp thành liên hệ Outlook, sẵn sàng lưu dưới dạng MSG hoặc thêm vào tệp 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());

Control How a vCard Is Read and Written

MapiContact::FromVCard có các overload nhận một VCardLoadOptions đối tượng hoặc một mã hoá, điều này quan trọng đối với các tệp được ghi bằng trang mã kế thừa. Khi xuất ra, VCardSaveOptions chọn phiên bản vCard và quyết định qua UseExtensions liệu phần mở rộng X- các thuộc tính có thể được ghi - tắt nó đi khi ứng dụng đích chỉ hiểu các trường tiêu chuẩn.

// 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);

Distribution Lists and Multi-Contact VCV Files

Một tệp VCF chứa nhiều liên hệ được ánh xạ vào một MapiDistributionList: FromVCF đọc một tệp như vậy, và MapiDistributionListSaveOptions với ContactSaveFormat::VCard ghi một lại.

// 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);

Render a Contact as MHTML

Thẻ liên hệ có thể được xuất bản dưới dạng MHTML - một trang thân thiện với email hiển thị các trường như Outlook. Liên hệ được lưu vào dòng MSG, rồi đọc lại như một MapiMessage, chuyển đổi thành một MailMessage và lưu với MhtFormatOptions::RenderVCardInfo. RenderedContactFields chọn các nhóm trường sẽ xuất hiện, từ 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);

Đọc và Đặt Định danh Sản phẩm cho một Liên hệ

Một tệp vCard ghi lại sản phẩm tạo ra nó trong PRODID trường. Aspose.Email giữ định danh này khi liên hệ được chuyển đổi giữa vCard và MSG, vì vậy bạn luôn có thể truy vết nguồn gốc của liên hệ. Định danh có sẵn thông qua các thành viên sau:

  • MapiContact::get_ProductId() và MapiContact::set_ProductId() - lấy hoặc đặt định danh của sản phẩm đã tạo một MapiContact.
  • ContactSaveOptions::set_ProductId() - đặt định danh được ghi khi liên hệ được lưu. Phương thức này có trong tất cả các tùy chọn lưu liên hệ, bao gồm MapiContactSaveOptions và VCardSaveOptions.
  • VCardExplanatoryInfo::get_ProdId() - trả về định danh của một liên hệ được tải bằng VCardContact lớp.

Đoạn mã mẫu sau đọc định danh sản phẩm từ tệp vCard, chuyển đổi liên hệ sang MSG và lưu với định danh mới:

#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);
}

Để đặt định danh cho một liên hệ mới, gọi set_ProductId() hoặc truyền định danh qua VCardSaveOptions khi lưu liên hệ dưới dạng 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);

Xem Thêm