Truy cập và Quản lý Các Thuộc tính MAPI của Outlook
MAPI properties là các mục siêu dữ liệu được sử dụng trong tin nhắn Microsoft Outlook, định nghĩa các thuộc tính như người gửi, người nhận, tiêu đề, tệp đính kèm và dữ liệu tùy chỉnh.
Aspose.Email for C++ cho phép các nhà phát triển truy cập, sửa đổi và xóa các thuộc tính này một cách lập trình trong MapiMessage đối tượng, tệp đính kèm và thuộc tính có tên.
Cái MapiProperty lớp đại diện cho một thuộc tính MAPI, bao gồm:
- Name – Định danh chuỗi của thuộc tính.
- Tag – Một định danh số dùng để tham chiếu tới thuộc tính.
- Data – Một mảng byte đại diện cho giá trị của thuộc tính.
Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:
#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;
Đọc Các Thuộc tính MAPI
Mỗi thuộc tính của một tin nhắn được giữ trong tập hợp mà get_Properties, được chỉ mục theo thẻ của nó; phần MapiPropertyTag lớp đặt tên cho các thẻ chuẩn. Tra cứu một thẻ mà tin nhắn không có trả về nullptr.
Nhiều thuộc tính văn bản tồn tại ở hai dạng - một thẻ ANSI như PR_SUBJECT và một phiên bản Unicode như PR_SUBJECT_W - và một tin nhắn mang một trong hai tùy thuộc vào định dạng mà nó được lưu. Mẫu thử cả hai. Một MapiProperty sau đó chuyển đổi dữ liệu thô của nó bằng các phương pháp như GetString hoặc 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()));
}
Đặt Thuộc tính MAPI
Một thuộc tính được thêm hoặc thay thế bằng SetProperty, điều này yêu cầu một MapiProperty được xây dựng từ một thẻ và các byte thô của giá trị. Các chuỗi được lưu dưới dạng UTF-16 cho Unicode (_W) thẻ, và một ngày được lưu dưới dạng FILETIME - số khoảng 100 nan giây kể từ 1 tháng 1 năm 1601 (UTC) - mà DateTime::ToFileTimeUtc sản xuất. Người nhận mang các thuộc tính riêng của chúng, được đặt theo cách tương tự trên một MapiRecipient.
Trong C++, MapiMessage khai báo SetProperty các bản overload của riêng nó, và chúng ẩn bản overload chung của lớp cơ sở MapiPropertyContainer. Do đó mẫu gọi nó thông qua một MapiPropertyContainer con trỏ tới tin nhắn.
Mẫu chuyển người nhận của tin nhắn mới thành địa chỉ fax, đánh dấu tin nhắn là tin chưa gửi từ người dùng hiện tại và đặt thời gian sửa đổi của nó.
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());
Đọc các Thuộc tính MAPI có tên
Các thuộc tính MAPI có tên là các thuộc tính tùy chỉnh do người dùng hoặc ứng dụng thêm vào. Thay vì một thẻ cố định, mỗi thuộc tính được xác định bằng GUID của tập hợp thuộc tính cùng với tên hoặc định danh số, vì vậy các thuộc tính của các ứng dụng khác nhau không bao giờ xung đột. Thẻ mà chúng được lưu trữ dưới đó được gán khi thuộc tính được thêm vào tin nhắn.
Aspose.Email cho phép đọc các thuộc tính này từ tin nhắn và tệp đính kèm.
Đọc các thuộc tính MAPI có tên từ tệp MSG
get_NamedProperties trả về các thuộc tính có tên của một tin nhắn. Mỗi giá trị của tập hợp là một MapiNamedProperty, mà get_NameId cung cấp tên mà thuộc tính được tạo ra với.
Để có gì đó để đọc, mẫu đầu tiên lưu trữ hai thuộc tính có tên. Một thuộc tính có tên được tạo ra trong ba bước: GetNextAvailablePropertyId của tin nhắn NamedPropertyMapping cung cấp một thẻ tự do cho kiểu giá trị, AddNamedPropertyMapping gán thẻ đó với tên và GUID của bộ thuộc tính, và SetProperty lưu giá trị.
// 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());
}
}
Truy cập các thuộc tính MAPI có tên trong tệp đính kèm
Đính kèm cũng có các thuộc tính có tên - một ứng dụng có thể, ví dụ, gắn thẻ mỗi tệp đính kèm bằng định danh riêng của mình. Tên của tất cả các thuộc tính có tên của một tin nhắn, bao gồm cả các đính kèm, được giữ trong ánh xạ của tin nhắn, vì vậy thuộc tính được đăng ký ở đó và sau đó được đặt trên MapiAttachment. Thuộc tính get_NamedProperties bộ sưu tập đính kèm trả về nó.
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());
}
}
Xóa các thuộc tính MAPI
RemoveProperty xóa một thuộc tính theo thẻ của nó - từ một tin nhắn cũng như từ một đính kèm hoặc người nhận. Mẫu đính kèm một tin nhắn Outlook vào một tin nhắn mới, xoá PR_RENDERING_POSITION thuộc tính của tập tin đính kèm đó và kiểm tra rằng thuộc tính vẫn bị xóa sau khi tin nhắn được lưu và tải lại. Việc lưu có thể thêm các thuộc tính khác vào đính kèm, vì vậy kiểm tra tìm thẻ chứ không phải số lượng thuộc tính.
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)));