การโหลด การบันทึก และการแปลงอีเมล
ตรวจจับรูปแบบไฟล์อีเมล
Aspose.Email API ให้ความสามารถในการตรวจจับรูปแบบไฟล์ของไฟล์ข้อความที่ให้มา. The DetectFileFormat เมธอดของ FileFormatUtil คลาสสามารถใช้เพื่อทำเช่นนี้ได้ คลาสและเมธอดต่อไปนี้สามารถใช้เพื่อตรวจจับรูปแบบไฟล์ที่โหลด:
- Enum FileFormatType
- คลาส FileFormatInfo
- คลาส FileFormatUtil
- เมธอด detect_file_format(stream)
- เมธอด detect_file_format(file_path)
โค้ดตัวอย่างต่อไปนี้แสดงวิธีตรวจจับรูปแบบไฟล์:
from aspose.email.tools import FileFormatUtil
# Detect file format and get the detected load format
info = FileFormatUtil.detect_file_format(data_dir + "message.msg")
print("The message format is: " + str(info.file_format_type))
Loading a Message with Load Options
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความด้วยตัวเลือกการโหลด.
from aspose.email import MailMessage, EmlLoadOptions, HtmlLoadOptions, MhtmlLoadOptions, MsgLoadOptions
# Load Eml, html, mhtml, msg, and dat files
mail_message = MailMessage.load(data_dir + "message.eml", EmlLoadOptions())
MailMessage.load(data_dir + "description.html", HtmlLoadOptions())
MailMessage.load(data_dir + "message.mhtml", MhtmlLoadOptions())
MailMessage.load(data_dir + "message.msg", MsgLoadOptions())
# Loading with custom options
eml_load_options = EmlLoadOptions()
eml_load_options.preferred_text_encoding = "utf-8"
eml_load_options.preserve_tnef_attachments = True
MailMessage.load(data_dir + "description.html", eml_load_options)
html_load_options = HtmlLoadOptions()
html_load_options.preferred_text_encoding = "utf-8"
html_load_options.should_add_plain_text_view = True
html_load_options.path_to_resources = data_dir
MailMessage.load(data_dir + "description.html", html_load_options)
Preserving Embedded Message Format during Loading
from aspose.email import MailMessage, EmlLoadOptions, HtmlLoadOptions, MhtmlLoadOptions, MsgLoadOptions
from aspose.email.tools import FileFormatUtil
eml_load_options = EmlLoadOptions()
eml_load_options.preserve_embedded_message_format = True
mail = MailMessage.load(data_dir + "message.eml", eml_load_options)
file_format = FileFormatUtil.detect_file_format(mail.attachments[0].content_stream).file_format_type
print("Embedded message file format: " + str(file_format))
Saving and Converting Emails
Aspose.Email ทำให้การแปลงประเภทข้อความใดๆ ไปยังรูปแบบอื่นเป็นเรื่องง่าย. เพื่อแสดงคุณลักษณะนี้, โค้ดในบทความนี้โหลดข้อความสามประเภทจากดิสก์และบันทึกกลับเป็นรูปแบบอื่น. คลาสฐาน SaveOptions และคลาส EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions สามารถใช้สำหรับการตั้งค่าเพิ่มเติมเมื่อบันทึกข้อความเป็นรูปแบบอื่น ๆ บทความแสดงวิธีใช้คลาสเหล่านี้ในการบันทึกอีเมลตัวอย่างเป็น:
- รูปแบบ EML.
- Outlook MSG.
- รูปแบบ MHTML.
- รูปแบบ HTML.
Saving as EML
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML และบันทึกลงดิสก์ในรูปแบบเดียวกัน.
from aspose.email import MailMessage, SaveOptions
# Initialize and Load an existing EML file by specifying the MessageFormat
mail_message = MailMessage.load(data_dir + "message.eml")
mail_message.save(data_dir + "LoadAndSaveFileAsEML_out.eml", SaveOptions.default_eml)
Preserving Boundaries in EML Files
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดไฟล์ EML และบันทึกในรูปแบบเดียวกันโดยคงขอบเขตเดิมไว้.
from aspose.email import MailMessage, EmlSaveOptions, MailMessageSaveType
mail_message = MailMessage.load(data_dir + "message.eml")
# Save as eml with preserved original boundaries
eml_save_options = EmlSaveOptions(MailMessageSaveType.eml_format)
mail_message.save(data_dir + "PreserveOriginalBoundaries_out.eml", eml_save_options)
Customize MIME Boundary Strings
Aspose.Email for .NET อนุญาตให้คุณกำหนดเทมเพลตขอบเขตที่ใช้ในข้อความ MIME ซึ่งทำได้ผ่าน boundaries_template คุณสมบัติของ EmlSaveOptions คลาส.
โดยการกำหนดเทมเพลตแบบกำหนดเอง คุณสามารถกำหนดวิธีการสร้างขอบเขต MIME ตัวอักษรแทนที่ {#} ในเทมเพลตจะถูกแทนที่ด้วยหมายเลขขอบเขตที่เพิ่มขึ้น ทำให้สตริงขอบเขตเป็นแบบไดนามิกและอ่านง่าย.
If boundaries_template หากไม่ได้ตั้งค่า (ค่าเริ่มต้นคือ None) ไลบรารีจะใช้รูปแบบขอบเขตภายในของตนเอง.
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีปรับแต่งสตริงขอบเขต MIME เมื่อบันทึกข้อความอีเมลเป็นรูปแบบ .eml:
import aspose.email as ae
# Create a sample MailMessage
message = ae.MailMessage()
message.subject = "Custom MIME Boundary"
message.body = "This message uses a custom MIME boundary template."
message.to.append(ae.MailAddress("recipient@example.com"))
# Configure save options with a custom boundary template
save_options = ae.EmlSaveOptions(ae.MailMessageSaveType.EML_FORMAT)
save_options.boundaries_template = "boundary--{#}"
# Save the message to .eml with custom boundaries
message.save("Custom_Boundary_Message.eml", save_options)
นี่คือตัวอย่าง โครงสร้างข้อความ ที่มีขอบเขตกำหนดเองซึ่งบันทึกโดยใช้โค้ดข้างต้น:
From: sender@example.com
To: recipient@example.com
Subject: Custom_Boundary_Message
Date: Fri, 27 Dec 2024 12:00:00 +0000
Content-Type: multipart/mixed;
boundary="boundary--1"
This is a multi-part message in MIME format.
--boundary--1
Content-Type: multipart/alternative; boundary="boundary--2"
--boundary--2
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="us-ascii"
This is the plain text content of the email.
--boundary--2
Content-Type: text/html; charset="windows-1251"
Content-Transfer-Encoding: quoted-printable
<html>
<body>
<p>This is the <b>HTML</b> content of the email.</p>
</body>
</html>
--boundary--2--
--boundary--1
Content-Type: application/octet-stream; name="report-2023-08.xlsx"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="report-2023-08.xlsx"
UEsDBBQABgAIAAAAIQBi7...
--boundary--1--
Preserving TNEF Attachments in EML Files
โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกไฟล์ EML พร้อมคงไฟล์แนบ TNEF.
from aspose.email import MailMessage, EmlSaveOptions, MailMessageSaveType, FileCompatibilityMode
mail_message = MailMessage.load(data_dir + "message.eml")
# Save as eml with preserved attachment
eml_save_options = EmlSaveOptions(MailMessageSaveType.eml_format)
eml_save_options.file_compatibility_mode = FileCompatibilityMode.PRESERVE_TNEF_ATTACHMENTS
mail_message.save(data_dir + "PreserveTNEFAttachment_out.eml", eml_save_options)
Saving as MSG
โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML และแปลงเป็น MSG โดยใช้ตัวเลือกที่เหมาะสมจาก SaveOptions.
from aspose.email import MailMessage, SaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
# Save the Email message to disk in ASCII format and Unicode format
eml.save(data_dir + "AnEmail_out.msg", SaveOptions.default_msg_unicode)
Preserving Dates in MSG Files
นี้ MsgSaveOptions class ให้คุณบันทึกข้อความต้นฉบับเป็นไฟล์ Outlook Message (MSG) พร้อมคงวันที่ไว้ โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกไฟล์ EML เป็น MSG และคงวันที่.
from aspose.email import MailMessage, MsgSaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
# Save as msg with preserved dates
msg_save_options = MsgSaveOptions(MailMessageSaveType.outlook_message_format_unicode)
msg_save_options.preserve_original_dates = True
eml.save(data_dir + "outTest_out.msg", msg_save_options)
Saving as MHTML
ตัวเลือกต่างๆ ของ MHTML สามารถใช้เพื่อให้ได้ผลลัพธ์ที่ต้องการ. โค้ดตัวอย่างต่อไปนี้แสดงวิธีโหลดข้อความ EML เข้าไปใน MailMessage และแปลงเป็น MHTML.
from aspose.email import MailMessage, SaveOptions, MailMessageSaveType, FileCompatibilityMode
# Initialize and Load an existing EML file by specifying the MessageFormat
eml = MailMessage.load(data_dir + "message.eml")
eml.save(data_dir + "AnEmail_out.mhtml", SaveOptions.default_mhtml)
Custom MHTML Conversion Settings
นี้ MhtSaveOptions คลาสนี้ให้ตัวเลือกเพิ่มเติมสำหรับการบันทึกอีเมลเป็นรูปแบบ MHTML. ตัวนับค่า MhtFormatOptions ทำให้สามารถเขียนข้อมูลอีเมลเพิ่มเติมลงไฟล์ MHTML ได้. ฟิลด์เพิ่มเติมต่อไปนี้สามารถเขียนได้:
- NONE - ไม่ได้ระบุการตั้งค่าเฉพาะ.
- WRITE_HEADER - ระบุว่าควรเขียนข้อมูลส่วนหัว.
- WRITE_OUTLINE_ATTACHMENTS - ระบุว่าควรเขียนไฟล์แนบแบบโครงร่าง.
- WRITE_COMPLETE_EMAIL_ADDRESS - ระบุว่าที่อยู่อีเมลเต็มควรถูกเขียนในส่วนหัวของอีเมลทั้งหมด.
- NO_ENCODE_CHARACTERS - ระบุว่าจะไม่ใช้การเข้ารหัสการถ่ายโอนของอักขระ.
- HIDE_EXTRA_PRINT_HEADER - ระบุว่าหัวส่วนหน้า (PageHeader) จะไม่แสดง.
- WRITE_COMPLETE_TO_EMAIL_ADDRESS - ระบุว่าควรเขียนที่อยู่อีเมลเต็มในส่วนหัว ‘To’.
- WRITE_COMPLETE_FROM_EMAIL_ADDRESS - ระบุว่าควรเขียนที่อยู่อีเมลเต็มในส่วนหัว ‘From’.
- WRITE_COMPLETE_CC_EMAIL_ADDRESS - ระบุว่าควรเขียนที่อยู่อีเมลเต็มในส่วนหัว ‘Cc’.
- WRITE_COMPLETE_BCC_EMAIL_ADDRESS - ระบุว่าควรเขียนที่อยู่อีเมลเต็มในส่วนหัว ‘Bcc’.
- RENDER_CALENDAR_EVENT - ระบุว่าข้อความจากเหตุการณ์ปฏิทินควรถูกเขียนในไฟล์ mhtml ที่ส่งออก.
- SKIP_BYTE_ORDER_MARK_IN_BODY - ระบุว่าต้องข้าม Byte Order Mark (BOM) ในส่วนเนื้อหา.
- RENDER_V_CARD_INFO - ระบุว่าข้อความจาก VCard AlternativeView ควรถูกเขียนในไฟล์ mhtml ที่ส่งออก.
- DISPLAY_AS_OUTLOOK - ระบุว่าหัวข้อ From จะถูกแสดงเหมือนใน Outlook.
- RENDER_TASK_FIELDS - ระบุว่าฟิลด์งานเฉพาะควรถูกเขียนในไฟล์ mhtml ที่ส่งออก.
โค้ดตัวอย่างต่อไปนี้แสดงวิธีแปลงไฟล์ EML เป็น MHTML พร้อมการตั้งค่าเพิ่มเติม.
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MailMessageSaveType, FileCompatibilityMode
# Load an existing EML file
eml = MailMessage.load(data_dir + "message.eml")
# Save as mht with header
mht_save_options = MhtSaveOptions()
# Specify formatting options required
# Here we are specifying to write header information to output without writing extra print header
# and the output headers should be displayed as the original headers in the message
mht_save_options.mht_format_options = MhtFormatOptions.WRITE_HEADER | MhtFormatOptions.HIDE_EXTRA_PRINT_HEADER | MhtFormatOptions.DISPLAY_AS_OUTLOOK
# Check the body encoding for validity.
mht_save_options.check_body_content_encoding = True
eml.save(data_dir + "outMessage_out.mht", mht_save_options)
Including Calendar Events in MHTML Conversion
นี้ MhtFormatOptions.RenderCalendarEvent ทำให้เหตุการณ์ปฏิทินแสดงผลใน MHTML ที่ส่งออก โค้ดตัวอย่างต่อไปนี้แสดงวิธีเรนเดอร์เหตุการณ์ปฏิทินขณะแปลงเป็น MHTML:
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
file_name = "message.msg"
# Load the MSG file
msg = MailMessage.load(data_dir + file_name)
# Create MHT save options
options = MhtSaveOptions()
options.mht_format_options = MhtFormatOptions.WRITE_HEADER | MhtFormatOptions.RENDER_CALENDAR_EVENT
# Save the message as MHTML
msg.save(data_dir + "Meeting with Recurring Occurrences.mhtml", options)
Exporting Email to MHT without Inline Images
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
# Load the EML file
eml = MailMessage.load(data_dir + "message.eml")
# Create MHT save options
mht_save_options = MhtSaveOptions()
mht_save_options.skip_inline_images = True
# Save the message as MHTML without inline images
eml.save(data_dir + "EmlToMhtmlWithoutInlineImages_out.mht", mht_save_options)
Exporting Email to MHT with Custom Time Zone
MailMessage class ให้ TimeZoneOffset property เพื่อกำหนดโซนเวลาแบบกำหนดเองขณะส่งออกเป็น MHT โค้ดตัวอย่างต่อไปนี้แสดงวิธีส่งออกอีเมลเป็น MHT พร้อมโซนเวลาแบบกำหนดเอง:
from aspose.email import MailMessage, MhtSaveOptions, MhtFormatOptions, MhtTemplateName, MailMessageSaveType, FileCompatibilityMode
from datetime import timedelta
# Load the EML file
eml = MailMessage.load(data_dir + "message.eml")
# Set the local time for message date
eml.time_zone_offset = timedelta(hours=-8)
# The dates will be rendered by the local system time zone
mht_save_options = MhtSaveOptions()
mht_save_options.mht_format_options = MhtFormatOptions.WRITE_HEADER
eml.save(data_dir + "ExportEmailToMHTWithCustomTimezone_out.mhtml", mht_save_options)
Saving as HTML
นี้ HtmlSaveOptions class ให้คุณส่งออกเนื้อหาข้อความเป็น HTML พร้อมตัวเลือกในการบันทึกทรัพยากรที่ฝังอยู่ โค้ดตัวอย่างต่อไปนี้แสดงวิธีบันทึกข้อความเป็น HTML โดยค่าเริ่มต้นของ embed_resources คือ true.
from aspose.email import MailMessage, SaveOptions, HtmlSaveOptions, HtmlFormatOptions
# Load the EML file
message = MailMessage.load(data_dir + "message.eml")
# Save the Email message as HTML
message.save(data_dir + "SaveAsHTML_out.html", SaveOptions.default_html)
# OR
eml = MailMessage.load(data_dir + "message.eml")
options = HtmlSaveOptions()
options.embed_resources = False
options.html_format_options = (
HtmlFormatOptions.WRITE_HEADER
| HtmlFormatOptions.WRITE_COMPLETE_EMAIL_ADDRESS
) # save the message headers to output HTML using the formatting options
eml.save(data_dir + "SaveAsHTML1_out.html", options)
Saving as Outlook Template (.OFT)
โค้ดตัวอย่างต่อไปนี้แสดงวิธีการบันทึกข้อความเป็นไฟล์เทมเพลต Outlook (.oft).
from aspose.email import MailMessage, SaveOptions
eml = MailMessage("test@from.to", "test@to.to", "template subject", "Template body")
oft_eml_file_name = "EmlAsOft_out.oft"
options = SaveOptions.default_msg_unicode
options.save_as_template = True
eml.save(data_dir + oft_eml_file_name, options)