Thuộc tính MAPI
Thuộc tính MAPI
Mỗi tệp MSG lưu trữ dữ liệu dưới dạng các thuộc tính MAPI — các cặp khóa‑giá trị có thẻ được xác định bằng ID thuộc tính và mã loại. Aspose.Email FOSS for .NET cung cấp quyền truy cập trực tiếp vào các thuộc tính này thông qua MapiMessage, MapiPropertyCollection và enum CommonMessagePropertyId.
Đọc Thuộc tính
Thuộc tính tiện lợi
MapiMessage cung cấp các thuộc tính phổ biến nhất dưới dạng các thuộc tính C# đã được gõ kiểu:
using Aspose.Email.Foss.Msg;
using var message = MapiMessage.FromFile("sample.msg");
Console.WriteLine(message.Subject); // string?
Console.WriteLine(message.Body); // string?
Console.WriteLine(message.HtmlBody); // string?
Console.WriteLine(message.SenderName); // string?
Console.WriteLine(message.SenderEmailAddress); // string?
Console.WriteLine(message.InternetMessageId); // string?
Console.WriteLine(message.MessageDeliveryTime); // DateTime?
Console.WriteLine(message.MessageClass); // string?Truy cập thuộc tính thô
Đối với các thuộc tính không được hiển thị dưới dạng trường tiện lợi, hãy sử dụng GetPropertyValue:
// Read a string property by ID and type
var displayTo = message.GetPropertyValue(
(ushort)CommonMessagePropertyId.DisplayTo,
(ushort)PropertyTypeCode.PtypString,
decode: true);
Console.WriteLine($"Display To: {displayTo}");Duyệt tất cả các thuộc tính
// List all property keys (ID + type pairs)
foreach (var key in message.IterPropertyKeys())
Console.WriteLine($"Property: 0x{key:X8}");
// Iterate full property objects
foreach (var prop in message.IterProperties())
{
Console.WriteLine($"ID=0x{prop.PropertyId:X4} " +
$"Type=0x{prop.PropertyType:X4} " +
$"Tag=0x{prop.PropertyTag:X8} " +
$"Value={prop.Value}");
}Thuộc tính ghi
Đặt Thuộc tính cho Tin nhắn Mới
var message = MapiMessage.Create("Test", "Body");
message.SetProperty(
(ushort)CommonMessagePropertyId.SenderName,
(ushort)PropertyTypeCode.PtypString,
"Alice");
message.SetProperty(
(ushort)CommonMessagePropertyId.SenderEmailAddress,
(ushort)PropertyTypeCode.PtypString,
"alice@example.com");
message.SetProperty(
(ushort)CommonMessagePropertyId.MessageDeliveryTime,
(ushort)PropertyTypeCode.PtypTime,
new DateTime(2026, 3, 15, 10, 30, 0, DateTimeKind.Utc));Truy cập Bộ sưu tập Trực tiếp
MapiPropertyCollection cung cấp Add, Set, Get và Remove:
// Access the property collection
var props = message.Properties;
// Add a property
props.Add(
(ushort)CommonMessagePropertyId.InternetCodepage,
(ushort)PropertyTypeCode.PtypInteger32,
65001, // UTF-8
flags: 0);
// Read it back
var codepage = props.Get(
(ushort)CommonMessagePropertyId.InternetCodepage,
(ushort)PropertyTypeCode.PtypInteger32);
Console.WriteLine($"Codepage: {codepage?.Value}");
// Remove a property
props.Remove(
(ushort)CommonMessagePropertyId.InternetCodepage,
(ushort)PropertyTypeCode.PtypInteger32);Các ID Thuộc tính Chung
Enum CommonMessagePropertyId cung cấp các hằng số có tên cho các ID thuộc tính MAPI
thường được sử dụng:
| Property | Description |
|---|---|
Subject | Chủ đề tin nhắn |
Body | Nội dung văn bản thuần |
BodyHtml | Nội dung HTML |
SenderName | Tên hiển thị người gửi |
SenderEmailAddress | Địa chỉ email người gửi |
DisplayTo | Chuỗi hiển thị người nhận (To) |
DisplayCc | Chuỗi hiển thị người nhận CC |
DisplayBcc | Chuỗi hiển thị người nhận BCC |
InternetMessageId | RFC 5322 Message-ID |
MessageDeliveryTime | Thời gian giao nhận |
TransportMessageHeaders | Tiêu đề RFC 5322 thô |
AttachFilename | Tên tệp đính kèm ngắn |
AttachLongFilename | Tên tệp đính kèm dài |
AttachMimeTag | Kiểu MIME của tệp đính kèm |
AttachContentId | Content-ID của tệp đính kèm |
Mã Loại Tài Sản
Enum PropertyTypeCode ánh xạ các mã loại MAPI:
| Kiểu | Mô tả |
|---|---|
PtypString | Chuỗi Unicode |
PtypString8 | Chuỗi ANSI |
PtypInteger32 | Số nguyên 32-bit |
PtypInteger64 | Số nguyên 64-bit |
PtypBoolean | Boolean |
PtypTime | DateTime (FILETIME) |
PtypBinary | Mảng byte |
PtypGuid | GUID |
Thuộc tính Tệp đính kèm
Mỗi MapiAttachment cũng có bộ sưu tập Properties riêng của nó:
foreach (var attachment in message.Attachments)
{
Console.WriteLine($"Attachment: {attachment.Filename}");
foreach (var prop in attachment.Properties.IterProperties())
Console.WriteLine($" 0x{prop.PropertyTag:X8} = {prop.Value}");
}Xem thêm
- Đọc tệp MSG — Tải và kiểm tra các tệp MSG
- Chuyển đổi EML — Chuyển đổi giữa các định dạng
- Container CFB — Truy cập container nhị phân cấp thấp