تبدیل MSG به فرمتهای دیگر
MSG فرمت فایل یک مورد Outlook واحد است - یک ایمیل، اما همچنین میتواند یک قرار، یک مخاطب یا یک کار باشد. Aspose.Email برای C++ آن را با MapiMessage::Load، که هر ویژگی MAPI مورد را نگه میدارد، یا با MailMessage::Load، که یک ایمیل را به شکل MIME آن تبدیل میکند. انتخاب بسته به هدف متفاوت است: فرمتهای مبتنی بر MIME از هر دو شروع میشوند، در حالی که صادرات تقویم و مخاطب به مورد MAPI نیاز دارند.
نمونههای این مقاله از این سرآیندها و فضای نامها استفاده میکنند:
#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;
تبدیل MSG به EML و EMLX
MapiMessage::Save همان پارامترها را میپذیرد SaveOptions به عنوان یک MailMessageتبدیل کنید، بنابراین یک فایل MSG در یک فراخوانی به یک EML یا یک فایل Apple Mail EMLX تبدیل میشود.
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());
وقتی تبدیل باید تنظیم شود، مورد را به یک MailMessage اول با ToMailMessage. MailConversionOptions سپس تصمیم میگیرد، بهعنوان مثال، آیا بدنه RTF مورد نگه داشته میشود یا نه.
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());
تبدیل MSG به HTML و MHTML
همان Save فراخوانی یک صفحه وب تولید میکند. HTML یک صفحه با پوشه منبع جداگانه یا منابع جاسازیشده میدهد، بسته به HtmlSaveOptions; MHTML همه چیز را در یک فایل بستهبندی میکند.
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());
تبدیل MSG به OFT
SaveAsTemplate مورد را بهعنوان قالب Outlook مینویسد: باز کردن آن در Outlook یک پیام جدید با همان محتوا را شروع میکند.
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()));
تبدیل یک Calendar MSG به ICS
یک قرار ذخیرهشده از Outlook یک فایل MSG است که مورد آن یک MapiCalendar. بررسی کنید SupportedType، مورد را با ToMapiMessageItem، و آن را با 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());
}
تبدیل یک Contact MSG به VCF
یک مخاطب ذخیرهشده از Outlook یک MapiContact; ContactSaveFormat VCard بهعنوان فایل 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());
}
افزودن پیامهای MSG به فایل MBOX
MBOX پیامهای MIME را ذخیره میکند، بنابراین هر مورد MSG با ToMailMessage قبل از MboxrdStorageWriter::WriteMessage به آن اضافه میکند.
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();
افزودن موارد MSG به فایل PST
یک PST موارد MAPI را مستقیماً ذخیره میکند، بنابراین یک MapiMessage بهصورت بدون تغییر به یک پوشه میرود. هر نوع مورد را به پوشهای که برای آن ساخته شده قرار دهید - ایمیلها به پوشه ایمیل، قرارها به پوشه Calendar، مخاطبان به پوشه Contacts - تا Outlook آنها را با نمای صحیح نشان دهد.
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();