在 Outlook 消息中管理附件
该 创建和保存 Outlook 消息(MSG)文件 本文解释了创建和保存消息的步骤,包括带附件的 MSG 文件。本文深入探讨了使用 Aspose.Email 管理 Microsoft Outlook 附件的方式。它将指导您如何使用该库从消息文件中访问和保存附件。 attachments 属性的 MapiMessage 类。此属性是属于 MapiAttachmentCollection 类,允许高效处理附件。
检查内嵌与常规附件
“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}")
识别 MAPI 消息中的引用附件
Aspose.Email API 包括 is_reference 属性的 MapiAttachment 类,使开发者能够识别消息中的引用附件。尝试下面的代码示例,了解它在 Python 项目中的工作方式:
import aspose.email as ae
# Load or create a MapiMessage
msg = ae.mapi.MapiMessage.load("Message_With_Reference_Attachment.msg")
# Iterate through the attachments
for attachment in msg.attachments:
if attachment.is_reference:
# Process reference attachment
pass
从 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.load(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)
TNEF 附件处理
加载和保存 TNEF 格式的附件
Aspose.Email 提供了处理 TNEF 附件的方法。这些方法允许以 TNEF 格式保存和加载附件,TNEF 常用于 Outlook 消息(winmail.dat)。
- static MapiAttachment MapiAttachment.load_from_tnef(string fileName) – 从 TNEF 文件中加载附件。
- static MapiAttachment MapiAttachment.load_from_tnef(Stream stream) – 从 TNEF 流中加载附件。
- void MapiAttachment.save_to_tnef(string filename) – 将附件保存到 TNEF 文件中。
- void MapiAttachment.save_to_tnef(Stream stream) – 将附件保存到 TNEF 流中。
以下代码示例演示了如何提取、保存并重新添加 TNEF 格式的电子邮件附件,同时通过内存流或文件进行管理:
import io
from aspose.email.mapi import MapiMessage, MapiAttachment
# Load the EML message (which contains a winmail.dat TNEF attachment)
msg = MapiMessage.load("message.eml")
# Save the first attachment as a winmail.dat file
msg.attachments[0].save_to_tnef("winmail.dat")
# Load the TNEF attachment from the file and add it back to the message
from_tnef_attachment = MapiAttachment.load_from_tnef("winmail.dat")
msg.attachments.add(from_tnef_attachment.long_file_name, from_tnef_attachment.binary_data)
# (Optional) If you also want to use a memory stream instead of the file
ms = io.BytesIO()
msg.attachments[0].save_to_tnef(ms)
# Reset the stream position to the beginning
ms.seek(0)
# Load the TNEF attachment directly from the memory stream and add it
from_tnef_attachment_from_stream = MapiAttachment.load_from_tnef(ms)
msg.attachments.add(from_tnef_attachment_from_stream.long_file_name, from_tnef_attachment_from_stream.binary_data)
检索嵌套的邮件消息附件
嵌入的 OLE(对象链接与嵌入)附件也出现在该类的附件集合中 MapiMessage 类。下面的代码示例演示了如何解析消息文件并提取嵌入式消息附件,然后将其保存到磁盘以供进一步使用。该 MapiMessage 类提供了一个 from_properties 静态方法用于从嵌入式附件创建新消息。以下代码片段展示了如何获取嵌套的邮件消息附件:
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.Email 库提供了从 Microsoft Outlook 消息(.msg)文件中删除附件的功能。使用 remove_attachments(path) 该方法需要消息文件的路径作为参数。作为公共静态方法,您可以直接调用,无需实例化对象,使过程简单高效。
以下代码片段展示了如何使用 Aspose.Email 从 MSG 文件中删除附件:
import aspose.email as ae
ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")
您也可以调用该类的静态 destroy_attachments 方法 MapiMessage 类。它比 remove_attachments 更快,因为 remove_attachments 方法需要解析消息文件。
import aspose.email as ae
# Destroy attachments in the MapiMessage
ae.mapi.MapiMessage.destroy_attachments("AttachmentsToDestroy_out.msg")
添加 MSG 附件
Outlook 消息可以在附件中包含其他 Microsoft Outlook 消息,既可以是常规附件,也可以是嵌入式附件。该 MapiAttachmentCollection 提供了该类的重载成员 添加 方法用于创建包含两种类型附件的 Outlook 消息。
Try it out!
使用免费工具在线添加或删除电子邮件附件 Aspose.Email Editor App.
向 MAPI 消息添加引用附件
引用附件 通常指包含对外部资源的引用或链接的附件,而不是实际文件本身。这类引用常用于 HTML 邮件中,链接到托管在远程服务器上的外部图像或资源。引用附件不嵌入整个文件,而是包含指向外部内容的 URL 或引用。
要向 MAPI 消息添加引用附件,请使用 add 方法的 MapiAttachmentCollection class 和 ReferenceAttachmentOptions 类,用于指定:
-
内容 URL(文件的直接链接)
-
提供商 URL(指向文件位置或 UI 视图的链接)
-
附件提供商名称(例如,“GoogleDrive”)
如下面的代码示例所示。
import aspose.email as ae
# Load or create a MapiMessage
msg = ae.mapi.MapiMessage()
msg.subject = "Email with Reference Attachment"
msg.body = "This message contains a cloud-based reference attachment."
# Create ReferenceAttachmentOptions
options = ae.mapi.ReferenceAttachmentOptions(
"https://drive.google.com/file/d/1HJ-M3F2qq1oRrTZ2GZhUdErJNy2CT3DF/",
"https://drive.google.com/drive/my-drive",
"GoogleDrive"
)
# Add the reference attachment to the message
msg.attachments.add("Document.pdf", options)
# Save the message to MSG format
msg.save("Message_With_Reference_Attachment.msg")
将消息嵌入为附件
以下代码片段演示了如何将 Outlook MSG 文件作为附件嵌入到另一个 MSG 文件中。嵌入的 MSG 文件包含 PR_ATTACH_METHOD 属性,其值为 5,表示它是“message”类型的附件。
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.load(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 使您能够使用 Insert 方法将 MSG 附件插入到父 MSG 中 MapiAttachmentCollection。此方法的签名为 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 方法用于使用新数据更新嵌入式附件的内容。但请注意,该方法不能用于将 PR_ATTACH_NUM = 4 的附件插入仅包含 2 个附件的集合中。下面的代码片段演示了如何替换附件内容:
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 中重命名附件
使用 Aspose.Email,您可以轻松修改从文件加载的电子邮件消息中附件的显示名称。以下代码示例演示了如何实现此功能:
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"