处理消息附件
使用 Aspose Outlook 管理附件
《创建和保存 Outlook 消息(MSG)文件》解释了如何创建和保存消息,以及如何创建带附件的 MSG 文件。本文阐述了如何使用 Aspose.Email 管理 Microsoft Outlook 附件。使用 MapiMessage 类的 Attachments 属性可以访问消息文件中的附件并将其保存到磁盘。Attachments 属性是 MapiAttachmentCollection 类型的集合。
检查附件是 Inline 还是 Regular
“Inline”(内嵌)和 “Regular”(常规)附件指的是它们在电子邮件中的包含方式。常规附件是传统方式附加的文件,通常在邮件客户端以列表形式显示,收件人可以下载并保存到本地。内嵌附件,也称为嵌入式或内联图像,通常用于在邮件正文中包含图像或其他媒体。它们不会出现在单独的列表中,而是直接显示在邮件内容中,例如邮件正文内。这使您能够在消息内容中加入图像或其他媒体。下面的代码示例演示了如何判断附件是内嵌还是常规:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("message.msg")
for attachment in msg.attachments:
print(f"{attachment.display_name}:{attachment.is_inline}")
从 Outlook 消息(MSG)文件保存附件
从 MSG 文件保存附件:
- 遍历 MapiAttachmentCollection 集合以获取各个附件。
- 要保存附件,请调用 MapiAttachment 类的 Save() 方法。
下面的代码片段展示了如何将附件保存到本地磁盘。
import aspose.email as ae
data_dir = "C://dataDir/"
file_name = "message.msg"
# Create an instance of MapiMessage from file
message = ae.mapi.MapiMessage.from_file(data_dir + file_name)
# Iterate through the attachments collection
for attachment in message.attachments:
# Save the individual attachment
attachment.save(data_dir + attachment.file_name)
获取嵌套的邮件消息附件
嵌入的 OLE 附件也会出现在 MapiMessage 类的 Attachment 集合中。以下代码示例解析消息文件中的嵌入式消息附件并将其保存到磁盘。MapiMessage 类的 FromProperties() 静态方法可以从嵌入的附件创建新消息。下面的代码片段展示了如何获取嵌套的邮件消息附件。
import aspose.email as ae
eml = ae.mapi.MapiMessage.load("my.msg")
# Create a MapiMessage object from the individual attachment
get_attachment = ae.mapi.MapiMessage.from_properties(eml.attachments[0].object_data.properties)
# Create an object of type MailMessageInterpreter from the above message and save the embedded message to a file on disk
mail_message = get_attachment.to_mail_message(ae.mapi.MailConversionOptions())
mail_message.save("NestedMailMessageAttachments_out.eml", ae.SaveOptions.default_eml)
移除附件
Aspose Outlook 库提供了从 Microsoft Outlook 消息(.msg)文件中删除附件的功能:
- 调用 RemoveAttachments() 方法。它接受消息文件的路径作为参数。该方法实现为公共静态方法,无需实例化对象。
下面的代码片段展示了如何删除附件。
import aspose.email as ae
ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")
您还可以调用 MapiMessage 类的静态方法 DestoryAttachment()。它比 RemoveAttachment() 更快,因为后者会解析消息文件。
import aspose.email as ae
# Destroy attachments in the MapiMessage
ae.mapi.MapiMessage.destroy_attachments(data_dir + "AttachmentsToDestroy_out.msg")
添加 MSG 附件
Outlook 消息的附件中可以包含其他 Microsoft Outlook 消息,可作为普通附件或嵌入式消息。MapiAttachmentCollection 为 Add 方法提供了重载成员,以创建包含这两种类型附件的 Outlook 消息。
Try it out!
使用免费工具在线添加或删除电子邮件附件 Aspose.Email Editor App.
向 MapiMessage 添加参考附件
“参考附件”通常指包含对外部资源的引用或链接的附件,而不是实际文件本身。这类引用常用于 HTML 邮件中,以链接远程服务器上的图像或资源。参考附件不嵌入整个文件,而是包含指向外部内容的 URL 或引用。
Aspose.Email 提供了一套工具用于正确显示参考附件,示例代码如下:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("message.msg")
# Add reference attachment
msg.attachments.add("Document.pdf",
"https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
"https://drive.google.com/drive/my-drive",
"GoogleDrive")
# Also, you can set additional attachment properties
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PERMISSION_TYPE, int(ae.AttachmentPermissionType.ANYONE_CAN_EDIT))
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_ORIGINAL_PERMISSION_TYPE, 0)
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_IS_FOLDER, False)
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PROVIDER_ENDPOINT_URL, "")
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_PREVIEW_URL, "")
msg.attachments[0].set_property(ae.mapi.KnownPropertyList.ATTACHMENT_THUMBNAIL_URL, "")
# Finally save the message
msg.save("msg_with_ref_attach.msg")
将消息嵌入为附件
下面的代码片段展示了 Outlook MSG 文件中嵌入的 MSG 文件其 PR_ATTACH_METHOD 值为 5 的情况。
import aspose.email as ae
# Create a new MapiMessage
message = ae.mapi.MapiMessage("from@test.com", "to@test.com", "Subj", "This is a message body")
# Load the attachment message
attach_msg = ae.mapi.MapiMessage.load("Message.msg")
# Add the attachment to the message
message.attachments.add("Weekly report.msg", attach_msg)
# Save the message with the embedded message attachment
message.save("WithEmbeddedMsg_out.msg")
从附件读取嵌入式消息
下面的代码片段展示了如何读取附件中的嵌入式消息。
import aspose.email as ae
file_name = "path/to/file.msg"
# Load the MapiMessage from file
message = ae.mapi.MapiMessage.from_file(file_name)
# Check if the first attachment is an Outlook message
if message.attachments[0].object_data.is_outlook_message:
# Get the embedded message as MapiMessage
embedded_message = message.attachments[0].object_data.to_mapi_message()
# Perform further operations with the embedded message
# ...
附件的插入和替换
Aspose.Email API 提供了在父消息中按特定索引插入附件的功能,并且能够用另一个消息附件替换附件内容。下面的代码片段展示了附件的插入和替换。
在特定位置插入
Aspose.Email API 提供了使用 MapiAttachmentCollection 的 Insert 方法将 MSG 附件插入到父 MSG 的功能,示例签名为 MapiAttachmentCollection Insert(int index, string name, MapiMessage msg)。下面的代码片段展示了如何在指定位置插入。
import aspose.email as ae
from io import BytesIO
file_name = "path/to/file.msg"
# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)
# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)
# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)
# Insert the loaded attachment at index 1
message.attachments.insert(1, "new 11", get_data)
替换附件内容
可以使用 Replace 方法将嵌入的附件内容替换为新内容。但无法在集合计数为 2 的情况下,向集合中插入 PR_ATTACH_NUM = 4(例如)的附件。下面的代码片段展示了如何替换附件内容。
import aspose.email as ae
from io import BytesIO
file_name = "path/to/file.msg"
# Load the MapiMessage from file
message = ae.mapi.MapiMessage.load(file_name)
# Save the attachment to a memory stream
memory_stream = BytesIO()
message.attachments[2].save(memory_stream)
# Load the attachment from the memory stream
get_data = ae.mapi.MapiMessage.load(memory_stream)
# Replace the attachment at index 1 with the loaded attachment
message.attachments.replace(1, "new 1", get_data)
在 MapiMessage 中重命名附件
可以修改从文件加载的电子邮件中附件的显示名称。下面的代码示例展示了如何实现:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("message.msg")
msg.attachments[0].display_name = "New display name 1"
msg.attachments[1].display_name = "New display name 2"