โหลดและแยกวิเคราะห์ไฟล์ 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 ที่บรรจุข้อความอื่นเป็นไฟล์แนบจะสูญเสียรูปแบบ MIME เมื่อแปลงเป็น MSG: รายการแนบจะแปลงเป็นข้อความ 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 objects; แต่ละอันแสดงชื่อที่แสดง, ที่อยู่ และ 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 ที่ไม่มีตัวเข้าถึงเฉพาะจะเข้าถึงได้ผ่านคอลเลกชันของคุณสมบัติ ตามที่อธิบายไว้ใน เข้าถึงและจัดการคุณสมบัติ MAPI ของ Outlook.