Convert MSG to Other Formats

MSG is the file format of a single Outlook item - an email, but also an appointment, a contact or a task. Aspose.Email for C++ opens it with MapiMessage::Load, which keeps every MAPI property of the item, or with MailMessage::Load, which turns an email into its MIME form. Which one to use depends on the target: MIME-based formats start from either, while calendar and contact exports need the MAPI item.

The samples in this article use these headers and namespaces:

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

Convert MSG to EML and EMLX

MapiMessage::Save accepts the same SaveOptions as a MailMessage, so an MSG file becomes an EML or an Apple Mail EMLX file in one call.

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

When the conversion has to be tuned, convert the item to a MailMessage first with ToMailMessage. MailConversionOptions then decides, for example, whether the RTF body of the item is kept.

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

Convert MSG to HTML and MHTML

The same Save call produces a web page. HTML gives a page with a separate resource folder or embedded resources, depending on HtmlSaveOptions; MHTML packs everything into one file.

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

Convert MSG to OFT

SaveAsTemplate writes the item as an Outlook template: opening it in Outlook starts a new message with the same content.

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

Convert a Calendar MSG to ICS

An appointment saved from Outlook is an MSG file whose item is a MapiCalendar. Check SupportedType, convert the item with ToMapiMessageItem, and save it with 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());
}

Convert a Contact MSG to VCF

A contact saved from Outlook is a MapiContact; ContactSaveFormat VCard writes it as a vCard file.

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

Add MSG Messages to an MBOX File

MBOX stores MIME messages, so each MSG item is converted with ToMailMessage before MboxrdStorageWriter::WriteMessage appends it.

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

Add MSG Items to a PST File

A PST stores MAPI items directly, so a loaded MapiMessage goes into a folder unchanged. Put each kind of item into the folder made for it - emails into a mail folder, appointments into the Calendar folder, contacts into the Contacts folder - so that Outlook shows them with the right view.

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