Outlook MSG 파일 로드 및 구문 분석

**Aspose.Email for C++**는 Microsoft Outlook 메시지 파일(.msg)을 읽고 분석하기 위한 강력한 도구를 제공합니다. MapiMessage 해당 클래스는 MSG 파일을 로드하고 구문 분석하는 데 사용되며, 제목, 발신자, 본문, 수신인 및 첨부 파일과 같은 메시지 세부 정보에 접근할 수 있습니다. 애플리케이션 요구에 따라 파일에서 직접 또는 메모리 스트림에서 메시지를 로드할 수 있습니다.

이 문서의 샘플은 다음 헤더와 네임스페이스를 사용합니다:

#include <MailMessage.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiAttachment.h>
#include <Mapi/MapiAttachmentCollection.h>
#include <Mapi/MapiRecipient.h>
#include <Mapi/MapiRecipientCollection.h>
#include <Mapi/MapiConversionOptions.h>
#include <system/array.h>
#include <system/convert.h>
#include <system/io/file.h>
#include <system/io/memory_stream.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;

디스크에서 MSG 파일 로드

MapiMessage::Load 경로에서 MSG 파일을 읽습니다. 반환된 객체는 제목, 발신자, 본문, 수신자 및 첨부 파일에 직접 접근할 수 있습니다.

SharedPtr<MapiMessage> msg = MapiMessage::Load(u"MsgWithAtt.msg");

Console::WriteLine(u"Subject: " + msg->get_Subject());
Console::WriteLine(u"From: " + msg->get_SenderEmailAddress());
Console::WriteLine(u"Body: " + msg->get_Body());
Console::WriteLine(u"Recipients: " + Convert::ToString(msg->get_Recipients()->get_Count()));

for (SharedPtr<MapiAttachment> att : msg->get_Attachments())
{
    Console::WriteLine(u"Attachment: " + att->get_LongFileName() + u" (display name: " + att->get_DisplayName() + u")");
}

스트림에서 MSG 파일 로드

메시지가 파일에서 오지 않을 때 - 데이터베이스에 보관되거나 네트워크를 통해 수신되었거나 메모리에서 생성된 경우 - MapiMessage::Load 스트림도 허용합니다. 스트림은 MSG 데이터 시작 지점에 위치해야 합니다.

ArrayPtr<uint8_t> bytes = IO::File::ReadAllBytes(u"Message.msg");
SharedPtr<IO::MemoryStream> stream = MakeObject<IO::MemoryStream>(bytes);

SharedPtr<MapiMessage> msg = MapiMessage::Load(stream);

Console::WriteLine(u"Subject: " + msg->get_Subject());
Console::WriteLine(u"From: " + msg->get_SenderEmailAddress());
Console::WriteLine(u"Body: " + msg->get_Body());

EML을 MSG로 변환하면서 포함된 메시지 형식 유지

다른 메시지를 첨부 파일로 포함하는 EML 파일은 MSG로 변환될 때 MIME 형태를 잃습니다: 첨부된 항목도 Outlook 메시지가 됩니다. PreserveEmbeddedMessageFormat 대신 EML로 유지합니다. 이는 첨부 파일이 바이트 단위로 그대로 유지되어야 할 때 중요합니다.

SharedPtr<MailMessage> mailMessage = MailMessage::Load(u"tnefWithMsgInside.eml");

// the messages attached to the source file keep their EML format
SharedPtr<MapiConversionOptions> options = MakeObject<MapiConversionOptions>();
options->set_PreserveEmbeddedMessageFormat(true);

SharedPtr<MapiMessage> mapiMessage = MapiMessage::FromMailMessage(mailMessage, options);
mapiMessage->Save(u"PreservedEmbedded.msg");

for (SharedPtr<MapiAttachment> attachment : mapiMessage->get_Attachments())
{
    Console::WriteLine(attachment->get_DisplayName() + u" | " + attachment->get_MimeTag());
}

Outlook 메시지 속성 구문 분석

MapiMessage Outlook 항목의 필드를 직접 노출합니다 - 발신자, 제목, 본문, 메시지 클래스 및 전달 시간. 수신자는 MapiRecipientCollection 의 MapiRecipient 객체; 각 객체는 표시 이름, 주소 및 RecipientType - To, Cc 또는 Bcc.

SharedPtr<MapiMessage> outlookMessageFile = MapiMessage::Load(u"Message.msg");

Console::WriteLine(u"Sender name: " + outlookMessageFile->get_SenderName());
Console::WriteLine(u"Sender email: " + outlookMessageFile->get_SenderEmailAddress());
Console::WriteLine(u"Subject: " + outlookMessageFile->get_Subject());
Console::WriteLine(u"Body: " + outlookMessageFile->get_Body());
Console::WriteLine(u"Message class: " + outlookMessageFile->get_MessageClass());
Console::WriteLine(u"Delivery time: " + outlookMessageFile->get_DeliveryTime().ToString());

Console::WriteLine(u"Recipients:");
for (SharedPtr<MapiRecipient> recipient : outlookMessageFile->get_Recipients())
{
    Console::WriteLine(u"   " + recipient->get_DisplayName() + u" <" + recipient->get_EmailAddress() + u"> " +
        u"type: " + Convert::ToString(static_cast<int32_t>(recipient->get_RecipientType())));
}

전용 접근자가 없는 MAPI 속성은 속성 컬렉션을 통해 접근합니다. 자세한 내용은 다음을 참조하십시오 Outlook MAPI 속성 액세스 및 관리.