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

Email Messages Containing TNEF Attachments

Transport Neutral Encapsulation Format (TNEF) เป็นรูปแบบไฟล์แนบอีเมลของ Microsoft Outlookและ Microsoft Exchange Server. API ของ Aspose.Email ช่วยให้คุณอ่านข้อความอีเมลที่มีไฟล์แนบ TNEF และแก้ไขเนื้อหาได้ จากนั้นสามารถบันทึกอีเมลเป็นข้อความปกติหรือในรูปแบบเดียวกันโดยคงไฟล์แนบเหล่านั้น บทความนี้แสดงตัวอย่างโค้ดต่าง ๆ สำหรับทำงานกับข้อความที่มีไฟล์แนบ TNEF รวมถึงการสร้างไฟล์ EML แบบ TNEF จากไฟล์ Outlook MSG.

Identifying TNEF Attachments in an Email

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการตรวจสอบว่าอีเมลมีไฟล์แนบ TNEF หรือไม่:

from aspose.email import MailMessage

mail = MailMessage.load(data_dir + "message.eml")
is_tnef = mail.original_is_tnef

Reading Emails while Preserving TNEF Attachments

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการอ่านข้อความและคงไฟล์แนบ TNEF ไว้

from aspose.email import MailMessage, SaveOptions, EmlLoadOptions, MessageFormat, FileCompatibilityMode

options = EmlLoadOptions()
# This will Preserve the TNEF attachment as it is, file contains the TNEF attachment
options.preserve_tnef_attachments = True

eml = MailMessage.load(data_dir + "message.eml", options)
for attachment in eml.attachments:
    print(attachment.name)

Converting MSG to TNEF

Outlook MSG บางไฟล์อาจมีข้อมูลเช่นตารางและสไตล์ข้อความที่อาจเสียหายเมื่อแปลงเป็น EML การสร้างข้อความ TNEF จากไฟล์ MSG ดังกล่าวช่วยให้คุณคงฟอร์แมตและแม้กระทั่งส่งข้อความดังกล่าวผ่านไคลเอนต์อีเมลโดยคงฟอร์แมตไว้. The convert_as_tnef property ถูกใช้เพื่อบรรลุเป้าหมายนี้ โค้ดตัวอย่างต่อไปนี้แสดงวิธีการสร้างอีเมล TNEF จาก MSG.

from aspose.email.mapi import MapiMessage, MailConversionOptions, OutlookMessageFormat

mapi_msg = MapiMessage.from_file(data_dir + "message.msg")

mail_conversion_options = MailConversionOptions()
mail_conversion_options.convert_as_tnef = True

message = mapi_msg.to_mail_message(mail_conversion_options)

Creating TNEF Email

โค้ดตัวอย่างต่อไปนี้สามารถใช้สร้างอีเมลในรูปแบบ TNEF:

  1. กำหนดค่าตัวเลือกไฟล์โดยใช้ MsgLoadOptions คลาส.
  2. ตรวจสอบให้ไฟล์แนบ TNEF ในอีเมลถูกคงไว้เมื่อโหลดไฟล์โดยตั้งค่า options.preserve_tnef_attachments เป็น True.
  3. ใช้ MailMessage.load(eml_file_name, options) วิธีการโหลดไฟล์อีเมลอินพุตพร้อมประยุกต์ใช้ตัวเลือกที่ตั้งค่าไว้ก่อนหน้า ซึ่งทำให้แน่ใจว่าอีเมลที่โหลดจะคงข้อมูลที่เกี่ยวข้องกับ TNEF (เช่นไฟล์แนบหรือรูปแบบพิเศษของ Microsoft Outlook)
from aspose.email import MailMessage, SaveOptions, MsgLoadOptions, MessageFormat, FileCompatibilityMode

options = MsgLoadOptions()
# The PreserveTnefAttachments option with MessageFormat.Msg will create the TNEF eml.
options.preserve_tnef_attachments = True

eml = MailMessage.load(eml_file_name, options)

Detecting TNEF Messages

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการตรวจจับว่าข้อความเป็น TNEF หรือไม่. The original_is_tnef คุณสมบัติของ MailMessage วัตถุจะถูกตรวจสอบเพื่อกำหนดว่าต้นฉบับของอีเมลเป็น TNEF (Transport Neutral Encapsulation Format) หรือไม่ หาก is_tnef ประเมินเป็น True หมายความว่าอีเมลมีการเข้ารหัส TNEF ซึ่งมักพบในอีเมลที่มาจาก Microsoft Outlook หรือ Exchange

from aspose.email import MailMessage

mail = MailMessage.load(data_dir + "message.eml")
is_tnef = mail.original_is_tnef