การอ่านไฟล์ MBOX
โปรไฟล์ Thunderbird เก็บแต่ละโฟลเดอร์เมลเป็นไฟล์ MBOX เดียว Aspose.Email สำหรับ 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();
EnumerateMessages อีกครั้งบนไฟล์ที่ได้ทำการนับแล้ว
ตั้งค่าการเข้ารหัสข้อความที่ต้องการ
ไฟล์สำรองที่สร้างโดยไคลเอนต์เก่าอาจมีข้อความ 8‑bit โดยไม่มีการระบุชุดอักขระ ให้วางรหัสอักขระให้กับ 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();