คุณลักษณะและฟังก์ชันการทำงาน
คุณลักษณะและฟังก์ชันการทำงาน
หน้านี้ครอบคลุมคุณลักษณะหลักทั้งหมดของ Aspose.Email FOSS for .NET พร้อมตัวอย่าง C# ที่ทำงานได้
การอ่านไฟล์ MSG
โหลดไฟล์ MSG จากเส้นทางหรือสตรีมและเข้าถึงคุณสมบัติทั้งหมดของมัน:
using System.IO;
using Aspose.Email.Foss.Msg;
using var stream = File.OpenRead("message.msg");
var message = MapiMessage.FromStream(stream);
Console.WriteLine(message.Subject);
Console.WriteLine(message.Body);
Console.WriteLine(message.HtmlBody);
Console.WriteLine(message.SenderName);
Console.WriteLine(message.SenderEmailAddress);
Console.WriteLine(message.MessageDeliveryTime);
Console.WriteLine(message.InternetMessageId);ใช้ strict: false สำหรับการพาร์สแบบยืดหยุ่นของไฟล์ MSG ที่ไม่เป็นมาตรฐาน:
var message = MapiMessage.FromFile("message.msg", strict: false);ตรวจสอบ ValidationIssues เพื่อทบทวนคำเตือนรูปแบบใด ๆ โดยไม่ทำให้เกิดข้อยกเว้น:
foreach (var issue in message.ValidationIssues)
Console.WriteLine($"Warning: {issue}");การสร้างไฟล์ MSG
สร้างอีเมลที่สมบูรณ์ด้วย MapiMessage.Create() และทำการซีเรียลไลซ์ด้วย Save():
using System.IO;
using Aspose.Email.Foss.Msg;
var message = MapiMessage.Create("Hello", "Body");
message.SenderName = "Alice";
message.SenderEmailAddress = "alice@example.com";
message.HtmlBody = "<p>Body</p>";
message.InternetMessageId = "<hello@example.com>";
message.MessageDeliveryTime = new DateTime(2024, 1, 2, 3, 4, 5, DateTimeKind.Utc);
message.AddRecipient("bob@example.com", "Bob");
using var output = File.Create("hello.msg");
message.Save(output);Save() โดยไม่มีอาร์กิวเมนต์จะคืนค่า byte[]. Save(path) เขียนไปยังเส้นทางไฟล์โดยตรง.
ผู้รับ
เพิ่มผู้รับ To, Cc และ Bcc พร้อมกับ AddRecipient():
message.AddRecipient("alice@example.com", "Alice");
message.AddRecipient("bob@example.com", "Bob", MapiMessage.RecipientTypeCc);
message.AddRecipient("carol@example.com", "Carol", MapiMessage.RecipientTypeBcc);วนซ้ำผู้รับจากข้อความที่โหลดแล้ว:
foreach (var recipient in message.Recipients)
Console.WriteLine($"{recipient.DisplayName} <{recipient.EmailAddress}>");ไฟล์แนบ
เพิ่มไฟล์หรือสตรีมแนบพร้อมเมตาดาต้า:
// From byte array
message.AddAttachment("report.pdf", pdfBytes, "application/pdf");
// From stream
using var attachStream = File.OpenRead("photo.png");
message.AddAttachment("photo.png", attachStream, "image/png");
// Inline image with Content-ID
message.AddAttachment("logo.png", logoBytes, "image/png", contentId: "logo@cid");อ่านไฟล์แนบจากข้อความที่โหลดแล้ว:
foreach (var attachment in message.Attachments)
{
Console.WriteLine($"Filename: {attachment.Filename}");
Console.WriteLine($"MIME type: {attachment.MimeType}");
Console.WriteLine($"Size: {attachment.Data.Length} bytes");
File.WriteAllBytes(attachment.Filename!, attachment.Data);
}ไฟล์แนบข้อความฝัง
แนบ MapiMessage หนึ่งไว้ในอีกอันหนึ่ง — เป็นเรื่องทั่วไปในห่วงโซ่อีเมลที่ส่งต่อ:
using Aspose.Email.Foss.Msg;
var parent = MapiMessage.Create("Outer", "Parent body");
var child = MapiMessage.Create("Inner", "Child body");
child.SenderEmailAddress = "inner@example.com";
parent.AddEmbeddedMessageAttachment(child, "inner.msg");
parent.Save("outer.msg");อ่านไฟล์แนบข้อความที่ฝังอยู่:
var loaded = MapiMessage.FromFile("outer.msg");
foreach (var attachment in loaded.Attachments)
{
if (attachment.IsEmbeddedMessage)
{
Console.WriteLine($"Embedded: {attachment.EmbeddedMessage!.Subject}");
}
}การแปลง EML และ MIME
โหลดไฟล์ RFC 5322 .eml ไปยัง MapiMessage เพื่อการจัดเก็บที่เข้ากันได้กับ Outlook:
using System.IO;
using Aspose.Email.Foss.Msg;
using var input = File.OpenRead("message.eml");
var message = MapiMessage.LoadFromEml(input);
using var msgOutput = File.Create("message.msg");
message.Save(msgOutput);แปลงกลับเป็น EML จากไฟล์ MSG:
var message = MapiMessage.FromFile("message.msg");
using var emlOutput = File.Create("roundtrip.eml");
message.SaveToEml(emlOutput);LoadFromEml() ยังรับเส้นทาง string หรือ byte[]. SaveToEml() โดยไม่มีอาร์กิวเมนต์จะคืนค่า byte[]. หัวเรื่อง, เนื้อหา, เนื้อหา HTML, ผู้ส่ง, ผู้รับ, และไฟล์แนบทั้งหมด (รวมถึงภาพในบรรทัดด้วย Content-ID) จะถูกเก็บรักษาไว้ตลอดการแปลง EML ↔ MSG อย่างเต็มรูปแบบ.
การเข้าถึงคุณสมบัติ MAPI
MapiPropertyCollection ให้การเข้าถึงแบบมีประเภทต่อคุณสมบัติ MAPI ผ่าน SetProperty() และ GetPropertyValue() ใช้ค่า enum ของ CommonMessagePropertyId สำหรับคุณสมบัติมาตรฐาน:
using Aspose.Email.Foss.Msg;
var message = MapiMessage.FromFile("sample.msg");
// Named convenience properties
Console.WriteLine(message.Subject);
Console.WriteLine(message.SenderName);
Console.WriteLine(message.MessageDeliveryTime);
// Iterate all MAPI property keys
foreach (var key in message.IterPropertyKeys())
Console.WriteLine($"0x{(ushort)key.PropertyId:X4} / {key.PropertyType}");การเข้าถึงคอนเทนเนอร์ CFB
ไฟล์ Outlook MSG ถูกสร้างบนรูปแบบ Compound File Binary (CFB) CfbReader เปิดเผยโครงสร้างไดเรกทอรีเต็มสำหรับการตรวจสอบและการสกัดข้อมูล:
using System.Text;
using Aspose.Email.Foss.Cfb;
using var reader = CfbReader.FromFile("message.msg");
// Iterate all top-level children
foreach (var entry in reader.IterChildren(CfbConstants.RootStreamId))
Console.WriteLine($"{(entry.IsStorage() ? "Storage" : "Stream")}: {entry.Name}");
// Navigate to a specific stream by path
var entry2 = reader.ResolvePath(["__substg1.0_0037001E"]);
if (entry2 is not null)
{
var data = reader.GetStreamData(entry2.StreamId);
Console.WriteLine(Encoding.Unicode.GetString(data));
}สร้างและทำการซีเรียลไลซ์เอกสาร CFB ตั้งแต่เริ่มต้นด้วย CfbWriter:
using System.Text;
using Aspose.Email.Foss.Cfb;
var document = new CfbDocument();
document.Root.AddStream(new CfbStream("Notes", Encoding.UTF8.GetBytes("hello")));
var storage = document.Root.AddStorage(new CfbStorage("DataStore"));
storage.AddStream(new CfbStream("Payload", Encoding.UTF8.GetBytes("content")));
byte[] bytes = CfbWriter.ToBytes(document);
File.WriteAllBytes("output.cfb", bytes);เคล็ดลับและแนวทางปฏิบัติที่ดีที่สุด
- ใช้
usingหรือDispose()บนMapiMessageและCfbReaderเพื่อปล่อยตัวจัดการไฟล์ - ตรวจสอบ
message.ValidationIssuesหลังจากโหลดเพื่อค้นพบรูปแบบ MSG ที่ไม่เป็นมาตรฐาน - พารามิเตอร์
strictในFromFile()/FromStream()ควบคุมความทนทานต่อข้อผิดพลาด — ใช้strict: falseสำหรับการพาร์สแบบยืดหยุ่นของไฟล์จากระบบของบุคคลที่สาม MapiMessage.Create()สร้างข้อความแบบ Unicode-string เป็นค่าเริ่มต้น; ตั้งค่าunicodeStrings: falseเพื่อความเข้ากันได้กับ ANSI รุ่นเก่าSaveToEml()โดยไม่มีอาร์กิวเมนต์จะคืนค่าbyte[]— สะดวกสำหรับกระบวนการทำงานในหน่วยความจำ
ปัญหาทั่วไป
| Issue | Cause | Fix |
|---|---|---|
CfbException เมื่อโหลด | ไฟล์ CFB/MSG ไม่ถูกต้อง | ตรวจสอบว่าไฟล์เป็น Outlook MSG |
Body ว่างหลังจากโหลด | เนื้อหาถูกเก็บเป็น HTML เท่านั้น | ตรวจสอบ message.HtmlBody แทน |
| ไม่พบค่าคงที่ประเภทผู้รับ | ไม่ใช่ประเภทที่แยกออกมา | ใช้ MapiMessage.RecipientTypeTo, MapiMessage.RecipientTypeCc หรือ MapiMessage.RecipientTypeBcc |
ไฟล์แนบ 0 รายการหลังจาก LoadFromEml | EML ไม่มีส่วน Content-Disposition: attachment | ส่วน Inline (เฉพาะ Content-ID) ยังปรากฏใน Attachments |
สรุปการอ้างอิง API
| Class | Description |
|---|---|
MapiMessage | การแสดงข้อความ MSG ระดับสูง |
MapiAttachment | ไฟล์แนบหรือข้อความฝังใน MapiMessage |
MapiRecipient | ผู้รับใน MapiMessage |
MapiProperty | รายการคุณสมบัติ MAPI เดียว |
MapiPropertyCollection | ถุงคุณสมบัติ MAPI แบบกำหนดประเภท |
CommonMessagePropertyId | การนับจำนวนของตัวระบุคุณสมบัติ MAPI มาตรฐาน |
MsgReader | ตัวอ่านโครงสร้าง MSG ระดับต่ำ |
MsgWriter | ตัวเขียนโครงสร้าง MSG ระดับต่ำ |
CfbReader | ตัวอ่านคอนเทนเนอร์ไบนารี CFB |
CfbWriter | ตัวเขียนคอนเทนเนอร์ไบนารี CFB |
CfbDocument | เอกสาร CFB ในหน่วยความจำสำหรับการสร้าง |
CfbStorage | โหนดการจัดเก็บในเอกสาร CFB |
CfbStream | โหนดสตรีมในเอกสาร CFB |