الوصول إلى خصائص 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 ، مفهرسة حسب علامتها؛ ال MapiPropertyTag تُسمي الفئة العلامات القياسية. البحث عن علامة لا تملكها الرسالة يعيد 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 نانوثانية منذ 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 المسماة
خصائص MAPI المسماة هي خصائص مخصصة يضيفها المستخدمون أو التطبيقات. بدلاً من علامة ثابتة، يتم تعريف كل منها بواسطة GUID لمجموعة الخصائص مع اسم أو معرف رقمي، بحيث لا تتصادم خصائص التطبيقات المختلفة. يتم تعيين العلامة التي تُخزن تحتها عند إضافة الخاصية إلى الرسالة.
Aspose.Email يتيح قراءة هذه الخصائص من الرسائل والمرفقات.
قراءة خصائص MAPI المُسماة من ملفات MSG
get_NamedProperties يعيد الخصائص المسماة لرسالة. كل قيمة في المجموعة هي MapiNamedProperty، الذي get_NameId يعطي الاسم الذي تم إنشاء الخاصية به.
لإتاحة شيء للقراءة، يخزن العينة أولاً خاصيتين مسمات. يتم إنشاء خاصية مسماة في ثلاث خطوات: GetNextAvailablePropertyId من رسالة تخطيط الخاصية المسماة يوفر وسمًا مجانيًا لنوع القيمة, AddNamedPropertyMapping يربط هذا الوسم بالاسم ومجموعة معرف الخصائص، و 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)));