فیلتر و استخراج پیامهای MBOX
Parsing every message body just to build a list of subjects is wasteful. Aspose.Email for C++ lets you look at message summaries first, decide which items you need, and load only those. This article shows the summary objects, extraction by identifier, query-based search and the load options that apply while messages are read.
نمونههای زیر از سرآیندها و فضاهای نام زیر استفاده میکنند:
#include <MailMessage.h>
#include <MailAddress.h>
#include <MailAddressCollection.h>
#include <EmlLoadOptions.h>
#include <Storage/Mbox/MboxStorageReader.h>
#include <Storage/Mbox/MboxrdStorageReader.h>
#include <Storage/Mbox/MboxLoadOptions.h>
#include <Storage/Mbox/MboxMessageInfo.h>
#include <Storage/MailStorageConverter.h>
#include <Tools/Search/MailQueryBuilder.h>
#include <Tools/Search/MailQuery.h>
#include <Tools/Search/StringComparisonField.h>
#include <Tools/Search/DateComparisonField.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage;
using namespace Aspose::Email::Storage::Mbox;
using namespace Aspose::Email::Tools::Search;
خواندن خلاصههای پیام
EnumerateMessageInfo yields an MboxMessageInfo object per message. The object carries the header-level facts - Subject, Date, From, به, CC, Bcc - and the EntryId that identifies the message inside the file.
SharedPtr<MboxStorageReader> reader = MboxStorageReader::CreateReader(u"mailbox.mbox", MakeObject<MboxLoadOptions>());
for (SharedPtr<MboxMessageInfo> info : reader->EnumerateMessageInfo())
{
Console::WriteLine(u"Subject: " + info->get_Subject());
Console::WriteLine(u"Date: " + info->get_Date().ToString());
// a message without a From header yields a null sender
if (info->get_From() != nullptr)
{
Console::WriteLine(u"From: " + info->get_From()->get_Address());
}
Console::WriteLine(u"To: " + info->get_To()->ToString());
Console::WriteLine(u"CC: " + info->get_CC()->ToString());
Console::WriteLine(u"Bcc: " + info->get_Bcc()->ToString());
}
reader->Dispose();
Extract a Message by Its Identifier
این EntryId of a summary can be stored - in a database, an index, or a cache - and later handed to ExtractMessage, which jumps straight to that message instead of scanning the archive from the beginning. The second argument is an EmlLoadOptions instance that controls how the MIME data is parsed.
SharedPtr<MboxStorageReader> reader = MboxStorageReader::CreateReader(u"mailbox.mbox", MakeObject<MboxLoadOptions>());
for (SharedPtr<MboxMessageInfo> info : reader->EnumerateMessageInfo())
{
// The identifier can be stored and used later to fetch the message directly
String entryId = info->get_EntryId();
SharedPtr<MailMessage> eml = reader->ExtractMessage(entryId, MakeObject<EmlLoadOptions>());
Console::WriteLine(eml->get_Subject());
}
reader->Dispose();
Search the Archive with a Query
MailQueryBuilder assembles conditions on subject, sender, dates and other fields into a MailQuery. Passing the query to EnumerateMessages returns only the matching messages. The example selects messages whose subject contains "Report" and that were sent before the current day.
SharedPtr<MboxStorageReader> reader = MboxStorageReader::CreateReader(u"mailbox.mbox", MakeObject<MboxLoadOptions>());
// Build the criteria: subject contains "Report" and the message was sent before today
SharedPtr<MailQueryBuilder> builder = MakeObject<MailQueryBuilder>();
builder->get_Subject()->Contains(u"Report");
builder->get_SentDate()->Before(DateTime::get_Today());
SharedPtr<MailQuery> query = builder->GetQuery();
for (SharedPtr<MailMessage> message : reader->EnumerateMessages(query))
{
Console::WriteLine(u"Subject: " + message->get_Subject());
}
reader->Dispose();
Control the Load Options
Two places accept an EmlLoadOptions شیء:
- the ReadNextMessage(options) overload applies the options to a single read;
- the static MboxMessageOptions ویژگیٔ MailStorageConverter applies them to every message parsed during an MBOX to PST conversion.
The snippet keeps TNEF (winmail.dat) attachments untouched in both cases.
SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(u"mailbox.mbox", MakeObject<MboxLoadOptions>());
// Keep TNEF attachments intact while reading a single message
SharedPtr<EmlLoadOptions> emlOptions = MakeObject<EmlLoadOptions>();
emlOptions->set_PreserveTnefAttachments(true);
SharedPtr<MailMessage> eml = reader->ReadNextMessage(emlOptions);
Console::WriteLine(eml->get_Subject());
reader->Dispose();
// Apply the same options to every message parsed during an MBOX to PST conversion
MailStorageConverter::set_MboxMessageOptions(emlOptions);
MailStorageConverter::MboxToPst(u"mailbox.mbox", u"ConvertedWithTnef.pst");