การแปลง OLM เป็น PST

Outlook for Windows ไม่สามารถนำเข้าไฟล์ OLM ได้ ดังนั้นการย้ายกล่องจดหมายจาก Mac ไป PC ส่วนใหญ่หมายถึงการสร้างไฟล์ PST ที่มีโฟลเดอร์และรายการเดียวกัน Aspose.Email for C++ สามารถทำได้โดยไม่ต้องติดตั้ง Outlook เวอร์ชันใดก็ได้: มันอ่านไฟล์อาโครไบฟด้วย OlmStorage และเขียนผลลัพธ์ด้วย PersonalStorage.

ตัวอย่างด้านล่างใช้หัวไฟล์และเนมสเปซต่อไปนี้:

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

โครงร่างการแปลง

  1. เปิดไฟล์ต้นทางด้วย OlmStorage คอนสตรัคเตอร์เพื่อให้ FolderHierarchy พร้อมใช้งาน
  2. สร้างปลายทางด้วย PersonalStorage::Create.
  3. กำหนด คลาสคอนเทนเนอร์ ของโฟลเดอร์ PST ใหม่แต่ละโฟลเดอร์จากคลาสของข้อความที่จะเก็บ - รายชื่อผู้ติดต่อจะเข้าไปใน IPF.Contact โฟลเดอร์, นัดหมายเข้า IPF.Appointment, แล้วต่อไป - เพื่อให้ Outlook แสดงโฟลเดอร์พร้อมไอคอนและมุมมองที่ถูกต้อง
  4. ทำการวนซ้ำผ่านต้นไม้ OLM: สำหรับทุกโฟลเดอร์ ให้ enumerate ข้อความของมันด้วย EnumerateMapiMessages, สร้างโฟลเดอร์ PST ที่ตรงกันด้วย AddSubFolder และเพิ่มข้อความด้วย AddMessage.

ฟังก์ชันช่วยเหลือ

GetContainerClass จับคู่คลาสข้อความ MAPI กับคลาสคอนเทนเนอร์ของโฟลเดอร์:

// 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 คัดลอกโฟลเดอร์ OLM หนึ่งโฟลเดอร์ - ข้อความและโฟลเดอร์ย่อย - ไปยังโฟลเดอร์ PST โฟลเดอร์ PST จะถูกสร้างแบบ lazy เมื่อลูกข้อความแรกบ่งบอกว่าต้องการคลาสคอนเทนเนอร์ใด; โฟลเดอร์ OLM เปล่าจะถูกสร้างด้วยคลาสเริ่มต้น รายการที่ไม่สามารถจัดเก็บได้จะถูกรายงานและข้ามไป ดังนั้นข้อความที่มีปัญหาเดียวจะไม่ทำให้การย้ายทั้งหมดล้มเหลว

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

เรียกใช้การแปลง

เมื่อได้ตั้งค่าตัวช่วยแล้ว รูทีนหลักจะสั้นลง: เปิดไฟล์ทั้งสองและจ่ายทุกโฟลเดอร์ OLM ระดับบนสุดให้ AddToPst, เริ่มต้นที่ RootFolder ของ 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();