Introduction

การพารัสข้อความอีเมลโดยโปรแกรมเป็นสิ่งจําเป็นที่ทั่วไปสําหรับเครื่องมือการเก็บรักษาข้อมูล, pipelines ความยอมรับและบริการโภคเมือง. ไฟล์ Outlook MSG ใช้รูปแบบขวด Compound File Binary (CFB) กับ MAPI property streams ส่วนไฟล์ EML ตามมาตรฐาน MIME ทั้งสองรูป แบบนี้ต้องการการจัดการอย่างระมัดระวังกับการโคด, หัวหน้าผูก และแนบเนสต์.

Aspose.Email FOSS for .NET is a free, MIT-licensed C# library that provides a complete read-write API for MSG and EML messages. It targets .NET 8.0+ and has no dependency on Microsoft Outlook or any native COM interop layer. The library is available as the aspose.email.foss พัสดุ NuGet.

บทความนี้จะนําไปใช้การพารัสข้อความที่อยู่ในห้องสมุด ตั้งแต่การอัดไฟล์จากดิสก์ จนถึงการสกัดคุณลักษณะ MAPI แต่ละตัว และแปลงระหว่างรูปแบบต่างๆ.


สิ่งที่รวมอยู่ใน

การอัดไฟล์ MSG

การ MapiMessage class เป็นจุดเข้าหลักในการทํางานกับไฟล์ Outlook MSG คุณสามารถอัตราการโหลดข้อความจากเส้นทางไฟ่หรือสตรีมได้ โดยมีป้ายรับรองอย่างเคร่งขันที่เปิดเผยปัญหาด้านโครงสร้างใด ๆ ที่พบในไฟ้ล.

// Load an MSG file from disk
var message = MapiMessage.FromFile("meeting-invite.msg");

Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine($"From: {message.SenderName} <{message.SenderEmailAddress}>");
Console.WriteLine($"Date: {message.MessageDeliveryTime}");
Console.WriteLine($"Recipients: {message.Recipients.Count}");
Console.WriteLine($"Attachments: {message.Attachments.Count}");

ผู้กลับคืนมา MapiMessage เปิดตัวคุณสมบัติที่ถูกชนกสําหรับสนามทั่วไปรวมถึง Subject, Body, HtmlBody, SenderName, SenderEmailAddress, InternetMessageId,และ MessageDeliveryTime.

การอัตราบรรจุ EML ข้อความ

ภาพ EML ใช้รูปแบบ MIME. MapiMessage.LoadFromEml รับการเดินทางไฟล์, a Stream,หรือ a byte[] และคืนเดิม MapiMessage object, ดังนั้นคุณจึงใช้ API ที่รวมกันอย่างเดียว ไม่ว่ารูปแบบแหล่งจะอย่างไร.

// Load from a file path
var fromFile = MapiMessage.LoadFromEml("newsletter.eml");

// Load from a stream
using var stream = File.OpenRead("newsletter.eml");
var fromStream = MapiMessage.LoadFromEml(stream);

Console.WriteLine($"Subject: {fromStream.Subject}");
Console.WriteLine($"Body: {fromStream.Body?.Trim()}");

เครื่องพาร์เซอร์จัดการหัวหน้าที่ผูก, Base64 และการโค้ดโอนแบบสามารถพิมพ์ได้และสนาม To / Cc หลายตัวรับ.

Aspose.3D FOSS for Python 26.1.0 ครอบคลุมรูปแบบไฟล์ 3D ที่ใช้บ่อยที่สุดในเครื่องมือ, กระบวนการทำงาน, และแอปพลิเคชันเว็บ (OBJ, STL, glTF, COLLADA, และ 3MF) ด้วย API Python ที่สอดคล้องและไม่มีอุปสรรคในการติดตั้ง หากคุณพบบั๊ก, กรณีขอบที่ไม่รองรับ, หรือรูปแบบที่ต้องการเพิ่ม, โปรดเปิด issue บน GitHub เราให้การดูแลไลบรารีอย่างต่อเนื่องและยินดีรับการมีส่วนร่วม

เมื่อข้อความถูกอัตราบรรจุแล้ว ผู้รับและเอกสารแนบจะสามารถใช้ได้ในรูปแบบการรวบรวมที่เขียนด้วยเครื่องหมายแรง MapiRecipient ขนส่ง EmailAddress, DisplayName,และ RecipientType ทุกๆ สายงาน MapiAttachment เสนอให้ Filename, Data, MimeType,และ ContentId.

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

foreach (var recipient in message.Recipients)
{
    Console.WriteLine($"  {recipient.DisplayName} <{recipient.EmailAddress}> (type={recipient.RecipientType})");
}

foreach (var attachment in message.Attachments)
{
    Console.WriteLine($"  {attachment.Filename} ({attachment.MimeType}, {attachment.Data.Length} bytes)");
    File.WriteAllBytes(attachment.Filename!, attachment.Data);
}

การติดต่อข้อความที่ถูกนํามาใช้ด้วยยังได้รับการสนับสนุนเช่นกัน เมื่อ MapiAttachment.IsEmbeddedMessage คือ true, การ EmbeddedMessage property กลับ nested MapiMessage ที่คุณสามารถตรวจสอบได้แบบถัดไป.

การเข้าถึงคุณสมบัติ MAPI

สําหรับฉบับที่พัฒนาขึ้น ทุกข้อความ ผู้รับ และเอกสารแนบ จะเปิดเผยข้อมูลของ Properties การเก็บของชนิด MapiPropertyCollection.คุณสามารถอ่าน, เพิ่มและลบคุณสมบัติ MAPI แต่ละตัวโดย ID ของคุณสิทธิ์ และรหัสชนิดของพวกเขา.

var message = MapiMessage.FromFile("custom-props.msg");

// Iterate all properties on the message
foreach (var prop in message.Properties.IterProperties())
{
    Console.WriteLine($"Tag: 0x{prop.PropertyTag:X8}, Value: {prop.Value}");
}

// Read a specific property
var subject = message.Properties.Get(
    (ushort)CommonMessagePropertyId.Subject,
    (ushort)PropertyTypeCode.PtypString);

การ CommonMessagePropertyId enum กําหนดตัวจําแนกของทรัพย์สิน MAPI ที่เป็นที่รู้จักดีสําหรับการแสดงความหมายหลักของการส่งข้อความ, หมากส่วนร่างกาย, หัวขนส่ง และเมตาข้อมูลที่ติดต่อ.

การแปลงระหว่าง MSG และ EML

ข้อความที่พาราส์สามารถบันทึกได้ในรูปแบบใดก็ได้. Save() เขียน MSG ไบท์, ขณะที่ SaveToEml() ผลิตผลการออก MIME. ทําให้การแปลงรูปแบบง่ายๆ.

// Load an EML and save as MSG
var message = MapiMessage.LoadFromEml("incoming.eml");
message.Save("converted.msg");

// Load an MSG and save as EML
var msg = MapiMessage.FromFile("outgoing.msg");
msg.SaveToEml("exported.eml");

ทั้งสองวิธียังยอมรับการใช้งานของ Stream ปารามีเตอร์สําหรับการประมวลผลในความจําโดยไม่สัมผัสระบบไฟล์.


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

ติดตั้งแพคเกจจาก NuGet:

dotnet add package Aspose.Email.FOSS

แล้วพิจารณาข้อความแรกของคุณ:

using Aspose.Email.Foss.Msg;

// Load an MSG file
var message = MapiMessage.FromFile("sample.msg");

Console.WriteLine($"Subject:     {message.Subject}");
Console.WriteLine($"From:        {message.SenderName} <{message.SenderEmailAddress}>");
Console.WriteLine($"Date:        {message.MessageDeliveryTime}");
Console.WriteLine($"Body length: {message.Body?.Length ?? 0} chars");

// List attachments
foreach (var att in message.Attachments)
    Console.WriteLine($"  Attachment: {att.Filename} ({att.Data.Length} bytes)");

// Convert to EML
message.SaveToEml("sample.eml");
Console.WriteLine("Saved as EML.");

รูปแบบที่สนับสนุน

FormatExtensionReadWrite
Outlook MSG.msgใช่แล้วใช่แล้ว
MIME (EML).emlใช่แล้วใช่แล้ว
ไบนารีไฟล์ผสม.cfbใช่แล้วใช่แล้ว

หลักสูตรและใบอนุญาต

Aspose.Email FOSS for .NET is released under the MIT license. The full source code is available on GitHub at aspose-email-foss/Aspose.Email-FOSS-for-.Net การจัดหาข้อมูลการใช้งานที่ผ่านระบบทางอินเทอร์เน็ต.การบริจาค, เรื่องและการขอดึงความสนใจเป็นของต้อนรับ.


เริ่มต้น

แหล่งข้อมูลที่เกี่ยวข้อง