บทนำ

Aspose.Email FOSS for .NET เป็นไลบรารี C# ที่ไม่มีการพึ่งพา ใบอนุญาต MIT สำหรับการทำงานกับ
ไฟล์ Outlook MSG, คอนเทนเนอร์ Compound File Binary (CFB) และข้อความ EML. โพสต์นี้พา
ผ่านแต่ละพื้นที่ฟีเจอร์หลักพร้อมตัวอย่างโค้ดที่ทำงานได้ซึ่งดึงมาจากชุดทดสอบของไลบรารี
— ทุกชื่อคลาสและลายเซ็นเมธอดที่แสดงที่นี่ได้รับการตรวจสอบกับ
source repository.

ติดตั้งด้วยคำสั่งเดียว — ไม่ต้องใช้ Microsoft Outlook, ไม่ต้องใช้ COM interop, ไม่ต้องใช้ไลบรารีเนทีฟ:

dotnet add package Aspose.Email.Foss

คุณลักษณะสำคัญ

อ่านและตรวจสอบไฟล์ MSG

MapiMessage.FromStream() และ MapiMessage.FromFile() เปิดไฟล์ Outlook .msg ใดก็ได้และเปิดเผยเนื้อหาทั้งหมดของไฟล์: หัวเรื่อง, เนื้อหาข้อความธรรมดา, เนื้อหา HTML, ชื่อผู้ส่ง, ที่อยู่อีเมลผู้ส่ง, เวลาในการจัดส่ง, รหัสข้อความอินเทอร์เน็ต, ผู้รับ, และไฟล์แนบ. ไม่จำเป็นต้องติดตั้ง Outlook — ชั้น MSG และ CFB ทั้งหมดถูกดำเนินการใน managed C#.

using System.IO;
using Aspose.Email.Foss.Msg;

using var stream = File.OpenRead("sample.msg");
var message = MapiMessage.FromStream(stream);

Console.WriteLine(message.Subject);
Console.WriteLine(message.SenderEmailAddress);
Console.WriteLine(message.Body);

foreach (var recipient in message.Recipients)
    Console.WriteLine($"To: {recipient.EmailAddress}");

foreach (var attachment in message.Attachments)
    Console.WriteLine($"Attachment: {attachment.Filename} ({attachment.MimeType})");

สร้างไฟล์ MSG ตั้งแต่เริ่มต้น

MapiMessage.Create() สร้างอีเมลที่สมบูรณ์ในหน่วยความจำ.
ตั้งค่า SenderName,
SenderEmailAddress, HtmlBody, InternetMessageId, และ MessageDeliveryTime เป็น
คุณสมบัติหลังจากสร้าง, จากนั้นเพิ่มผู้รับด้วย AddRecipient() และไฟล์หรือสตรีม
แนบด้วย AddAttachment(). ทำการซีเรียลไลซ์ด้วย message.Save() — คืนค่า byte[]
หรือเขียนโดยตรงไปยังเส้นทางหรือ Stream.

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");
message.AddAttachment("note.txt", "abc"u8.ToArray(), "text/plain");

using var output = File.Create("hello.msg");
message.Save(output);

แปลง EML เป็น MSG (และกลับกัน)

MapiMessage.LoadFromEml() ยอมรับเส้นทางไฟล์, Stream, หรือ byte[] ที่มีข้อความ RFC 5322 / MIME มาตรฐาน. ตัวแยกวิเคราะห์ MIME ในตัวจะเก็บรักษาหัวเรื่อง, เนื้อหาข้อความธรรมดา, เนื้อหา HTML, ผู้ส่ง, ผู้รับ, และไฟล์แนบทั้งหมด — รวมถึงภาพอินไลน์ที่มีหัวข้อ Content-ID. SaveToEml() ทำการแปลงเป็น MIME กลับเพื่อการเดินทางรอบ EML ↔ MSG อย่างเต็มรูปแบบโดยไม่ต้องใช้ไลบรารี MIME ภายนอก.

using System.IO;
using Aspose.Email.Foss.Msg;

using var input = File.OpenRead("message.eml");
var message = MapiMessage.LoadFromEml(input);

// Inspect fields parsed from MIME headers
Console.WriteLine(message.Subject);
Console.WriteLine(message.SenderEmailAddress);
Console.WriteLine(message.HtmlBody);

// Save as MSG for Outlook-compatible storage
using var msgOutput = File.Create("message.msg");
message.Save(msgOutput);

// Round-trip back to EML
using var emlOutput = File.Create("roundtrip.eml");
message.SaveToEml(emlOutput);

ไฟล์แนบข้อความฝัง

ไฟล์ MSG สามารถบรรจุไฟล์ MSG อีกไฟล์หนึ่งเป็นไฟล์แนบ — รูปแบบที่พบบ่อยใน
ห่วงโซ่อีเมลที่ส่งต่อ. AddEmbeddedMessageAttachment() แนบวัตถุ MapiMessage เป็นไฟล์แนบแบบซ้อน
.msg ไฟล์แนบ. เมื่ออ่าน, MapiAttachment.IsEmbeddedMessage ระบุประเภทที่ฝังอยู่
และ EmbeddedMessage เปิดเผย MapiMessage ที่ซ้อนอยู่โดยตรง.

using System.IO;
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");
var attachment = loaded.Attachments[0];

if (attachment.IsEmbeddedMessage)
    Console.WriteLine($"Embedded subject: {attachment.EmbeddedMessage!.Subject}");

การอ่านและเขียนคอนเทนเนอร์ CFB

ไฟล์ Outlook MSG ถูกสร้างบนรูปแบบ Compound File Binary (CFB) — ตัวไบนารีแบบลำดับชั้น
คอนเทนเนอร์ที่คล้ายกับระบบไฟล์ FAT. CfbReader เปิดเผยโครงสร้างไดเรกทอรีทั้งหมด: ทำการวนซ้ำ
storages และ streams ด้วย IterChildren(), นำทางไปยังรายการเฉพาะโดยใช้ path chain ด้วย
ResolvePath(), และอ่านไบต์ของ stream ดิบด้วย GetStreamData(). CfbWriter.ToBytes()
ทำการ serialize CfbDocument เป็นไบต์ — มีประโยชน์สำหรับการสร้างเอกสาร CFB แบบกำหนดเองหรือ
การตรวจสอบการทำ round‑trip แบบไม่มีการสูญเสีย.

using System.Text;
using Aspose.Email.Foss.Cfb;

// Build a CFB document from scratch
var document = new CfbDocument();
document.Root.AddStream(new CfbStream("ReadMe", 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);

// Read it back
using var reader = new CfbReader(bytes);
var entry = reader.ResolvePath(["DataStore", "Payload"]);
if (entry is not null)
{
    var data = reader.GetStreamData(entry.StreamId);
    Console.WriteLine(Encoding.UTF8.GetString(data)); // "content"
}

การเข้าถึงคุณสมบัติ MAPI แบบกำหนดประเภท

MapiPropertyCollection ให้การเข้าถึงคุณสมบัติ MAPI ที่กำหนดประเภทสำหรับอ็อบเจ็กต์ MSG หรือ storage ใด ๆ.
enum CommonMessagePropertyId ครอบคลุมตัวระบุคุณสมบัติ MAPI มาตรฐานทั้งหมด — Subject,
Body, BodyHtml, SenderName, SenderEmailAddress, MessageDeliveryTime,
InternetMessageId, และคุณสมบัติของไฟล์แนบรวมถึง AttachFilename, AttachMimeTag,
และ AttachContentId. ใช้ SetProperty() และ GetPropertyValue() เพื่ออ่านและเขียนคุณสมบัติใด ๆ ที่อยู่นอกเหนือจากฟิลด์ความสะดวกที่ตั้งชื่อไว้บน MapiMessage.

using Aspose.Email.Foss.Msg;

var message = MapiMessage.FromFile("sample.msg");

// Typed 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}");

เริ่มต้นอย่างรวดเร็ว

dotnet add package Aspose.Email.Foss
using System.IO;
using Aspose.Email.Foss.Msg;

// Read an MSG file
using var stream = File.OpenRead("sample.msg");
var message = MapiMessage.FromStream(stream);
Console.WriteLine(message.Subject);
Console.WriteLine(message.SenderEmailAddress);
foreach (var recipient in message.Recipients)
    Console.WriteLine($"To: {recipient.EmailAddress}");
foreach (var attachment in message.Attachments)
    Console.WriteLine($"Attachment: {attachment.Filename} ({attachment.MimeType})");

รูปแบบที่รองรับ

รูปแบบส่วนขยายอ่านเขียน
Compound File Binary.cfb
Outlook Message.msg
MIME / EML.eml

โอเพนซอร์สและการให้สิทธิ์

Aspose.Email FOSS for .NET ถูกปล่อยภายใต้ MIT license. ใช้งานได้อย่างอิสระในโครงการส่วนบุคคล, เชิงพาณิชย์ และโอเพนซอร์สโดยไม่มีข้อจำกัดการใช้งาน. โค้ดต้นฉบับสามารถเข้าถึงได้ที่ github.com/aspose-email-foss/Aspose.Email-FOSS-for-.Net.


เริ่มต้นใช้งาน