读取 MBOX 文件

Thunderbird 配置文件将每个邮件文件夹保存为单个 MBOX 文件。Aspose.Email for C++ 使用 MboxrdStorageReader 类,它将每个存储的项目作为普通的 MailMessage。因为读取器会流式遍历文件,内存使用量不取决于归档的大小。

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

#include <MailMessage.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <Storage/Mbox/MboxStorageReader.h>
#include <Storage/Mbox/MboxrdStorageReader.h>
#include <Storage/Mbox/MboxLoadOptions.h>
#include <system/io/file_stream.h>
#include <system/text/encoding.h>
#include <system/convert.h>

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

逐条读取消息

处理归档的最直接方式是将其打开为流,并创建一个 MboxrdStorageReader 在该流上遍历并持续调用 ReadNextMessage 直到返回 nullptr。每个返回的 MailMessage 可以以任何支持的格式检查或保存;示例将每条消息存储为 EML 和 MSG 两种格式。

// Open the storage file as a stream
SharedPtr<IO::FileStream> stream = MakeObject<IO::FileStream>(u"mailbox.mbox", IO::FileMode::Open, IO::FileAccess::Read);

// Create the reader and pass the stream to it
SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(stream, MakeObject<MboxLoadOptions>());

// Read the first message
SharedPtr<MailMessage> message = reader->ReadNextMessage();
int index = 0;

// Walk through the remaining messages
while (message != nullptr)
{
    Console::WriteLine(u"Subject: " + message->get_Subject());

    // Store the message in EML and MSG formats
    message->Save(String::Format(u"message_{0}.eml", index), SaveOptions::get_DefaultEml());
    message->Save(String::Format(u"message_{0}.msg", index), SaveOptions::get_DefaultMsgUnicode());
    index++;

    // Move to the next message
    message = reader->ReadNextMessage();
}

// Release the reader and the stream
reader->Dispose();
stream->Close();

读取器还接受文件名而不是流: MakeObject<MboxrdStorageReader>(u"mailbox.mbox", options).

分批读取消息

当只需要大型存档的一部分——例如填充列表视图的单页时——使用 EnumerateMessages(startIndex, count) 的重载 MboxStorageReader接受。静态 CreateReader 方法为文件挑选正确的读取器实现。相同的一对参数也被 EnumerateMessageInfo,它返回摘要而非完整消息。

SharedPtr<MboxStorageReader> reader = MboxStorageReader::CreateReader(u"mailbox.mbox", MakeObject<MboxLoadOptions>());

int32_t startIndex = 0;
int32_t count = 10; // read the archive in batches of 10 messages

for (SharedPtr<MailMessage> message : reader->EnumerateMessages(startIndex, count))
{
    Console::WriteLine(u"Subject: " + message->get_Subject());
}

reader->Dispose();

设置首选文本编码

较旧客户端生成的归档可能包含未声明字符集的 8 位文本。为 PreferredTextEncoding 属性属于 MboxLoadOptions 以便此类文本按您期望的方式解码。

SharedPtr<MboxLoadOptions> options = MakeObject<MboxLoadOptions>();
options->set_PreferredTextEncoding(System::Text::Encoding::get_UTF8());

SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(u"mailbox.mbox", options);
SharedPtr<MailMessage> message = reader->ReadNextMessage();
Console::WriteLine(message->get_Subject());
reader->Dispose();

统计消息数

GetTotalItemsCount 扫描文件并返回存储的消息数。这对于在长时间循环开始前报告进度非常方便。

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

Console::WriteLine(u"Total number of messages in the MBOX file: " + Convert::ToString(reader->GetTotalItemsCount()));

reader->Dispose();
stream->Close();

获取当前消息的大小

在每个 ReadNextMessage 调用 CurrentDataSize 属性保存消息在文件中占用的字节数——在您想要按字节报告进度或跳过超大项时非常有用。

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

SharedPtr<MailMessage> message;
while ((message = reader->ReadNextMessage()) != nullptr)
{
    // Size, in bytes, of the message that has just been read
    int64_t currentDataSize = reader->get_CurrentDataSize();
    Console::WriteLine(message->get_Subject() + u": " + Convert::ToString(currentDataSize) + u" bytes");
}

reader->Dispose();
stream->Close();