המרת MSG לפורמטים אחרים

MSG הוא פורמט הקובץ של פריט Outlook יחיד – אימייל, אך גם פגישה, קשר או משימה. Aspose.Email עבור C++ פותח אותו עם MapiMessage::Load, המשמר כל מאפיין MAPI של הפריט, או עם MailMessage::Load, שממיר אימייל לצורתו MIME. הבחירה תלויה ביעד: פורמטים מבוססי MIME מתחילים באחד מהם, בעוד שייצוא לוח שנה וקשר דורש פריט MAPI.

הדוגמאות במאמר זה משתמשות בכותרות ובמרחבי שם אלה:

#include <MailMessage.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <HtmlSaveOptions.h>
#include <MhtSaveOptions.h>
#include <MsgSaveOptions.h>
#include <Calendar/AppointmentSaveFormat.h>
#include <Mapi/MapiMessage.h>
#include <Mapi/MapiItemType.h>
#include <Mapi/MapiCalendar.h>
#include <Mapi/MapiContact.h>
#include <Mapi/MapiContactNamePropertySet.h>
#include <Mapi/MailConversionOptions.h>
#include <Mapi/ContactSaveFormat.h>
#include <Storage/Mbox/MboxrdStorageWriter.h>
#include <Storage/Mbox/MboxSaveOptions.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/convert.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Calendar;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Mbox;
using namespace Aspose::Email::Storage::Pst;

המרת MSG ל‑EML ו‑EMLX

MapiMessage::Save מקבל את אותה SaveOptions כ‑ MailMessage, כך שקובץ MSG הופך ל‑EML או לקובץ Apple Mail EMLX בקריאה אחת.

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

msg->Save(u"from-msg.eml", SaveOptions::get_DefaultEml());
msg->Save(u"from-msg.emlx", SaveOptions::get_DefaultEmlx());

כאשר יש צורך לכוונן את ההמרה, המר את הפריט ל‑ MailMessage קודם עם ToMailMessage. MailConversionOptions ואז מחליט, למשל, אם לשמור את גוף RTF של הפריט.

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

SharedPtr<MailConversionOptions> options = MakeObject<MailConversionOptions>();
options->set_PreserveRtfContent(true);   // keep the RTF body of the Outlook item

SharedPtr<MailMessage> mail = msg->ToMailMessage(options);
mail->Save(u"from-msg-with-rtf.eml", SaveOptions::get_DefaultEml());

המרת MSG ל‑HTML ו‑MHTML

זהה Save קריאה מייצרת דף אינטרנט. HTML נותן דף עם תיקיית משאבים נפרדת או משאבים מוטמעים, בהתאם ל‑ HtmlSaveOptions; MHTML אורז את הכול בקובץ אחד.

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

msg->Save(u"from-msg.html", SaveOptions::get_DefaultHtml());
msg->Save(u"from-msg.mhtml", SaveOptions::get_DefaultMhtml());

המרת MSG ל‑OFT

SaveAsTemplate כותב את הפריט כתבנית Outlook: פתיחת הקובץ ב‑Outlook מתחילה הודעה חדשה עם אותו תוכן.

SharedPtr<MapiMessage> msg = MapiMessage::Load(u"sample.msg");
msg->SaveAsTemplate(u"from-msg.oft");

Console::WriteLine(u"Template: " + Convert::ToString(MapiMessage::Load(u"from-msg.oft")->get_IsTemplate()));

המרת MSG של לוח שנה ל‑ICS

פגישה שנשמרה מ‑Outlook היא קובץ MSG שהפריט בו הוא MapiCalendar. בדוק SupportedType, המר את הפריט עם ToMapiMessageItem, ושומר אותו עם AppointmentSaveFormat::Ics.

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

if (msg->get_SupportedType() == MapiItemType::Calendar)
{
    SharedPtr<MapiCalendar> calendar = DynamicCast<MapiCalendar>(msg->ToMapiMessageItem());
    calendar->Save(u"from-msg.ics", AppointmentSaveFormat::Ics);
    Console::WriteLine(u"Exported: " + calendar->get_Subject());
}

המרת MSG של קשר ל‑VCF

איש קשר שנשמר מ‑Outlook הוא MapiContact; ContactSaveFormat VCard כותב אותו קובץ vCard.

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

if (msg->get_SupportedType() == MapiItemType::Contact)
{
    SharedPtr<MapiContact> contact = DynamicCast<MapiContact>(msg->ToMapiMessageItem());
    contact->Save(u"from-msg.vcf", ContactSaveFormat::VCard);
    Console::WriteLine(u"Exported: " + contact->get_NameInfo()->get_DisplayName());
}

הוספת הודעות MSG לקובץ MBOX

MBOX מאחסן הודעות MIME, ולכן כל פריט MSG מומר באמצעות ToMailMessage לפני MboxrdStorageWriter::WriteMessage מוסיף אותו.

SharedPtr<MboxrdStorageWriter> writer = MakeObject<MboxrdStorageWriter>(u"from-msg.mbox", MakeObject<MboxSaveOptions>());

for (String file : { String(u"sample.msg"), String(u"Message.msg") })
{
    SharedPtr<MapiMessage> msg = MapiMessage::Load(file);
    writer->WriteMessage(msg->ToMailMessage(MakeObject<MailConversionOptions>()));
}

writer->Dispose();

הוספת פריטי MSG לקובץ PST

PST מאחסן פריטי MAPI ישירות, ולכן קובץ MapiMessage נכנס לתיקייה ללא שינוי. שמור כל סוג פריט בתיקייה שהוקצתה לו – הודעות במאגר דוא"ל, פגישות בתיקיית לוח השנה, אנשי קשר בתיקיית אנשי הקשר – כך ש‑Outlook יציג אותם בתצוגה הנכונה.

SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"from-msg.pst", FileFormatVersion::Unicode);
SharedPtr<FolderInfo> inbox = pst->CreatePredefinedFolder(u"Inbox", StandardIpmFolder::Inbox);
SharedPtr<FolderInfo> calendar = pst->CreatePredefinedFolder(u"Calendar", StandardIpmFolder::Appointments);
SharedPtr<FolderInfo> contacts = pst->CreatePredefinedFolder(u"Contacts", StandardIpmFolder::Contacts);

for (String file : { String(u"sample.msg"), String(u"Test_Meeting.msg"), String(u"Contact.msg") })
{
    SharedPtr<MapiMessage> msg = MapiMessage::Load(file);

    switch (msg->get_SupportedType())
    {
    case MapiItemType::Calendar:
        calendar->AddMessage(msg);
        break;
    case MapiItemType::Contact:
        contacts->AddMessage(msg);
        break;
    default:
        inbox->AddMessage(msg);
        break;
    }
}

Console::WriteLine(u"Inbox: " + Convert::ToString(inbox->get_ContentCount()) +
    u", Calendar: " + Convert::ToString(calendar->get_ContentCount()) +
    u", Contacts: " + Convert::ToString(contacts->get_ContentCount()));
pst->Dispose();