导出和提取 Zimbra TGZ 内容
Contents
[
Hide
]
读取备份本身很少是最终目标——通常需要将消息导向其他位置。 TgzReader 支持两种方式:一次性导出镜像磁盘上的邮箱,或在遍历时手动保存您挑选的消息。
以下示例使用以下头文件和命名空间:
#include <MailMessage.h>
#include <SaveOptions.h>
#include <EmlSaveOptions.h>
#include <MsgSaveOptions.h>
#include <AttachmentCollection.h>
#include <Storage/Zimbra/TgzReader.h>
#include <system/io/directory.h>
#include <system/string_comparison.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::Storage::Zimbra;
导出整个邮箱
ExportTo 接受一个目录路径并将备份的每个项目写入其下,为每个 Zimbra 文件夹创建子目录。消息以原始 MIME 形式保存,非邮件项目如联系人则以各自格式写入(联系人会变成一个 .vcf 文件所示)。目标目录必须已存在。
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
// the target directory must exist and be writable
IO::Directory::CreateDirectory_(u"ZimbraExport");
// recreate the mailbox folder tree on disk and write every message into it
reader->ExportTo(u"ZimbraExport");
reader->Dispose();
保存选定的邮件
当只需要邮箱的一部分时,如 读取 Zimbra TGZ 文件 并保存 CurrentMessage 您自己。因为它是一个 MailMessage开头的项目,任何 SaveOptions 可以应用。示例仅保留文件夹路径以 Inbox 并将它们写为 EML:
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
IO::Directory::CreateDirectory_(u"ZimbraInbox");
int32_t index = 0;
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message == nullptr)
{
continue;
}
// keep only the items that belong to the Inbox folder
if (reader->get_CurrentDirectory().StartsWith(u"Inbox", StringComparison::OrdinalIgnoreCase))
{
message->Save(String::Format(u"ZimbraInbox\\message_{0}.eml", index++), SaveOptions::get_DefaultEml());
}
}
while (reader->ReadNextMessage());
reader->Dispose();
按消息属性筛选
任何消息属性都可以驱动决定。此处通过传递以下参数,将携带附件的消息保存为 Outlook MSG 格式 SaveOptions::DefaultMsgUnicode:
SharedPtr<TgzReader> reader = MakeObject<TgzReader>(u"ZimbraSample.tgz");
IO::Directory::CreateDirectory_(u"ZimbraWithAttachments");
int32_t index = 0;
do
{
SharedPtr<MailMessage> message = reader->get_CurrentMessage();
if (message != nullptr && message->get_Attachments()->get_Count() > 0)
{
// Outlook MSG format this time
message->Save(String::Format(u"ZimbraWithAttachments\\message_{0}.msg", index++), SaveOptions::get_DefaultMsgUnicode());
}
}
while (reader->ReadNextMessage());
reader->Dispose();