Reading MBOX Files
A Thunderbird profile keeps each mail folder as a single MBOX file. Aspose.Email for C++ reads such files with the MboxrdStorageReader class, which returns every stored item as a regular MailMessage. Because the reader streams through the file, memory usage does not depend on the size of the archive.
The samples below use the following headers and namespaces:
#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;
Read Messages One by One
The most direct way to process an archive is to open it as a stream, create an MboxrdStorageReader over that stream and keep calling ReadNextMessage until it returns nullptr. Each returned MailMessage can be inspected or saved in any supported format; the example stores every message as both EML and 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();
The reader also accepts a file name instead of a stream: MakeObject<MboxrdStorageReader>(u"mailbox.mbox", options).
Read Messages in Batches
When only a slice of a large archive is needed - for example, to fill one page of a list view - use the EnumerateMessages(startIndex, count) overload of MboxStorageReader. The static CreateReader method picks the right reader implementation for the file. The same pair of arguments is accepted by EnumerateMessageInfo, which returns summaries instead of full messages.
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();
EnumerateMessages again on the one that has already been enumerated.
Set the Preferred Text Encoding
Archives produced by older clients may contain 8-bit text without a declared character set. Assign an encoding to the PreferredTextEncoding property of MboxLoadOptions so that such text is decoded the way you expect.
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();
Count the Messages
GetTotalItemsCount scans the file and returns the number of stored messages. It is handy for progress reporting before a long-running loop starts.
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();
Get the Size of the Current Message
After each ReadNextMessage call the CurrentDataSize property holds the number of bytes the message occupies in the file - useful when you want to report progress in bytes or skip oversized items.
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();