使用 PST 文件的异步操作

为提升现代应用的可伸缩性和响应性,Aspose.Email for .NET 提供了用于操作 PST 文件的异步方法。这些新 API 允许开发者在不阻塞主线程的情况下创建、打开、合并和拆分 PST 文件,非常适合 UI 应用和高负载服务。

异步创建 PST 文件

PersonalStorage.CreateAsync 此方法允许您异步创建新 PST 文件,使处理大数据或需要响应式 UI 的应用程序能够实现非阻塞执行。以下代码示例演示了如何在 C# 项目中实现此功能:

string pstFilePath = "newMailbox.pst";
var format = FileFormatVersion.Unicode;

// Create a new PST file asynchronously
using (var pst = await PersonalStorage.CreateAsync(pstFilePath, format, CancellationToken.None))
{
     Console.WriteLine($"PST file created at: {pstFilePath}");
}

异步打开 PST 文件

要异步打开现有 PST 文件,请使用 PersonalStorage.FromFileAsync Aspose.Email API 的方法。以下代码示例演示了如何在 C# 项目中实现此功能:

string pstFilePath = "newMailbox.pst";
var format = FileFormatVersion.Unicode;

// Open the PST file asynchronously
        using (var pst = await PersonalStorage.FromFileAsync(pstFilePath, CancellationToken.None))
        {
            Console.WriteLine("PST file opened successfully.");

            // Access folders or messages here
            Console.WriteLine($"Root folder name: {pst.RootFolder.DisplayName}");
}

异步合并 PST 文件

MergeWithAsync Aspose.Email 的方法 PersonalStorage 此类允许您异步将一个 PST 文件的内容合并到另一个 PST 文件中。这在合并邮件存档、组合备份或批量处理电子邮件数据等场景中非常有用——不会阻塞应用程序的主线程。下面的代码示例演示了如何在 C# 项目中使用此方法:

string targetPstPath = "mainMailbox.pst";
string sourcePstPath = "archiveToMerge.pst";

// Open both PST files asynchronously
using (var targetPst = await PersonalStorage.FromFileAsync(targetPstPath, CancellationToken.None))
using (var sourcePst = await PersonalStorage.FromFileAsync(sourcePstPath, CancellationToken.None))
{
    // Merge the source PST into the target PST
    await targetPst.MergeWithAsync(sourcePst, CancellationToken.None);

    Console.WriteLine("Merge operation completed successfully.");
}

异步拆分 PST 文件

下面的代码示例演示了如何使用异步方式打开现有 PST 文件 FromFileAsync,然后使用 SplitIntoAsync。每个部分的大小不超过 50 MB,并保存到指定的输出目录。异步执行确保操作不会阻塞主线程,适用于处理大型 PST 档案的应用程序。

using (var pst = await PersonalStorage.FromFileAsync("input.pst"))
{
    await pst.SplitIntoAsync(50 * 1024 * 1024, "part_", "outputDirectory", CancellationToken.None);
}