คุณสมบัติ MAPI
คุณสมบัติ MAPI
ทุกไฟล์ MSG จะเก็บข้อมูลเป็นคุณสมบัติ MAPI — คู่คีย์‑ค่าแบบแท็กที่ระบุด้วยรหัสคุณสมบัติและรหัสประเภท. Aspose.Email FOSS for .NET ให้การเข้าถึงโดยตรงไปยังคุณสมบัติเหล่านี้ผ่าน MapiMessage, MapiPropertyCollection, และ enum CommonMessagePropertyId.
การอ่านคุณสมบัติ
คุณสมบัติที่สะดวก
MapiMessage เปิดเผยคุณสมบัติที่พบบ่อยที่สุดในรูปแบบคุณสมบัติของ C# ที่กำหนดประเภท:
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?การเข้าถึงคุณสมบัติแบบดิบ
สำหรับคุณสมบัติที่ไม่ได้เปิดเผยเป็นฟิลด์สะดวก ให้ใช้ 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}");วนซ้ำคุณสมบัติทั้งหมด
// 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}");
}คุณสมบัติการเขียน
ตั้งค่าคุณสมบัติบนข้อความใหม่
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));การเข้าถึงคอลเลกชันโดยตรง
MapiPropertyCollection ให้ Add, Set, Get, และ 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);รหัสคุณสมบัติทั่วไป
enum CommonMessagePropertyId ให้ค่าคงที่ที่มีชื่อสำหรับ ID ของคุณสมบัติ MAPI ที่ใช้บ่อย:
| Property | Description |
|---|---|
Subject | หัวข้อข้อความ |
Body | เนื้อความแบบข้อความธรรมดา |
BodyHtml | เนื้อความ HTML |
SenderName | ชื่อที่แสดงของผู้ส่ง |
SenderEmailAddress | ที่อยู่อีเมลของผู้ส่ง |
DisplayTo | สตริงการแสดงผู้รับ (To) |
DisplayCc | สตริงการแสดงผู้รับสำเนา (Cc) |
DisplayBcc | สตริงการแสดงผู้รับสำเนาลับ (Bcc) |
InternetMessageId | Message-ID ของ RFC 5322 |
MessageDeliveryTime | เวลาประทับการจัดส่ง |
TransportMessageHeaders | ส่วนหัว RFC 5322 ดิบ |
AttachFilename | ชื่อไฟล์แนบสั้น |
AttachLongFilename | ชื่อไฟล์แนบยาว |
AttachMimeTag | ประเภท MIME ของไฟล์แนบ |
AttachContentId | Content-ID ของไฟล์แนบ |
รหัสประเภทอสังหาริมทรัพย์
enum PropertyTypeCode ทำการแมปรหัสประเภท MAPI:
| ประเภท | คำอธิบาย |
|---|---|
PtypString | สตริง Unicode |
PtypString8 | สตริง ANSI |
PtypInteger32 | จำนวนเต็ม 32‑บิต |
PtypInteger64 | จำนวนเต็ม 64‑บิต |
PtypBoolean | บูลีน |
PtypTime | DateTime (FILETIME) |
PtypBinary | อาร์เรย์ไบต์ |
PtypGuid | GUID |
คุณสมบัติของไฟล์แนบ
แต่ละ MapiAttachment ยังมีคอลเลกชัน Properties ของตนเอง:
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}");
}ดูเพิ่มเติม
- อ่านไฟล์ MSG — โหลดและตรวจสอบไฟล์ MSG
- การแปลง EML — แปลงระหว่างรูปแบบต่างๆ
- คอนเทนเนอร์ CFB — การเข้าถึงคอนเทนเนอร์ไบนารีระดับต่ำ