在 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 联系人并保存到磁盘,请按照以下步骤操作:

  1. 实例化 MapiContact.
  2. 填写姓名、职业、个人和地址详情。
  3. 添加电子邮件地址和电话号码。
  4. 分配类别、里程、计费和自定义字段。
  5. 可选地,嵌入联系人照片。
  6. 将联系人保存为 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 格式时的正确选择(参见 使用 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 之间转换时保留此标识符,因此您始终可以追溯联系人的来源。该标识符可通过以下成员获取:

以下代码示例从 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);

另见