MBOX 메시지 필터링 및 추출

모든 메시지 본문을 파싱해 주제 목록을 만드는 것은 비효율적입니다. C++용 Aspose.Email은 먼저 메시지 요약을 살펴보고 필요한 항목을 결정한 뒤 해당 항목만 로드할 수 있게 합니다. 이 문서에서는 요약 객체, 식별자 기반 추출, 쿼리 기반 검색 및 메시지 읽기 시 적용되는 로드 옵션을 소개합니다.

아래 샘플은 다음 헤더와 네임스페이스를 사용합니다:

#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 결과는 MboxMessageInfo 각 메시지당 객체. 객체는 헤더 수준의 사실을 담고 있습니다 - Subject, Date, From, To, CC, Bcc - 및 EntryId 파일 내에서 메시지를 식별하는 데 사용됩니다.

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();

식별자로 메시지 추출

다음은 EntryId 요약을 데이터베이스, 인덱스 또는 캐시 등에 저장하고 나중에 ExtractMessage, 이는 아카이브 시작부터 스캔하지 않고 바로 해당 메시지로 이동합니다. 두 번째 인자는 EmlLoadOptions MIME 데이터를 파싱하는 방식을 제어하는 인스턴스입니다.

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();

쿼리로 아카이브 검색

MailQueryBuilder 제목, 발신자, 날짜 및 기타 필드에 대한 조건을 조합하여 MailQuery. 쿼리를 EnumerateMessages 일치하는 메시지만 반환합니다. 예제는 제목에 "Report"가 포함되고 현재 날짜 이전에 전송된 메시지를 선택합니다.

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();

로드 옵션 제어

두 곳에서 EmlLoadOptions 객체:

이 스니펫은 두 경우 모두 TNEF (winmail.dat) 첨부 파일을 손대지 않고 유지합니다.

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");