Converting OLM to PST

Outlook for Windows cannot import an OLM archive, so moving a mailbox from a Mac to a PC usually means producing a PST file with the same folders and items. Aspose.Email for C++ can do this without either Outlook version installed: it reads the archive with OlmStorage and writes the result with PersonalStorage.

The sample below uses the following headers and namespaces:

#include <Storage/Olm/OlmStorage.h>
#include <Storage/Olm/OlmFolder.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FolderInfo.h>
#include <Mapi/MapiMessage.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <system/collections/list_ext.h>

using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Mapi;
using namespace Aspose::Email::Storage::Olm;
using namespace Aspose::Email::Storage::Pst;

Conversion Outline

  1. Open the source file with an OlmStorage constructor so that FolderHierarchy is available.
  2. Create the destination with PersonalStorage::Create.
  3. Decide the container class of each new PST folder from the class of the messages it will hold - contacts go into an IPF.Contact folder, appointments into IPF.Appointment, and so on - so that Outlook shows the folder with the right icon and view.
  4. Recurse through the OLM tree: for every folder, enumerate its messages with EnumerateMapiMessages, create the matching PST folder with AddSubFolder and add the messages with AddMessage.

Helper Functions

GetContainerClass maps a MAPI message class to the container class of the folder:

// Maps a message class to the container class the target folder should have
String GetContainerClass(String messageClass)
{
    if (messageClass.StartsWith(u"IPM.Contact") || messageClass.StartsWith(u"IPM.DistList"))
    {
        return u"IPF.Contact";
    }
    if (messageClass.StartsWith(u"IPM.StickyNote"))
    {
        return u"IPF.StickyNote";
    }
    if (messageClass.StartsWith(u"IPM.Activity"))
    {
        return u"IPF.Journal";
    }
    if (messageClass.StartsWith(u"IPM.Task"))
    {
        return u"IPF.Task";
    }
    if (messageClass.StartsWith(u"IPM.Appointment") || messageClass.StartsWith(u"IPM.Schedule.meeting"))
    {
        return u"IPF.Appointment";
    }
    return u"IPF.Note";
}

AddToPst copies one OLM folder - messages and sub-folders - under a PST folder. The PST folder is created lazily, when the first message reveals which container class it needs; an empty OLM folder is created with the default class. Items that cannot be stored are reported and skipped, so one problematic message does not abort the whole migration.

// Copies one OLM folder (with its messages and sub-folders) under the given PST folder
void AddToPst(SharedPtr<FolderInfo> pstFolder, SharedPtr<OlmFolder> olmFolder)
{
    SharedPtr<FolderInfo> pstSubFolder = pstFolder->GetSubFolder(olmFolder->get_Name());

    for (SharedPtr<MapiMessage> msg : olmFolder->EnumerateMapiMessages())
    {
        if (pstSubFolder == nullptr)
        {
            // the first message decides the container class of the new folder
            pstSubFolder = pstFolder->AddSubFolder(olmFolder->get_Name(), GetContainerClass(msg->get_MessageClass()));
        }

        try
        {
            pstSubFolder->AddMessage(msg);
        }
        catch (Exception& ex)
        {
            // report the item and carry on with the rest of the folder
            Console::WriteLine(u"Skipped '" + msg->get_Subject() + u"': " + ex->get_Message());
        }
    }

    if (pstSubFolder == nullptr)
    {
        // empty folder: create it with the default container class
        pstSubFolder = pstFolder->AddSubFolder(olmFolder->get_Name());
    }

    for (SharedPtr<OlmFolder> olmSubFolder : olmFolder->get_SubFolders())
    {
        AddToPst(pstSubFolder, olmSubFolder);
    }
}

Run the Conversion

With the helpers in place, the main routine is short: open both files and feed every top-level OLM folder to AddToPst, starting at the RootFolder of the PST.

// open the source OLM file
SharedPtr<OlmStorage> olm = MakeObject<OlmStorage>(u"SampleOLM.olm");

// create the target PST file
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"FromOlm.pst", FileFormatVersion::Unicode);

// walk the OLM hierarchy and rebuild it inside the PST
for (SharedPtr<OlmFolder> olmFolder : olm->get_FolderHierarchy())
{
    AddToPst(pst->get_RootFolder(), olmFolder);
}

pst->Dispose();
olm->Dispose();