การจัดการไฟล์แนบในข้อความ Outlook

นี้ การสร้างและบันทึกไฟล์ข้อความ Outlook (MSG) บทความอธิบายขั้นตอนในการสร้างและบันทึกข้อความ รวมถึงไฟล์ MSG ที่มีไฟล์แนบ บทความนี้เจาะลึกการจัดการไฟล์แนบ Microsoft Outlook ด้วย Aspose.Email จะนำคุณผ่านการเข้าถึงและบันทึกไฟล์แนบจากไฟล์ข้อความโดยใช้ attachments คุณสมบัติของ MapiMessage คลาส คุณสมบัตินี้เป็นคอลเลกชันที่เป็นของ MapiAttachmentCollection คลาส ที่ช่วยให้การจัดการไฟล์แนบมีประสิทธิภาพ

ตรวจสอบไฟล์แนบ Inline กับ Regular

"Inline" และ "Regular" ไฟล์แนบหมายถึงวิธีการรวมเข้ากับข้อความอีเมล Regular ไฟล์แนบเป็นไฟล์ที่แนบแบบดั้งเดิม ซึ่งมักจะแสดงในรายการภายในโปรแกรมอีเมลและผู้รับสามารถดาวน์โหลดและบันทึกลงที่เก็บข้อมูลภายใน Inline ไฟล์แนบ หรือที่เรียกว่าภาพฝังหรือตรงในเนื้อหา มักใช้เพื่อใส่รูปภาพหรือสื่ออื่นๆ ภายในส่วนเนื้อหาของอีเมล ไม่แสดงเป็นรายการแยก แต่จะแสดงโดยตรงในเนื้อหาอีเมล เช่น ในส่วนเนื้อหาของอีเมล ซึ่งช่วยให้คุณสามารถใส่รูปภาพหรือสื่ออื่นๆ ที่เป็นส่วนหนึ่งของเนื้อความได้ ตัวอย่างโค้ดด้านล่างแสดงวิธีการกำหนดว่าไฟล์แนบเป็นแบบ 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

API ของ Aspose.Email รวมถึง 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:

  1. วนซ้ำผ่าน MapiAttachmentCollection คอลเลกชันและดึงไฟล์แนบแต่ละรายการ
  2. เพื่อบันทึกไฟล์แนบ ให้เรียก 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 ซึ่งมักใช้ในข้อความ Outlook (winmail.dat)

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการสกัด บันทึก และเพิ่มไฟล์แนบอีเมลรูปแบบ 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 (Object Linking and Embedding) ที่ฝังอยู่ก็ปรากฏในคอลเลกชันไฟล์แนบของ MapiMessage คลาส ตัวอย่างโค้ดด้านล่างนี้แสดงวิธีการวิเคราะห์ไฟล์ข้อความและดึงไฟล์แนบข้อความที่ฝังอยู่ จากนั้นบันทึกลงดิสก์สำหรับการใช้งานต่อไป MapiMessage คลาสให้ from_properties เมธอด static เพื่อสร้างข้อความใหม่จากไฟล์แนบที่ฝังอยู่ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการรับไฟล์แนบข้อความอีเมลที่ซ้อนกัน

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 Message (.msg) ใช้ remove_attachments(path) เมธอดนี้ต้องการพาธของไฟล์ข้อความเป็นพารามิเตอร์ เป็นเมธอด static สาธารณะ คุณสามารถเรียกได้โดยตรงโดยไม่ต้องสร้างอ็อบเจกต์ ทำให้กระบวนการง่ายและมีประสิทธิภาพ

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการลบไฟล์แนบจากไฟล์ MSG ด้วย Aspose.Email:

import aspose.email as ae

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

คุณยังสามารถเรียกเมธอด static 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 ให้สมาชิกที่มีการ overload ของ เพิ่ม วิธีการสร้างข้อความ Outlook ที่มีไฟล์แนบทั้งสองประเภท

เพิ่มไฟล์แนบอ้างอิงในข้อความ MAPI

Reference attachment โดยทั่วไปหมายถึงไฟล์แนบที่มีการอ้างอิงหรือเชื่อมโยงไปยังทรัพยากรภายนอกแทนที่จะเป็นไฟล์จริง ตัวอ้างอิงเหล่านี้มักใช้ในอีเมล 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 ช่วยให้คุณสามารถแทรกไฟล์แนบ MSG ลงใน MSG พ่อแม่โดยใช้เมธอด Insert ของ 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 method to update the contents of an embedded attachment with new data. However, note that this method cannot be used to insert an attachment with PR_ATTACH_NUM = 4 into a collection that holds only 2 attachments. Below is a code snippet demonstrating how to replace attachment contents:

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)

เปลี่ยนชื่อไฟล์แนบใน MapiMessages

ด้วย 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"