本简短教程包含有关如何在 C# 中不使用 Outlook 打开 MSG 文件的信息。您将获得有关环境设置的详细信息、逐步过程和可运行的示例代码,以在 C# 中打开 MSG 文件。您将学习阅读和显示 MSG 文件的不同属性以及加载的 MSG 中附件的详细信息。
在 C# 中打开 Outlook MSG 文件的步骤
- 通过添加 Aspose.Email for .NET 来创建环境以读取 MSG 文件
- 将源 MSG 文件加载到 MapiMessage 类对象中
- 读取并显示加载的 MSG 文件的不同属性
- 解析 MSG 文件中的所有 attachments
- 读取并显示 MSG 文件中每个附件的不同属性
这些步骤解释了如何在 C# 中打开 MSG 文件并检索其属性。首先,您可以将 MSG 文件加载到 MapiMessage 对象中,然后检索其属性以及附件详细信息。请注意,MapiMessage.Load() 方法具有多个重载函数,使用 LoadOptions 类对象支持不同的加载选项,例如设置消息格式和首选文本编码。
在 C# 中读取 Outlook MSG 文件的代码
using System; | |
using Aspose.Email; | |
using Aspose.Email.Mapi; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to read Outlook message using C# | |
{ | |
// Initialize license | |
License lic = new License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Load the MSG file | |
MapiMessage mapiMsg = MapiMessage.Load("message.msg"); | |
// Display subject | |
Console.WriteLine("Subject:" + mapiMsg.Subject); | |
// Display from address | |
Console.WriteLine("From:" + mapiMsg.SenderEmailAddress); | |
// Display body | |
Console.WriteLine("Body" + mapiMsg.Body); | |
// Display recipients information | |
Console.WriteLine("Recipient: " + mapiMsg.Recipients); | |
// Display attachments | |
foreach (MapiAttachment att in mapiMsg.Attachments) | |
{ | |
Console.Write("File Name: " + att.FileName); | |
Console.Write("Display Name: " + att.DisplayName); | |
} | |
System.Console.WriteLine("Outlook message read successfully"); | |
} | |
} | |
} |
此代码演示如何在 C# 中打开 Outlook MSG 文件。您可以显示许多其他属性,例如账单信息、正文类型、客户提交时间、公司和对话主题等。您还可以根据需要设置属性,并在附件集合中使用不同的方法来添加、插入和删除附件。
本文教会了我们如何在 C# 中不使用 Outlook 打开 Outlook MSG 文件。如果您想了解将 EML 转换为 MSG 文件的过程,请参阅 如何使用 C# 将 EML 转换为 MSG 上的文章。