Chuyển đổi MSG sang các định dạng khác

MSG là định dạng tệp của một mục Outlook duy nhất - một email, nhưng cũng có thể là một cuộc hẹn, danh bạ hoặc công việc. Aspose.Email for C++ mở nó với MapiMessage::Load, giữ mọi thuộc tính MAPI của mục, hoặc với MailMessage::Load, chuyển đổi email thành dạng MIME của nó. Lựa chọn nào phụ thuộc vào đích: các định dạng dựa trên MIME bắt đầu từ cả hai, trong khi xuất lịch và danh bạ cần mục MAPI.

Các mẫu trong bài viết này sử dụng các tiêu đề và không gian tên sau:

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

Chuyển Đổi MSG sang EML và EMLX

MapiMessage::Save chấp nhận cùng một SaveOptions Như một MailMessage, vì vậy một tệp MSG trở thành EML hoặc tệp Apple Mail EMLX trong một lần gọi.

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

Khi việc chuyển đổi cần được tinh chỉnh, chuyển mục sang một MailMessage đầu tiên với ToMailMessage. MailConversionOptions sau đó quyết định, ví dụ, liệu phần thân RTF của mục có được giữ lại hay không.

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

Chuyển Đổi MSG sang HTML và MHTML

Cùng một Save gọi tạo ra một trang web. HTML cung cấp một trang với thư mục tài nguyên riêng hoặc tài nguyên nhúng, tùy thuộc vào HtmlSaveOptions; MHTML đóng gói mọi thứ vào một tệp.

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

Chuyển đổi MSG sang OFT

SaveAsTemplate ghi mục dưới dạng mẫu Outlook: mở nó trong Outlook sẽ bắt đầu một tin nhắn mới với cùng nội dung.

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

Chuyển Đổi Calendar MSG sang ICS

Một cuộc hẹn được lưu từ Outlook là một tệp MSG mà mục là một MapiCalendar. Kiểm tra SupportedType, chuyển đổi mục với ToMapiMessageItem, và lưu nó với 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());
}

Chuyển Đổi Contact MSG sang VCF

Một danh bạ được lưu từ Outlook là một MapiContact; ContactSaveFormat VCard ghi nó dưới dạng tệp 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());
}

Thêm Các Tin Nhắn MSG vào Tệp MBOX

MBOX lưu trữ các tin nhắn MIME, vì vậy mỗi mục MSG được chuyển đổi bằng ToMailMessage trước MboxrdStorageWriter::WriteMessage đính kèm nó.

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

Thêm Các Mục MSG vào Tệp PST

Một PST lưu trữ các mục MAPI trực tiếp, vì vậy một MapiMessage đi vào một thư mục không thay đổi. Đặt mỗi loại mục vào thư mục được tạo cho nó - email vào thư mục mail, cuộc hẹn vào thư mục Calendar, danh bạ vào thư mục Contacts - để Outlook hiển thị chúng với chế độ xem đúng.

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