เข้าถึงและจัดการคุณสมบัติ MAPI ของ Outlook
MAPI properties คือรายการเมทาดาต้าที่ใช้ในข้อความ Microsoft Outlook, กำหนดแอตทริบิวต์เช่น ผู้ส่ง, ผู้รับ, เรื่อง, ไฟล์แนบ, และข้อมูลที่กำหนดเอง.
Aspose.Email for C++ อนุญาตให้ผู้พัฒนาสามารถเข้าถึง, แก้ไข, และลบคุณสมบัติเหล่านี้โดยโปรแกรมใน MapiMessage อ็อบเจ็กต์, ไฟล์แนบ, และคุณสมบัติที่มีชื่อ.
นี้ MapiProperty คลาสนี้แสดงถึงคุณสมบัติ MAPI ซึ่งประกอบด้วย:
- Name – ตัวระบุแบบสตริงของคุณสมบัติ.
- Tag – ตัวระบุเชิงตัวเลขที่ใช้ในการอ้างอิงคุณสมบัติ.
- Data – อาร์เรย์ไบต์ที่เป็นตัวแทนของค่าคุณสมบัติ.
ตัวอย่างในบทความนี้ใช้ส่วนหัวและเนมสเปซต่อไปนี้:
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiMessageFlags.h>
#include <Mapi/BodyContentType.h>
#include <Mapi/MapiProperty.h>
#include <Mapi/MapiPropertyContainer.h>
#include <Mapi/MapiNamedProperty.h>
#include <Mapi/MapiNamedPropertyMappingStorage.h>
#include <Mapi/MapiPropertyCollection.h>
#include <Mapi/MapiPropertyTag.h>
#include <Mapi/MapiPropertyType.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <system/bit_converter.h>
#include <system/convert.h>
#include <system/guid.h>
#include <system/text/encoding.h>
using namespace System;
using namespace Aspose::Email::Mapi;
อ่านคุณสมบัติ MAPI
ทุกคุณสมบัติของข้อความจะถูกเก็บในคอลเล็กชันที่ส่งคืนโดย get_Properties, ดัชนีตามแท็กของมัน; ส่วน แท็กคุณสมบัติ Mapi คลาสตั้งชื่อแท็กมาตรฐาน การค้นหาแท็กที่ข้อความไม่มีจะคืนค่า nullptr.
หลายคุณสมบัติข้อความมีสองรูปแบบ - แท็ก ANSI เช่น PR_SUBJECT และแบบ Unicode เช่น PR_SUBJECT_W - และข้อความหนึ่งบรรจุอันใดอันหนึ่งขึ้นอยู่กับรูปแบบที่บันทึก ตัวอย่างลองทั้งสองแบบ MapiProperty จากนั้นเปลี่ยนข้อมูลดิบของมันด้วยวิธีต่าง ๆ เช่น GetString หรือ GetInt32.
SharedPtr<MapiMessage> msg = MapiMessage::Load(u"Message.msg");
SharedPtr<MapiProperty> prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_SUBJECT);
if (prop == nullptr)
{
// the Unicode counterpart of PR_SUBJECT
prop = msg->get_Properties()->idx_get(MapiPropertyTag::PR_SUBJECT_W);
}
if (prop != nullptr)
{
Console::WriteLine(u"Subject: " + prop->GetString());
}
// the code page of the internet message the item was created from
SharedPtr<MapiProperty> codePage = msg->get_Properties()->idx_get(MapiPropertyTag::PR_INTERNET_CPID);
if (codePage != nullptr)
{
Console::WriteLine(u"Code page: " + Convert::ToString(codePage->GetInt32()));
}
ตั้งค่าคุณสมบัติ MAPI
คุณสมบัติจัดเพิ่มหรือแทนที่ด้วย SetProperty, ซึ่งต้องใช้ MapiProperty สร้างจากแท็กและไบต์ดิบของค่า สตริงถูกเก็บใน UTF-16 สำหรับ Unicode (_W) แท็ก และวันที่ถูกเก็บเป็น FILETIME - จำนวนช่วง 100-nanoseconds ตั้งแต่วันที่ 1 มกราคม 1601 (UTC) - ซึ่ง DateTime::ToFileTimeUtc สร้างขึ้น ผู้รับมีคุณสมบัติเฉพาะของตน ตั้งค่าในลักษณะเดียวกันบน MapiRecipient.
ใน C++ MapiMessage ประกาศ SetProperty การโอเวอร์โหลดของตัวเอง และเหล่านี้ซ่อนไว้การโอเวอร์โหลดทั่วไปของคลาสฐาน MapiPropertyContainer. ตัวอย่างจึงเรียกใช้ผ่าน MapiPropertyContainer ตัวชี้ไปยังข้อความ
ตัวอย่างเปลี่ยนผู้รับของข้อความใหม่เป็นที่อยู่แฟกซ์ ทำเครื่องหมายข้อความว่าเป็นข้อความที่ยังไม่ได้ส่งจากผู้ใช้ปัจจุบันและตั้งเวลาแก้ไข
SharedPtr<MapiMessage> mapiMsg = MakeObject<MapiMessage>(u"user1@gmail.com", u"user2@gmail.com", u"This is subject", u"This is body");
// the general SetProperty overload is declared on the base class
SharedPtr<MapiPropertyContainer> container = mapiMsg;
container->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_SENDER_ADDRTYPE_W, Text::Encoding::get_Unicode()->GetBytes(u"EX")));
// properties of a recipient
SharedPtr<MapiRecipient> recipientTo = mapiMsg->get_Recipients()->idx_get(0);
recipientTo->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_RECEIVED_BY_ADDRTYPE_W, Text::Encoding::get_Unicode()->GetBytes(u"MYFAX")));
recipientTo->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_RECEIVED_BY_EMAIL_ADDRESS_W,
Text::Encoding::get_Unicode()->GetBytes(u"My Fax User@/FN=fax#/VN=voice#/CO=My Company/CI=Local")));
mapiMsg->SetMessageFlags(MapiMessageFlags::MSGFLAG_UNSENT | MapiMessageFlags::MSGFLAG_FROMME);
container->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_RTF_IN_SYNC, BitConverter::GetBytes(static_cast<int64_t>(1))));
// a date property holds a FILETIME value
DateTime modified(2013, 9, 11, 0, 0, 0, DateTimeKind::Utc);
container->SetProperty(MakeObject<MapiProperty>(MapiPropertyTag::PR_LAST_MODIFICATION_TIME, BitConverter::GetBytes(modified.ToFileTimeUtc())));
mapiMsg->Save(u"MapiProperties.msg");
SharedPtr<MapiMessage> saved = MapiMessage::Load(u"MapiProperties.msg");
Console::WriteLine(u"Sender address type: " + saved->get_Properties()->idx_get(MapiPropertyTag::PR_SENDER_ADDRTYPE_W)->GetString());
Console::WriteLine(u"Modification time: " + saved->get_Properties()->idx_get(MapiPropertyTag::PR_LAST_MODIFICATION_TIME)->GetDateTime().ToString());
อ่านคุณสมบัติ MAPI ที่ตั้งชื่อ
Named MAPI properties คือคุณสมบัติกำหนดเองที่ผู้ใช้หรือแอปพลิเคชันเพิ่มเข้าไป แทนการใช้แท็กคงที่ แต่ละคุณสมบัติเช่นนี้ระบุด้วย GUID ของชุดคุณสมบัติพร้อมชื่อหรือรหัสตัวเลข ดังนั้นคุณสมบัติของแอปพลิเคชันต่าง ๆ จะไม่ชนกัน แท็กที่พวกมันถูกเก็บไว้จะถูกกำหนดเมื่อคุณสมบัติเพิ่มเข้าไปในข้อความ
Aspose.Email อนุญาตให้อ่านคุณสมบัติเหล่านี้จากข้อความและไฟล์แนบ.
การอ่านคุณสมบัติ MAPI ที่มีชื่อจากไฟล์ MSG
get_NamedProperties ส่งคืนคุณสมบัติมีชื่อของข้อความ แต่ละค่าในคอลเล็กชันเป็น MapiNamedProperty, ซึ่ง get_NameId ให้ชื่อที่คุณสมบัติเสร็จถูกสร้างด้วย
เพื่อให้มีบางอย่างที่จะอ่าน ตัวอย่างจะเก็บคุณสมบัติมีชื่อสองรายการก่อน คุณสมบัติที่มีชื่อถูกสร้างในสามขั้นตอน: GetNextAvailablePropertyId ของข้อความ NamedPropertyMapping จัดหาแท็กฟรีสำหรับประเภทค่าที่ AddNamedPropertyMapping ผูกแท็กนั้นกับชื่อและ GUID ของชุดคุณสมบัติ SetProperty เก็บค่า
// the property set the custom properties belong to
Guid propertySet = Guid(u"6ED8DA90-450B-101B-98DA-00AA003F1305");
SharedPtr<MapiMessage> source = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"Named properties", u"Body");
SharedPtr<MapiPropertyContainer> container = source;
for (String name : { String(u"TEST"), String(u"MYPROP") })
{
SharedPtr<MapiProperty> property = MakeObject<MapiProperty>(
source->get_NamedPropertyMapping()->GetNextAvailablePropertyId(MapiPropertyType::PT_UNICODE),
Text::Encoding::get_Unicode()->GetBytes(u"value of " + name));
source->get_NamedPropertyMapping()->AddNamedPropertyMapping(property, name, propertySet);
container->SetProperty(property);
}
source->Save(u"NamedProperties.msg");
// read the named properties back
SharedPtr<MapiMessage> message = MapiMessage::Load(u"NamedProperties.msg");
for (auto entry : message->get_NamedProperties())
{
// DynamicCast_noexcept gives nullptr for a value that is not a named property
SharedPtr<MapiNamedProperty> namedProperty = DynamicCast_noexcept<MapiNamedProperty>(entry.get_Value());
if (namedProperty == nullptr)
{
continue;
}
String name = namedProperty->get_NameId();
if (name == u"TEST" || name == u"MYPROP")
{
Console::WriteLine(name + u" = " + namedProperty->GetString());
}
}
การเข้าถึงคุณสมบัติ MAPI ที่มีชื่อในไฟล์แนบ
ไฟล์แนบมีคุณสมบัติชื่อด้วย - แอปพลิเคชันสามารถแท็กไฟล์ทุกไฟล์ที่แนบด้วยตัวระบุของตนเอง ชื่อของคุณสมบัติที่ตั้งชื่อทั้งหมดของข้อความ รวมถึงไฟล์แนบ จะถูกเก็บในแมปของข้อความ ดังนั้นคุณสมบัตินั้นจะลงทะเบียนที่นั่นและตั้งค่าใน MapiAttachment. ส่วน get_NamedProperties คอลเลกชันของไฟล์แนบส่งคืนค่า
Guid propertySet = Guid(u"6ED8DA90-450B-101B-98DA-00AA003F1305");
SharedPtr<MapiMessage> source = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"Attachment properties", u"Body");
source->get_Attachments()->Add(u"report.txt", Text::Encoding::get_UTF8()->GetBytes(u"Report text"));
SharedPtr<MapiProperty> property = MakeObject<MapiProperty>(
source->get_NamedPropertyMapping()->GetNextAvailablePropertyId(MapiPropertyType::PT_UNICODE),
Text::Encoding::get_Unicode()->GetBytes(u"{1B2C3D4E-0000-0000-0000-000000000001}"));
source->get_NamedPropertyMapping()->AddNamedPropertyMapping(property, String(u"CustomAttGuid"), propertySet);
source->get_Attachments()->idx_get(0)->SetProperty(property);
source->Save(u"AttachmentNamedProperty.msg");
SharedPtr<MapiMessage> mapi = MapiMessage::Load(u"AttachmentNamedProperty.msg");
for (auto entry : mapi->get_Attachments()->idx_get(0)->get_NamedProperties())
{
SharedPtr<MapiNamedProperty> namedProperty = DynamicCast_noexcept<MapiNamedProperty>(entry.get_Value());
if (namedProperty != nullptr && String::Compare(namedProperty->get_NameId(), u"CustomAttGuid", StringComparison::OrdinalIgnoreCase) == 0)
{
Console::WriteLine(u"CustomAttGuid = " + namedProperty->GetString());
}
}
การลบคุณสมบัติ MAPI
RemoveProperty ลบคุณสมบัติตามแท็ก - จากข้อความและจากไฟล์แนบหรือผู้รับ ตัวอย่างแนบข้อความ Outlook ไปยังข้อความใหม่และลบ PR_RENDERING_POSITION คุณสมบัติของไฟล์แนบนั้นและตรวจสอบว่าคุณสมบัตินั้นยังคงถูกลบหลังจากบันทึกและโหลดข้อความใหม่ การบันทึกอาจเพิ่มคุณสมบัติอื่น ๆ ให้ไฟล์แนบ ดังนั้นการตรวจสอบจะดูที่แท็กเองแทนจำนวนคุณสมบัติ
SharedPtr<MapiMessage> mapi = MakeObject<MapiMessage>(u"from@domain.com", u"to@domain.com", u"subject", u"body");
mapi->SetBodyContent(u"<html><body><h1>This is the body content</h1></body></html>", BodyContentType::Html);
mapi->get_Attachments()->Add(u"Attached message.msg", MapiMessage::Load(u"Message.msg"));
SharedPtr<MapiAttachment> attachment = mapi->get_Attachments()->idx_get(mapi->get_Attachments()->get_Count() - 1);
Console::WriteLine(u"Before removal: " + Convert::ToString(attachment->get_Properties()->ContainsKey(MapiPropertyTag::PR_RENDERING_POSITION)));
attachment->RemoveProperty(MapiPropertyTag::PR_RENDERING_POSITION);
Console::WriteLine(u"After removal: " + Convert::ToString(attachment->get_Properties()->ContainsKey(MapiPropertyTag::PR_RENDERING_POSITION)));
mapi->Save(u"PropertyRemoved.msg");
SharedPtr<MapiMessage> reloaded = MapiMessage::Load(u"PropertyRemoved.msg");
SharedPtr<MapiAttachment> reloadedAttachment = reloaded->get_Attachments()->idx_get(reloaded->get_Attachments()->get_Count() - 1);
Console::WriteLine(u"After reloading: " + Convert::ToString(reloadedAttachment->get_Properties()->ContainsKey(MapiPropertyTag::PR_RENDERING_POSITION)));