ทำงานกับไฟล์แนบของข้อความ
การจัดการไฟล์แนบด้วย Aspose Outlook
การสร้างและบันทึกไฟล์ Outlook Message (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}")
บันทึกไฟล์แนบจากไฟล์ Outlook Message (MSG)
เพื่อบันทึกไฟล์แนบจากไฟล์ MSG:
- วนลูปผ่านคอลเลกชัน MapiAttachmentCollection เพื่อดึงไฟล์แนบแต่ละไฟล์.
- เพื่อบันทึกไฟล์แนบ ให้เรียกเมธอด Save() ของคลาส MapiAttachment.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการบันทึกไฟล์แนบลงดิสก์ในเครื่อง.
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 ที่ฝังอยู่ก็ปรากฏในคอลเลกชัน Attachment ของคลาส MapiMessage ตัวอย่างโค้ดต่อไปนี้ทำการพาร์สไฟล์ข้อความเพื่อหาไฟล์แนบที่ฝังอยู่และบันทึกลงดิสก์ เมธอดสถิติกลาส FromProperties() ของ MapiMessage สามารถสร้างข้อความใหม่จากไฟล์แนบที่ฝังอยู่ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการดึงไฟล์แนบเมลแบบซ้อนกัน.
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 Message (.msg):
- เรียกเมธอด RemoveAttachments() ซึ่งรับพาธของไฟล์ข้อความเป็นพารามิเตอร์ ถูกกำหนดเป็นเมธอดสาธารณะแบบสถิติ จึงไม่จำเป็นต้องสร้างอ็อบเจกต์.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการลบไฟล์แนบ.
import aspose.email as ae
ae.mapi.MapiMessage.remove_attachments("AttachmentsToRemove_out.msg")
คุณสามารถเรียกเมธอดสถาติก DestoryAttachment() ของคลาส MapiMessage ได้ มันทำงานเร็วกว่า RemoveAttachment() เพราะเมธอด 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 พร้อมทั้งสองประเภทของไฟล์แนบ.
ลองใช้ดู!
เพิ่มหรือลบไฟล์แนบอีเมลออนไลน์ด้วยบริการฟรี แอป Aspose.Email Editor.
เพิ่มไฟล์แนบอ้างอิงให้กับ 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
# ...
การแทรกและการแทนที่ไฟล์แนบ
API ของ Aspose.Email ให้ความสามารถในการแทรกไฟล์แนบในตำแหน่งเฉพาะของข้อความพาเรนต์ นอกจากนี้ยังให้ฟังก์ชันการแทนที่เนื้อหาไฟล์แนบด้วยไฟล์แนบจากข้อความอื่น โค้ดตัวอย่างต่อไปนี้แสดงการแทรกและการแทนที่ไฟล์แนบ.
แทรกในตำแหน่งที่กำหนด
API ของ Aspose.Email ให้ความสามารถในการแทรกไฟล์แนบ 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 อย่างไรก็ตาม ไม่สามารถใช้แทรกไฟล์แนบที่มี PR_ATTACH_NUM = 4 (เช่น) ในคอลเลกชันที่มี collection.Count = 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
สามารถแก้ไขชื่อการแสดงผลของไฟล์แนบในข้อความอีเมลที่โหลดจากไฟล์ได้ ตัวอย่างโค้ดด้านล่างแสดงวิธีทำเช่นนี้:
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"