Làm việc với Đính Kèm Tin Nhắn

Quản lý tệp đính kèm với Aspose Outlook

Tạo và Lưu các tệp Outlook Message (MSG) giải thích cách tạo và lưu tin nhắn, và cách tạo tệp MSG có đính kèm. Bài viết này trình bày cách quản lý các tệp đính kèm Microsoft Outlook bằng Aspose.Email. Các tệp đính kèm từ một tệp tin nhắn được truy cập và lưu vào đĩa bằng thuộc tính Attachments của lớp MapiMessage. Thuộc tính Attachments là một bộ sưu tập thuộc loại lớp MapiAttachmentCollection.

Kiểm tra tệp đính kèm là Inline hay Regular

"Inline" và "Regular" đề cập tới cách chúng được bao gồm trong một tin nhắn email. Đính kèm Regular là các tệp được đính kèm theo cách truyền thống. Chúng thường hiển thị trong danh sách trong khách hàng email và có thể được người nhận tải xuống và lưu vào bộ nhớ cục bộ. Đính kèm Inline, còn gọi là nhúng hoặc ảnh inline, thường được dùng để chèn hình ảnh hoặc phương tiện khác vào phần thân của email. Chúng không hiển thị trong danh sách riêng mà được hiển thị trực tiếp trong nội dung email, chẳng hạn trong phần thân email. Điều này cho phép bạn bao gồm hình ảnh hoặc phương tiện khác là một phần của nội dung tin nhắn. Đoạn mã dưới đây minh họa cách xác định xem một đính kèm là inline hay 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}")

Lưu tệp đính kèm từ tệp Outlook Message (MSG)

Để lưu các đính kèm từ tệp MSG:

  1. Lặp qua bộ sưu tập MapiAttachmentCollection và lấy các tệp đính kèm riêng lẻ.
  2. Để lưu các tệp đính kèm, gọi phương thức Save() của lớp MapiAttachment.

Đoạn mã dưới đây cho bạn thấy cách lưu các tệp đính kèm vào đĩa cục bộ.

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)

Lấy các tệp đính kèm tin nhắn lồng nhau

Các tệp đính kèm OLE nhúng cũng xuất hiện trong bộ sưu tập Attachment của lớp MapiMessage. Ví dụ mã dưới đây phân tích tệp tin tin nhắn để tìm các tệp đính kèm tin nhắn nhúng và lưu chúng vào đĩa. Phương thức tĩnh FromProperties() của lớp MapiMessage có thể tạo một tin nhắn mới từ tệp đính kèm nhúng. Đoạn mã dưới đây cho bạn thấy cách lấy các tệp đính kèm tin nhắn lồng nhau.

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)

Xóa Tệp Đính Kèm

Thư viện Aspose Outlook cung cấp chức năng xóa tệp đính kèm khỏi các tệp Microsoft Outlook Message (.msg):

  • Gọi phương thức RemoveAttachments(). Nó nhận đường dẫn của tệp tin tin nhắn làm tham số. Phương thức này được triển khai dưới dạng public static, vì vậy bạn không cần khởi tạo đối tượng.

Đoạn mã dưới đây cho bạn thấy cách xóa bỏ các tệp đính kèm.

import aspose.email as ae

ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")

Bạn cũng có thể gọi phương thức tĩnh DestoryAttachment() của lớp MapiMessage. Nó hoạt động nhanh hơn RemoveAttachment() vì phương thức RemoveAttachment() phải phân tích tệp tin tin nhắn.

import aspose.email as ae

# Destroy attachments in the MapiMessage
ae.mapi.MapiMessage.destroy_attachments(data_dir + "AttachmentsToDestroy_out.msg")

Thêm tệp đính kèm MSG

Một tin nhắn Outlook có thể chứa các tin nhắn Outlook khác dưới dạng tệp đính kèm, có thể là tin nhắn thường hoặc nhúng. MapiAttachmentCollection cung cấp các phương thức Add được nạp chồng để tạo tin nhắn Outlook với cả hai loại tệp đính kèm.

Thêm tệp đính kèm tham chiếu vào MapiMessage

"Tệp đính kèm tham chiếu" thường chỉ một tệp đính kèm chứa tham chiếu hoặc liên kết đến một tài nguyên bên ngoài thay vì tệp thực tế. Những tham chiếu này thường được sử dụng trong email HTML để liên kết đến hình ảnh hoặc tài nguyên lưu trên máy chủ từ xa. Thay vì nhúng toàn bộ tệp, tệp đính kèm tham chiếu chỉ bao gồm một URL hoặc tham chiếu đến nội dung bên ngoài.

Aspose.Email cung cấp một bộ công cụ để hiển thị đúng các tệp đính kèm tham chiếu, được trình bày trong mẫu mã dưới đây:

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")

Nhúng một tin nhắn dưới dạng tệp đính kèm

Đoạn mã dưới đây cho bạn thấy cách các tệp MSG Outlook nhúng trong một tệp MSG chứa PR_ATTACH_METHOD có giá trị bằng 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")

Đọc Tin nhắn Nhúng từ Đính kèm

Đoạn mã dưới đây cho bạn thấy cách đọc tin nhắn nhúng từ tệp đính kèm.

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
    # ...

Chèn và Thay thế tệp đính kèm

API Aspose.Email cung cấp khả năng chèn tệp đính kèm vào vị trí chỉ định trong tin nhắn cha. Nó cũng cho phép thay thế nội dung của một tệp đính kèm bằng tệp đính kèm của tin nhắn khác. Đoạn mã dưới đây cho bạn thấy cách Chèn và Thay thế tệp đính kèm.

Chèn tại vị trí cụ thể

API Aspose.Email cung cấp khả năng chèn một tệp đính kèm MSG vào MSG cha bằng phương thức Insert của MapiAttachmentCollection: MapiAttachmentCollection Insert(int index, string name, MapiMessage msg). Đoạn mã dưới đây cho bạn thấy cách chèn tại vị trí cụ thể.

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)

Thay thế Nội dung Đính kèm

Điều này có thể được sử dụng để thay thế nội dung tệp đính kèm nhúng bằng các nội dung mới bằng phương thức Replace. Tuy nhiên, không thể dùng để chèn tệp đính kèm với PR_ATTACH_NUM = 4 (ví dụ) vào bộ sưu tập khi collection.Count = 2. Đoạn mã dưới đây cho bạn thấy cách thay thế nội dung tệp đính kèm.

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)

Đổi Tên Đính Kèm trong MapiMessage

Có thể sửa đổi tên hiển thị của các tệp đính kèm trong tin nhắn email được tải từ tệp. Mẫu mã dưới đây cho thấy cách thực hiện việc này:

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"