بارگذاری و تجزیه فایل‌های Outlook MSG

Aspose.Email برای 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 اشیاء؛ هر کدام نام نمایشی، آدرس و 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.