สร้าง, บันทึก, และโหลดที่อยู่ติดต่อ Outlook ใน C++

Aspose.Email for C++ ให้คุณสร้างและจัดการที่อยู่ติดต่อ Microsoft Outlook ผ่านโปรแกรมได้. MapiContact คลาสให้คุณสมบัติที่เกี่ยวกับที่อยู่ติดต่อทั้งหมด รวมถึงชื่อ, ข้อมูลอาชีพ, ที่อยู่, บัญชีอีเมล, หมายเลขโทรศัพท์, หมวดหมู่, ฟิลด์ที่กำหนดเอง, และรูปภาพ.

บทความนี้แสดงวิธีสร้าง, บันทึก, และอ่านที่อยู่ติดต่อ Outlook ในรูปแบบ MSG และ vCard (VCF).

ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:

#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 คลาสสามารถโหลดที่อยู่ติดต่อ Outlook ที่บันทึกในรูปแบบ MSG และ VCF ได้.

โหลดผู้ติดต่อจาก 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());

Control How a vCard Is Read and Written

MapiContact::FromVCard มี overloads ที่รับ 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);

Distribution Lists and Multi-Contact VCV Files

ไฟล์ 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);

Render a Contact as 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);

ดูเพิ่มเติม