加载和解析 MSG 文件

使用 Aspose.Email for .NET,开发人员可以加载并解析 Outlook 消息文件的内容。

  • 要从磁盘加载 MSG 文件,请使用静态方法 MapiMessage.Load 方法的 MapiMessage 类。该类为不同场景提供了多个静态加载函数。
  • 要解析 MSG 文件内容, MapiMessage 公开了许多方法和属性。

在本文中,您将学习如何加载和解析 MSG 文件以显示其内容。带有步骤的代码示例将帮助您清晰了解如何在项目中实现加载和解析 Outlook MSG 文件的功能。

首先,学习如何从文件或流中加载 MSG 文件。

加载 MSG 文件

以下代码片段展示如何加载 MSG 文件。

// Create an instance of MapiMessage from file
var msg = MapiMessage.Load(@"message.msg");

// Get subject
Console.WriteLine("Subject:" + msg.Subject);

// Get from address
Console.WriteLine("From:" + msg.SenderEmailAddress);

// Get body
Console.WriteLine("Body" + msg.Body);

// Get recipients information
Console.WriteLine("Recipient: " + msg.Recipients);

// Get attachments
foreach (var att in msg.Attachments)
{
    Console.Write("Attachment Name: " + att.FileName);
    Console.Write("Attachment Display Name: " + att.DisplayName);
}

下面的代码示例展示了如何使用 MailMessage 加载 MSG 格式的消息。


var eml = MailMessage.Load("message.msg");

需要注意的是,生成的消息会转换为 EML 格式,包括嵌入的消息附件。如果想保留原始消息的一些特定 MSG 格式属性,请不要使用此加载方法。

要保留嵌入消息附件的原始格式,请使用 msgLoadOptions.PreserveEmbeddedMessageFormat 属性。


var msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.PreserveEmbeddedMessageFormat = true;
var msg = MailMessage.Load(stream, msgLoadOptions);

从流加载

以下代码片段展示如何从流中加载文件。

// Create an instance of MapiMessage from file
byte[] bytes = File.ReadAllBytes(@"message.msg");

using (MemoryStream stream = new MemoryStream(bytes))
{
    stream.Seek(0, SeekOrigin.Begin);
    // Create an instance of MapiMessage from file
    MapiMessage msg = MapiMessage.Load(stream);

    // Get subject
    Console.WriteLine("Subject:" + msg.Subject);

    // Get from address
    Console.WriteLine("From:" + msg.SenderEmailAddress);

    // Get body
    Console.WriteLine("Body" + msg.Body);

}

在保留嵌入式 EML 格式的同时将 EML 转换为 MSG

EML 文件可以加载到 MapiMessage 类,通过实例化一个 MailMessage 对象并将其传递给 MapiMessage.FromMailMessage 方法。如果 EML 文件包含嵌入的 EML 文件,请使用 MapiConversionOptions.PreserveEmbeddedMessageFormat 以保留嵌入 EML 文件的格式。下面的代码片段展示如何将 EML 文件加载到 MapiMessage 同时保留嵌入的 EML 文件的格式。

// Load the EML file into a MailMessage object
var mailMessage = MailMessage.Load(emlFilePath);

// Set conversion options to preserve the format of embedded EML messages
var options = new MapiConversionOptions
    {
        PreserveEmbeddedMessageFormat = true
    };

// Convert MailMessage to MapiMessage, preserving embedded EML files
var mapiMessage = MapiMessage.FromMailMessage(mailMessage, options);

// Save the converted message in MSG format
mapiMessage.Save(msgFilePath);

解析 Outlook 消息文件

Aspose.Email for .NET 提供了 MapiMessage 用于打开和解析 MSG 文件的类。由于 MSG 文件中可能有许多收件人, MapiMessage 类公开了 收件人 返回一个 MapiRecipientCollection 表示一组 MapiRecipient 对象。该 MapiRecipient 对象进一步公开了用于处理收件人属性的方法。

以下步骤序列实现此目的:

  1. 创建该类的实例 MapiMessage 使用 MapiMessage.Load 静态方法的引用。
  2. 使用以下方式显示 MSG 文件的发件人姓名、主题和正文: SenderName, Subject 和 正文 属性。
  3. 使用 收件人 属性用于获取集合的引用 MapiRecipient 与 MSG 文件关联的对象。
  4. 循环遍历 MapiRecipientCollection 集合以显示每个 MapiRecipient 对象通过其公共方法。
//Instantiate an MSG file to load an MSG file from disk
var outlookMessageFile = MapiMessage.Load(dataDir + "message.msg");
//Display sender's name
Console.WriteLine("Sender Name : " + outlookMessageFile.SenderName);
//Display Subject
Console.WriteLine("Subject : " + outlookMessageFile.Subject);
//Display Body
Console.WriteLine("Body : " + outlookMessageFile.Body);
//Display Recipient's info
Console.WriteLine("Recipients : \n");

//Loop through the recipients collection associated with the MapiMessage object
foreach (var rcp in outlookMessageFile.Recipients)
{
	//Display recipient email address
	Console.WriteLine("Email : " + rcp.EmailAddress);
	//Display recipient name
	Console.WriteLine("Name : " + rcp.DisplayName);
	//Display recipient type
	Console.WriteLine("Recipient Type : " + rcp.RecipientType);
}