将 MBOX 转换为 PST

从 Thunderbird 迁移到 Outlook 意味着将 MBOX 存档转换为 PST 文件。Aspose.Email for C++ 提供两种路径:手动循环,提供对文件夹和消息转换的完整控制,以及 MailStorageConverter 一次调用完成全部工作的帮助程序。

以下示例使用以下头文件和命名空间:

#include <MailMessage.h>
#include <EmlLoadOptions.h>
#include <Mapi/MapiMessage.h>
#include <Storage/Mbox/MboxStorageReader.h>
#include <Storage/Mbox/MboxrdStorageReader.h>
#include <Storage/Mbox/MboxLoadOptions.h>
#include <Storage/MailStorageConverter.h>
#include <Storage/MboxToPstConversionOptions.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/io/file_stream.h>
#include <system/io/memory_stream.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage;
using namespace Aspose::Email::Storage::Mbox;
using namespace Aspose::Email::Storage::Pst;

手动将消息复制到 PST

手动路由包括四个步骤:

  1. 使用 MboxStorageReader::CreateReader.
  2. 使用以下内容创建输出文件 PersonalStorage::Create.
  3. 添加目标文件夹——这里使用标准 Inbox,使用 CreatePredefinedFolder.
  4. 转换每个 MailMessage 到一个 MapiMessage 与 MapiMessage::FromMailMessage 并使用 FolderInfo::AddMessage.

因为您自行控制循环,轻松将消息路由到不同文件夹,跳过部分消息,或在存储前更改属性。

// Open the source MBOX archive
SharedPtr<MboxStorageReader> mbox = MboxStorageReader::CreateReader(u"mailbox.mbox", MakeObject<MboxLoadOptions>());

// Create the target Outlook data file
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"FromMbox.pst", FileFormatVersion::Unicode);

// Create the folder that will receive the imported messages
SharedPtr<FolderInfo> inbox = pst->CreatePredefinedFolder(u"Inbox", StandardIpmFolder::Inbox);

// Convert every message to MAPI and add it to the folder
for (SharedPtr<MailMessage> eml : mbox->EnumerateMessages())
{
    SharedPtr<MapiMessage> msg = MapiMessage::FromMailMessage(eml);
    inbox->AddMessage(msg);
}

pst->Dispose();
mbox->Dispose();

使用 MailStorageConverter 转换

MailStorageConverter::MboxToPst 读取存档,创建 PST,将每条消息放入 “Inbox” 文件夹并返回打开的 PersonalStorage。该 MboxMessageOptions 属性定义了 EmlLoadOptions 用于解析消息;在调用前分配它。

// Options used to parse each message taken from the MBOX file
MailStorageConverter::set_MboxMessageOptions(MakeObject<EmlLoadOptions>());

// One call: read the MBOX file and write a new PST file
SharedPtr<PersonalStorage> pst = MailStorageConverter::MboxToPst(u"mailbox.mbox", u"Converted.pst");

// the converter places the messages into an "Inbox" folder under the root
SharedPtr<FolderInfo> inbox = pst->get_RootFolder()->GetSubFolder(u"Inbox");
Console::WriteLine(u"Messages imported into Inbox: " + Convert::ToString(inbox->get_ContentCount()));

pst->Dispose();

在转换过程中去除数字签名

源消息的 S/MIME 签名并非总是想要的目标文件。设置 RemoveSignature 在一个 MboxToPstConversionOptions 对象并使用 MboxToPst 重载接受现有读取器、现有存储、目标文件夹名称和选项。此重载还允许将 PST 写入内存流,示例即如此操作。

MailStorageConverter::set_MboxMessageOptions(MakeObject<EmlLoadOptions>());

SharedPtr<IO::FileStream> mboxStream = MakeObject<IO::FileStream>(u"mailbox.mbox", IO::FileMode::Open, IO::FileAccess::Read);
SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(mboxStream, MakeObject<MboxLoadOptions>());

// The PST is built in memory here; a file path works the same way
SharedPtr<IO::MemoryStream> pstStream = MakeObject<IO::MemoryStream>();
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(pstStream, FileFormatVersion::Unicode);

// Strip S/MIME signatures from the messages while they are copied
SharedPtr<MboxToPstConversionOptions> options = MakeObject<MboxToPstConversionOptions>();
options->set_RemoveSignature(true);

MailStorageConverter::MboxToPst(reader, pst, u"Inbox", options);

Console::WriteLine(u"Messages imported: " + Convert::ToString(pst->get_RootFolder()->GetSubFolder(u"Inbox")->get_ContentCount()));

pst->Dispose();
reader->Dispose();
mboxStream->Close();