ایجاد، ذخیره و بارگذاری مخاطبین Outlook در C++
Aspose.Email برای 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 و ذخیرهاش بر روی دیسک، مراحل زیر را دنبال کنید:
- نمونهسازی 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 کلاس میتواند مخاطبین 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 بارگذاریهای مختلفی دارد که یک 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 VCF 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);
همچنین ببینید
- کار با فایلهای vCard (VCF) - فرمت-نابسته
VCardContactکلاس. - کار با مخاطبین در فایل PST - مخاطبانی که داخل یک PST ذخیره شدهاند.