วิธีเริ่มต้นใช้งาน Aspose.Email FOSS สำหรับ .NET
คู่มือนี้จะพาคุณผ่านขั้นตอนการติดตั้งไลบรารี, การอ่านไฟล์ MSG แรกของคุณ, การสร้างข้อความตั้งแต่ต้น, และการแปลงระหว่างรูปแบบ EML และ MSG.
ขั้นตอนที่ 1 — ติดตั้งแพคเกจ
dotnet add package Aspose.Email.Fossไม่จำเป็นต้องกำหนดค่าเพิ่มเติม แพคเกจไม่มีการพึ่งพาแบบเนทีฟ.
ขั้นตอนที่ 2 — อ่านไฟล์ MSG
สร้างแอปคอนโซลและเพิ่มโค้ดต่อไปนี้:
using System.IO;
using Aspose.Email.Foss.Msg;
using var stream = File.OpenRead("sample.msg");
var message = MapiMessage.FromStream(stream);
Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine($"From: {message.SenderEmailAddress}");
Console.WriteLine($"Body: {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})");MapiMessage.FromStream() แยกวิเคราะห์คอนเทนเนอร์ CFB ของไฟล์ MSG และเปิดเผยคุณสมบัติ MAPI
ทั้งหมดผ่านคุณสมบัติ C# ที่กำหนดประเภทอย่างเข้มงวด ไม่จำเป็นต้องใช้ Microsoft Outlook.
ขั้นตอนที่ 3 — สร้างไฟล์ MSG ใหม่
using System.IO;
using Aspose.Email.Foss.Msg;
var message = MapiMessage.Create("Meeting Notes", "Please find the notes attached.");
message.SenderName = "Alice";
message.SenderEmailAddress = "alice@example.com";
message.AddRecipient("bob@example.com", "Bob");
// Add a file attachment
message.AddAttachment("notes.txt", System.Text.Encoding.UTF8.GetBytes("Meeting notes here"), "text/plain");
// Save to file
message.Save("meeting_notes.msg");
Console.WriteLine("Created meeting_notes.msg");MapiMessage.Create() สร้างข้อความในหน่วยความจำ Save() ทำการแปลงเป็นรูปแบบ MSG — คุณสามารถส่งเส้นทางไฟล์, Stream, หรือเรียก Save() โดยไม่มีอาร์กิวเมนต์เพื่อรับ byte[].
ขั้นตอนที่ 4 — แปลง EML เป็น MSG
using System.IO;
using Aspose.Email.Foss.Msg;
// Load from EML
using var input = File.OpenRead("message.eml");
var message = MapiMessage.LoadFromEml(input);
Console.WriteLine($"Subject: {message.Subject}");
// Save as MSG
message.Save("converted.msg");
Console.WriteLine("Saved converted.msg");
// Or save back to EML (round-trip)
message.SaveToEml("roundtrip.eml");ตัวแยกวิเคราะห์ MIME ในตัวคงรักษาหัวเรื่อง, เนื้อหา, เนื้อหา HTML, ผู้ส่ง, ผู้รับ, และไฟล์แนบทั้งหมดผ่านการทำรอบเต็มระหว่าง EML ↔ MSG
ขั้นตอนต่อไป
- คุณลักษณะและฟังก์ชันการทำงาน — อ้างอิงคุณลักษณะทั้งหมดพร้อมตัวอย่าง C#
- FAQ — คำตอบสำหรับคำถามทั่วไป
- Installation Guide — การตั้งค่า NuGet และข้อกำหนดเวอร์ชัน .NET