这个简短的主题重点介绍如何在 C#** 中将 MBOX 转换为 PST 文件,方法是提供建立环境的参考,然后提供执行此任务的程序流程。本主题将通过几行代码引导您将 MBOX 保存到 C# 中的 PST 文件,这些代码可用于任何 .NET 支持的环境,包括 Windows、Linux 和 macOS。
在 C# 中将 MBOX 转换为 PST 文件的步骤
- 设置开发环境以包含来自 NuGet 包管理器的 Aspose.Email for .NET
- 使用 PersonalStorage 类实例在磁盘上创建一个空的输出 PST 存储文件
- 在 PST 中添加收件箱文件夹
- 使用 MboxrdStorageReader 类并通过设置 MBOX 加载选项从磁盘加载源 MBOX 文件
- 遍历 MBOX 消息并将它们插入所需的 PST 文件夹中
上述步骤描述了使用 C#* 从 MBOX 创建 PST 的简单过程,其中通过创建一个包含 Inbox 文件夹的空 PST 文件开始该过程。然后使用 MailMessage 类迭代 MBOX 中的消息,并在使用 MboxrdStorageReader 类实例从磁盘加载源 MBOX 文件后保存在 PST 文件夹中。
使用 C# 从 MBOX 创建 PST 的代码
using System; | |
using Aspose.Email; | |
using Aspose.Email.Mapi; | |
using Aspose.Email.Storage.Mbox; | |
using Aspose.Email.Storage.Pst; | |
namespace KBEmail | |
{ | |
public class MboxToPST | |
{ | |
public static void MboxToPSTCoverter() | |
{ | |
string FilePath = @"C:/TestData/"; | |
// Initialize a license to convert MBOX to a PST File | |
Aspose.Email.License LicenseMboxToPST = new Aspose.Email.License(); | |
LicenseMboxToPST.SetLicense(FilePath + "Conholdate.Total.Product.Family.lic"); | |
// Create the target PST file | |
using (PersonalStorage desPersonalStorage = PersonalStorage.Create(FilePath + "TargetOutput.pst", FileFormatVersion.Unicode)) | |
{ | |
// Add a subfolder inside the PST | |
FolderInfo inbox = desPersonalStorage.RootFolder.AddSubFolder("Inbox"); | |
// Set the MBOX Load options to load the file | |
MboxLoadOptions mboxLoadOptions = new MboxLoadOptions(); | |
mboxLoadOptions.LeaveOpen = false; | |
// Open the source MBOX file from the disk | |
using (MboxrdStorageReader mboxReader = new MboxrdStorageReader(FilePath + "srcInputFile.mbox", mboxLoadOptions)) | |
{ | |
MailMessage itrMessage; | |
// Iterate through the MBOX messages and add them inside a selected PST sub-folder | |
while ((itrMessage = mboxReader.ReadNextMessage()) != null) | |
{ | |
using (MapiMessage mapi = MapiMessage.FromMailMessage(itrMessage, MapiConversionOptions.UnicodeFormat)) | |
inbox.AddMessage(mapi); | |
} | |
} | |
} | |
} | |
} | |
} |
这段代码演示了为了开发一个 MBOX 到 PST 转换器,可以在简单的 API 调用的帮助下有效地使用基于 C# 的 API。这是一个两步过程,首先在磁盘上创建一个 PST 存储文件及其各自的收件箱文件夹,然后在后续步骤中,目标 MBOX 文件消息在从磁盘加载文件后迭代并保存在 PST 文件夹中。
在本文中,我们了解了如何使用简单的 API 接口在 C# MBOX 到 PST 转换器应用程序中进行开发。如果您想了解如何在 C# 中将一个 PST 文件拆分为多个 PST 文件,请参阅 如何使用 C# 拆分 PST 文件 上的文章。