管理 Outlook MSG 文件中的附件
Contents
[
Hide
]
Aspose.Email for C++ 提供丰富的 API,用于在处理 Microsoft Outlook MSG 文件时访问、保存、删除和嵌入附件。附件通过以下方式处理 MapiMessage 类,使用其 Attachments 属性,公开一个 MapiAttachmentCollection.
从 MSG 文件保存附件
从 MSG 文件提取并保存附件:
- 使用以下方式加载邮件 MapiMessage::Load.
- 遍历 MapiAttachmentCollection.
- 使用以下方法保存每个附件 MapiAttachment::Save() 方法。
// Create an instance of MapiMessage from file
System::SharedPtr<MapiMessage> message = MapiMessage::Load(fileName);
// Iterate through the attachments collection
{
auto attachment_enumerator = (message->get_Attachments())->GetEnumerator();
decltype(attachment_enumerator->get_Current()) attachment;
while (attachment_enumerator->MoveNext() && (attachment = attachment_enumerator->get_Current(), true))
{
// Save the individual attachment
attachment->Save(dataDir + attachment->get_FileName());
}
}
删除附件
Aspose.Email for C++ 提供两种方式从 MSG 文件中删除附件:
- 调用 RemoveAttachments() method**
它以消息文件路径为参数实现为公共静态方法,无需实例化对象。此静态帮助方法可删除消息文件中的所有附件。
下面的代码片段展示了如何使用此方法。
- 调用 DestoryAttachment() method**
它更快,因为在未完全解析 MSG 文件的情况下即可删除附件。
添加 MSG 附件
MSG 文件可以包含其他 MSG 文件,既可以作为标准附件,也可以作为嵌入式附件。使用重载的 Add 中的方法 MapiAttachmentCollection 以嵌入 Outlook 消息。
下面的代码示例演示了如何创建具有指定发件人、收件人、主题和正文的新 MAPI 消息,然后将现有 MSG 文件作为嵌入式消息附件,最后将包含嵌入附件的消息保存为新的 MSG 文件。
System::SharedPtr<MapiMessage> message = System::MakeObject<MapiMessage>(L"from@test.com", L"to@test.com", L"Subj", L"This is a message body");
System::SharedPtr<MapiMessage> attachMsg = MapiMessage::Load(L"Message.msg");
message->get_Attachments()->Add(L"Weekly report.msg", attachMsg);
message->Save(dataDir + L"WithEmbeddedMsg_out.msg");