Tải & Phân tích tệp Outlook MSG

Aspose.Email cho C++ cung cấp các công cụ mạnh mẽ để đọc và phân tích các tệp tin nhắn Microsoft Outlook (.msg). The MapiMessage lớp được sử dụng để tải và phân tích các tệp MSG, cung cấp quyền truy cập vào chi tiết tin nhắn như chủ đề, người gửi, nội dung, người nhận và tệp đính kèm. Bạn có thể tải tin nhắn trực tiếp từ tệp hoặc từ luồng bộ nhớ, tùy thuộc vào nhu cầu của ứng dụng.

Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:

#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;

Tải tệp MSG từ đĩa

MapiMessage::Load đọc một tệp MSG từ đường dẫn. Đối tượng trả về cho phép truy cập trực tiếp vào tiêu đề, người gửi, nội dung, người nhận và tệp đính kèm của tin nhắn.

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

Tải tệp MSG từ luồng

Khi tin nhắn không đến từ một tệp - nó được lưu trong cơ sở dữ liệu, nhận qua mạng hoặc tạo trong bộ nhớ - MapiMessage::Load cũng chấp nhận một luồng. Luồng phải được đặt ở đầu dữ liệu 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());

Chuyển đổi EML sang MSG Giữ Định dạng của Các Tin nhắn Nhúng

Một tệp EML mang các tin nhắn khác làm tệp đính kèm sẽ mất dạng MIME khi nó được chuyển đổi sang MSG: các mục đính kèm cũng trở thành tin nhắn Outlook. PreserveEmbeddedMessageFormat giữ chúng dưới dạng EML thay vì, điều này quan trọng khi các tệp đính kèm phải giữ nguyên byte‑by‑byte như ban đầu.

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

Phân tích các Thuộc tính của Tin nhắn Outlook

MapiMessage phơi bày các trường của một mục Outlook ngay lập tức - người gửi, tiêu đề, nội dung, lớp tin nhắn và thời gian giao. Những người nhận là một MapiRecipientCollection của MapiRecipient đối tượng; mỗi đối tượng báo cáo tên hiển thị, địa chỉ và RecipientType - To, Cc hoặc 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())));
}

Các thuộc tính MAPI không có accessor riêng được truy cập thông qua bộ sưu tập thuộc tính, như mô tả trong Truy cập và Quản lý Các Thuộc tính MAPI của Outlook.