טעינה ופענוח קבצי Outlook MSG

Aspose.Email for C++ מספקת כלי עבודה חזקים לקריאה וניתוח קבצי הודעה של Microsoft Outlook (.msg). ה MapiMessage ה‑class משמשת לטעון ולפענח קבצי 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.