Đọc tệp MSG
Đọc các tệp MSG
Aspose.Email FOSS for .NET cung cấp ba cấp độ truy cập để đọc các tệp Outlook .msg: API cấp cao MapiMessage, API trung cấp MsgDocument/MsgReader, và cấp thấp CfbReader để kiểm tra nhị phân thô.
Cấp cao: MapiMessage
MapiMessage là lớp chính để đọc các tệp MSG. Nó cung cấp các thuộc tính kiểu mạnh
cho tất cả các trường email tiêu chuẩn.
Tải từ Tệp hoặc Luồng
using Aspose.Email.Foss.Msg;
// From file path
using var message = MapiMessage.FromFile("sample.msg");
// From stream
using var stream = File.OpenRead("sample.msg");
using var message2 = MapiMessage.FromStream(stream);Tham số strict tùy chọn kiểm soát hành vi xác thực:
// Strict mode — populates ValidationIssues but does not throw
using var message = MapiMessage.FromFile("sample.msg", strict: true);
foreach (var issue in message.ValidationIssues)
Console.WriteLine($"Warning: {issue}");Truy cập các trường tin nhắn
Console.WriteLine($"Subject: {message.Subject}");
Console.WriteLine($"Body: {message.Body}");
Console.WriteLine($"HTML Body: {message.HtmlBody}");
Console.WriteLine($"From: {message.SenderName} <{message.SenderEmailAddress}>");
Console.WriteLine($"Message-ID: {message.InternetMessageId}");
Console.WriteLine($"Date: {message.MessageDeliveryTime}");
Console.WriteLine($"Message Class: {message.MessageClass}");Đọc người nhận
foreach (var recipient in message.Recipients)
{
var type = recipient.RecipientType switch
{
MapiMessage.RecipientTypeTo => "To",
MapiMessage.RecipientTypeCc => "Cc",
MapiMessage.RecipientTypeBcc => "Bcc",
_ => "Unknown"
};
Console.WriteLine($" {type}: {recipient.DisplayName} <{recipient.EmailAddress}>");
}Đọc Tệp Đính Kèm
foreach (var attachment in message.Attachments)
{
Console.WriteLine($" {attachment.Filename} ({attachment.MimeType})");
if (attachment.IsEmbeddedMessage)
{
Console.WriteLine($" Embedded subject: {attachment.EmbeddedMessage!.Subject}");
}
else
{
File.WriteAllBytes(attachment.Filename!, attachment.Data);
}
}Cấp trung: MsgDocument và MsgReader
MsgDocument cung cấp quyền truy cập vào cây lưu trữ MSG — các kho lưu trữ, luồng, người nhận và tệp đính kèm như các phần tử cấu trúc.
using Aspose.Email.Foss.Msg;
var document = MsgDocument.FromFile("sample.msg");
// Navigate storages by role
foreach (var storage in document.Root.Storages)
{
Console.WriteLine($"{storage.Name} — role: {storage.Role}");
if (storage.Role == MsgStorageRole.Recipient)
Console.WriteLine(" (recipient storage)");
else if (storage.Role == MsgStorageRole.Attachment)
Console.WriteLine(" (attachment storage)");
}MsgReader cung cấp quyền truy cập chỉ đọc kèm xác thực:
using var reader = MsgReader.FromFile("sample.msg", strict: true);
Console.WriteLine($"Validation issues: {reader.ValidationIssues.Count}");Cấp thấp: CfbReader
Để kiểm tra nhị phân thô, sử dụng CfbReader để duyệt trực tiếp container CFB:
using Aspose.Email.Foss.Cfb;
using var reader = CfbReader.FromFile("sample.msg");
// Traverse the full directory tree
foreach (var (depth, entry) in reader.IterTree())
{
var indent = new string(' ', depth * 2);
Console.WriteLine($"{indent}{entry.Name} [type={entry.ObjectType}, size={entry.StreamSize}]");
}
// Read raw stream data
var rootChildren = reader.IterChildren(CfbConstants.RootStreamId);
foreach (var entry in rootChildren)
{
if (entry.IsStream())
{
var data = reader.GetStreamData(entry.StreamId);
Console.WriteLine($"{entry.Name}: {data.Length} bytes");
}
}Xem thêm
- Features — Tham chiếu đầy đủ các tính năng
- EML Conversion — Chuyển đổi giữa EML và MSG
- CFB Containers — Truy cập nhị phân CFB cấp thấp