Chuyển đổi MBOX sang PST

Di chuyển từ Thunderbird sang Outlook nghĩa là chuyển đổi một kho lưu trữ MBOX sang tệp PST. Aspose.Email cho C++ cung cấp hai cách: một vòng lặp thủ công cho phép bạn kiểm soát hoàn toàn thư mục và việc chuyển đổi tin, và MailStorageConverter trợ giúp thực hiện toàn bộ công việc trong một lệnh.

Các mẫu dưới đây sử dụng các tiêu đề và không gian tên sau:

#include <MailMessage.h>
#include <EmlLoadOptions.h>
#include <Mapi/MapiMessage.h>
#include <Storage/Mbox/MboxStorageReader.h>
#include <Storage/Mbox/MboxrdStorageReader.h>
#include <Storage/Mbox/MboxLoadOptions.h>
#include <Storage/MailStorageConverter.h>
#include <Storage/MboxToPstConversionOptions.h>
#include <Storage/Pst/PersonalStorage.h>
#include <Storage/Pst/FolderInfo.h>
#include <Storage/Pst/FileFormatVersion.h>
#include <Storage/Pst/StandardIpmFolder.h>
#include <system/io/file_stream.h>
#include <system/io/memory_stream.h>
#include <system/convert.h>

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

Sao chép tin vào PST một cách thủ công

Quy trình thủ công gồm bốn bước:

  1. Mở kho lưu trữ bằng MboxStorageReader::CreateReader.
  2. Tạo tệp đầu ra với PersonalStorage::Create.
  3. Thêm thư mục đích - ở đây là Inbox tiêu chuẩn, được tạo bằng CreatePredefinedFolder.
  4. Chuyển đổi mỗi MailMessage tới một MapiMessage với MapiMessage::FromMailMessage và thêm nó với FolderInfo::AddMessage.

Vì bạn tự điều khiển vòng lặp, rất dễ đưa tin vào các thư mục khác nhau, bỏ qua một số tin, hoặc thay đổi thuộc tính trước khi lưu.

// Open the source MBOX archive
SharedPtr<MboxStorageReader> mbox = MboxStorageReader::CreateReader(u"mailbox.mbox", MakeObject<MboxLoadOptions>());

// Create the target Outlook data file
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(u"FromMbox.pst", FileFormatVersion::Unicode);

// Create the folder that will receive the imported messages
SharedPtr<FolderInfo> inbox = pst->CreatePredefinedFolder(u"Inbox", StandardIpmFolder::Inbox);

// Convert every message to MAPI and add it to the folder
for (SharedPtr<MailMessage> eml : mbox->EnumerateMessages())
{
    SharedPtr<MapiMessage> msg = MapiMessage::FromMailMessage(eml);
    inbox->AddMessage(msg);
}

pst->Dispose();
mbox->Dispose();

Chuyển đổi với MailStorageConverter

MailStorageConverter::MboxToPst đọc kho lưu trữ, tạo PST, đưa mỗi tin vào thư mục "Inbox" và trả về đối tượng đã mở PersonalStorage. Thuộc tính MboxMessageOptions thuộc tính xác định EmlLoadOptions được dùng để phân tích tin; gán nó trước khi gọi.

// Options used to parse each message taken from the MBOX file
MailStorageConverter::set_MboxMessageOptions(MakeObject<EmlLoadOptions>());

// One call: read the MBOX file and write a new PST file
SharedPtr<PersonalStorage> pst = MailStorageConverter::MboxToPst(u"mailbox.mbox", u"Converted.pst");

// the converter places the messages into an "Inbox" folder under the root
SharedPtr<FolderInfo> inbox = pst->get_RootFolder()->GetSubFolder(u"Inbox");
Console::WriteLine(u"Messages imported into Inbox: " + Convert::ToString(inbox->get_ContentCount()));

pst->Dispose();

Loại bỏ chữ ký số khi chuyển đổi

Chữ ký S/MIME của tin nguồn không phải lúc nào cũng muốn có trong tệp đích. Đặt RemoveSignature trên một MboxToPstConversionOptions đối tượng và sử dụng MboxToPst gói quá tải nhận một bộ đọc hiện có, một kho lưu trữ hiện có, tên thư mục đích và các tùy chọn. Gói này cũng cho phép bạn ghi PST vào một luồng bộ nhớ, như ví dụ đã làm.

MailStorageConverter::set_MboxMessageOptions(MakeObject<EmlLoadOptions>());

SharedPtr<IO::FileStream> mboxStream = MakeObject<IO::FileStream>(u"mailbox.mbox", IO::FileMode::Open, IO::FileAccess::Read);
SharedPtr<MboxrdStorageReader> reader = MakeObject<MboxrdStorageReader>(mboxStream, MakeObject<MboxLoadOptions>());

// The PST is built in memory here; a file path works the same way
SharedPtr<IO::MemoryStream> pstStream = MakeObject<IO::MemoryStream>();
SharedPtr<PersonalStorage> pst = PersonalStorage::Create(pstStream, FileFormatVersion::Unicode);

// Strip S/MIME signatures from the messages while they are copied
SharedPtr<MboxToPstConversionOptions> options = MakeObject<MboxToPstConversionOptions>();
options->set_RemoveSignature(true);

MailStorageConverter::MboxToPst(reader, pst, u"Inbox", options);

Console::WriteLine(u"Messages imported: " + Convert::ToString(pst->get_RootFolder()->GetSubFolder(u"Inbox")->get_ContentCount()));

pst->Dispose();
reader->Dispose();
mboxStream->Close();