แปลง 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()));

แปลง Calendar 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());
}

แปลง Contact 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();