Konwertuj MSG do innych formatów

MSG jest formatem pliku pojedynczego elementu Outlook – e‑mail, ale także spotkania, kontaktu lub zadania. Aspose.Email dla C++ otwiera go przy użyciu MapiMessage::Load, który zachowuje każdą właściwość MAPI elementu, lub z MailMessage::Load, które przekształca e‑mail w jego formę MIME. Który format wybrać, zależy od celu: formaty oparte na MIME zaczynają się od jednego z nich, podczas gdy eksport kalendarza i kontaktów wymaga elementu MAPI.

Przykłady w tym artykule używają następujących nagłówków i przestrzeni nazw:

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

Konwertuj MSG na EML i EMLX

MapiMessage::Save przyjmuje te same SaveOptions jako MailMessage, tak aby plik MSG stał się plikiem EML lub plikiem Apple Mail EMLX jednym wywołaniem.

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

Kiedy konwersja wymaga dostrojenia, skonwertuj element do MailMessage najpierw z ToMailMessage. MailConversionOptions następnie decyduje, np., czy zachować ciało RTF elementu.

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

Konwertuj MSG na HTML i MHTML

To samo Save wywołanie generuje stronę internetową. HTML daje stronę z oddzielnym folderem zasobów lub osadzonymi zasobami, w zależności od HtmlSaveOptions; MHTML pakuje wszystko w jeden plik.

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

Konwersja MSG do OFT

SaveAsTemplate zapisuje element jako szablon Outlook: otwarcie go w Outlooku rozpoczyna nową wiadomość z tą samą treścią.

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

Konwertuj kalendarz MSG na ICS

Spotkanie zapisane z Outlooka to plik MSG, którego elementem jest MapiCalendar. Sprawdź SupportedType, skonwertuj element przy użyciu ToMapiMessageItem, i zapisz go z 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());
}

Konwertuj kontakt MSG na VCF

Kontakt zapisany z Outlooka to MapiContact; ContactSaveFormat VCard zapisuje go jako plik 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());
}

Dodaj wiadomości MSG do pliku MBOX

MBOX przechowuje wiadomości MIME, więc każdy element MSG jest konwertowany przy pomocy ToMailMessage przed MboxrdStorageWriter::WriteMessage dołącza go.

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

Dodaj elementy MSG do pliku PST

PST przechowuje elementy MAPI bezpośrednio, więc załadowany MapiMessage przechodzi do folderu niezmieniony. Umieść każdy rodzaj elementu w przeznaczonym dla niego folderze – e‑maile w folderze poczty, spotkania w folderze Kalendarz, kontakty w folderze Kontakty – aby Outlook wyświetlał je odpowiednim widokiem.

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