การจัดการไฟล์แนบในข้อความ 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:
- วนซ้ำผ่าน MapiAttachmentCollection คอลเลกชันและดึงไฟล์แนบแต่ละรายการ
- เพื่อบันทึกไฟล์แนบ ให้เรียก MapiAttachment คลาส Save() เมธอด.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการบันทึกไฟล์แนบลงดิสก์ในเครื่อง:
import aspose.email as ae
# Create an instance of MapiMessage from file
message = ae.mapi.MapiMessage.load(data_dir + "message.msg")
# Iterate through the attachments collection
for attachment in message.attachments:
# Save the individual attachment
attachment.save(data_dir + attachment.long_file_name)
การจัดการไฟล์แนบ TNEF
โหลดและบันทึกไฟล์แนบในรูปแบบ TNEF
Aspose.Email มีเมธอดสำหรับจัดการไฟล์แนบ 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 (Object Linking and Embedding) ที่ฝังอยู่ก็ปรากฏในคอลเลกชันไฟล์แนบของ MapiMessage คลาส ตัวอย่างโค้ดด้านล่างนี้แสดงวิธีการวิเคราะห์ไฟล์ข้อความและดึงไฟล์แนบข้อความที่ฝังอยู่ จากนั้นบันทึกลงดิสก์สำหรับการใช้งานต่อไป MapiMessage คลาสให้ from_properties เมธอด static เพื่อสร้างข้อความใหม่จากไฟล์แนบที่ฝังอยู่ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการรับไฟล์แนบข้อความอีเมลที่ซ้อนกัน
import aspose.email as ae
eml = ae.mapi.MapiMessage.load("my.msg")
for index, attachment in enumerate(eml.attachments):
# Only the embedded messages carry OLE object data
if attachment.object_data is None:
continue
# Create a MapiMessage object from the individual attachment
get_attachment = ae.mapi.MapiMessage.from_properties(attachment.object_data.properties)
# Convert the embedded message to a MailMessage and save it to disk
mail_message = get_attachment.to_mail_message(ae.mapi.MailConversionOptions())
mail_message.save(f"NestedMailMessageAttachments_{index}_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 ที่มีไฟล์แนบทั้งสองประเภท
ลองใช้ดู!
เพิ่มหรือลบไฟล์แนบอีเมลออนไลน์ด้วยบริการฟรี แอป Aspose.Email Editor.
เข้าถึงไฟล์แนบที่เพิ่งเพิ่ม
นี้ เพิ่ม เมธอดของ MapiAttachmentCollection คลาสคืนค่า MapiAttachment อ็อบเจ็กต์ที่สร้างขึ้น. สิ่งนี้ทำให้คุณเข้าถึงไฟล์แนบใหม่ได้ทันที, ดังนั้นคุณสามารถกำหนดค่าได้หลังการแทรกโดยไม่ต้องค้นหาจากคอลเลกชัน. ไฟล์แนบที่สร้างจะถูกส่งกลับไม่ว่าจะเป็นไฟล์แนบปกติจากไบต์, ข้อความ Outlook เป็นไฟล์แนบฝัง, หรือไฟล์แนบอ้างอิง.
โค้ดตัวอย่างต่อไปนี้เพิ่มไฟล์แนบปกติและข้อความฝัง และเปลี่ยนชื่อที่แสดงของพวกมัน:
import aspose.email as ae
message = ae.mapi.MapiMessage("from@domain.com", "to@domain.com", "Weekly report", "Please find the files attached.")
# Add a regular attachment and change its display name
with open("invoice.pdf", "rb") as f:
attachment = message.attachments.add("invoice.pdf", f.read())
attachment.display_name = "Invoice #2026-001.pdf"
# Add an embedded message and change its display name
embedded = message.attachments.add("original.msg", ae.mapi.MapiMessage.load("message.msg"))
embedded.display_name = "Original request"
message.save("report.msg")
เพิ่มไฟล์แนบอ้างอิงในข้อความ 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 ช่วยให้คุณแทรกไฟล์แนบ MSG เข้าไปใน MSG พ่อโดยใช้ insert() เมธอดของ MapiAttachmentCollection คลาส, ซึ่งรับดัชนีเพื่อแทรก, ชื่อไฟล์แนบและ MapiMessage เพื่อแนบ. ด้านล่างคือตัวอย่างโค้ดที่แสดงวิธีแทรกไฟล์แนบในตำแหน่งที่กำหนด:
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"
สกัดไฟล์แนบจากไฟล์ MSG ที่จัดรูปแบบเป็น RTF
สำหรับข้อความที่ฟอร์แมตเป็น RTF โค้ดด้านล่างจะแยกแยะและดึงไฟล์แนบที่เป็นอินไลน์หรือเป็นไอคอนในส่วนเนื้อหา ข้อมูลเก็บไว้ในข้อมูลอ็อบเจกต์ OLE ของไฟล์แนบ ซึ่งสามารถเข้าถึงได้ผ่าน object_data คุณสมบัติของ MapiAttachment:
- the
\x03ObjInfoคุณสมบัติบอกว่ามีการแสดงอ็อบเจกต์แบบในบรรทัดหรือเป็นไอคอน; - the
Packageคุณสมบัติถือเนื้อหาดิบของไฟล์ที่ฝังไว้
from aspose.email.mapi import MapiMessage
def is_attachment_inline(attachment):
if attachment.object_data is None:
return False
# `values` is a property of MapiPropertyCollection, not a method
for prop in attachment.object_data.properties.values:
if prop.name == "\x03ObjInfo":
odt_persist1 = int.from_bytes(prop.data[:2], "little")
return (odt_persist1 & (1 << 6)) == 0
return False
def save_attachment(attachment, file_name):
for prop in attachment.object_data.properties.values:
if prop.name == "Package":
with open(file_name, "wb") as f:
f.write(prop.data)
msg = MapiMessage.load(data_dir + "MSG file with RTF Formatting.msg")
for index, attachment in enumerate(msg.attachments):
if is_attachment_inline(attachment):
save_attachment(attachment, data_dir + f"attachment_{index}")
ดึงอ็อบเจ็กต์ OLE ฝังจาก oledata.mso
บางครั้งข้อมูล OLE ฝังจะแสดงเป็น oledata.mso ไฟล์แนบใน MapiAttachment และต้องแยกออกด้วยตนเอง oledata.mso ไฟล์อยู่ในรูปแบบ Microsoft Compound Document File (MCDF) และการสนับสนุนไฟล์ดังกล่าวอยู่นอกขอบเขตของ Aspose.Email อย่างไรก็ตาม Aspose.Email สามารถใช้ร่วมกับไลบรารีไฟล์คอมพาวด์เพื่ออ่านเนื้อหาและบันทึกลงดิสก์ได้
ไบต์ดิบของแพคเกจสามารถเข้าถึงได้ผ่าน binary_data คุณสมบัติของ MapiAttachment อ็อบเจ็กต์. เขียนลงดิสก์หรือส่งต่อให้ไลบรารีการอ่านไฟล์คอมพาวด์เช่น olefile, เพื่อเข้าถึงออบเจ็กต์ที่ฝังไว้.
เมื่อประเภทเนื้อความข้อความเป็น HTML (ไม่ใช่ RTF) และมีอ็อบเจ็กต์ OLE ในข้อความ, ส่วน PR_ATTACH_DATA_OBJ คุณสมบัติหายไป ในกรณีนี้ข้อมูลเกี่ยวกับอ็อบเจกต์ OLE จะถูกเก็บใน oledata.mso.
from aspose.email.mapi import MapiMessage
msg = MapiMessage.load(data_dir + "double.msg")
for mapi_attachment in msg.attachments:
if mapi_attachment.long_file_name == "oledata.mso":
# The package is a compound file: save it and read its streams with a
# library such as olefile
with open(data_dir + "oledata.mso", "wb") as f:
f.write(mapi_attachment.binary_data)