بارگذاری، ذخیرهسازی و تبدیل ایمیلها
تشخیص قالبهای فایل ایمیل
API Aspose.Email قابلیت شناسایی قالب فایل پیام ارائهشده را فراهم میکند. این 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))
بارگذاری پیام با گزینههای بارگذاری
قطعه کد زیر نشان میدهد چگونه یک پیام را با گزینههای بارگذاری بارگذاری کنید.
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)
حفظ قالب پیام توکار هنگام بارگذاری
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))
ذخیره و تبدیل ایمیلها
Aspose.Email تبدیل هر نوع پیام به قالب دیگری را آسان میکند. برای نشان دادن این ویژگی، کد این مقاله سه نوع پیام را از دیسک بارگذاری کرده و دوباره در قالبهای دیگر ذخیره میکند. کلاس پایه SaveOptions و کلاسها EmlSaveOptions, MsgSaveOptions, MhtSaveOptions, HtmlSaveOptions میتواند برای تنظیمات اضافی هنگام ذخیره پیامها به فرمتهای دیگر استفاده شود. مقاله نشان میدهد چگونه از این کلاسها برای ذخیره یک ایمیل نمونه به عنوان:
- قالب EML.
- Outlook MSG.
- قالب MHTML.
- قالب HTML.
ذخیره بهعنوان 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)
حفظ مرزها در فایلهای EML
قطعه کد زیر نشان میدهد چگونه یک فایل 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)
سفارشیسازی رشتههای مرز MIME
Aspose.Email برای .NET به شما اجازه میدهد قالب مرز استفادهشده در پیامهای MIME را پیکربندی کنید. این کار از طریق boundaries_template ویژگی EmlSaveOptions کلاس.
با تنظیم یک قالب سفارشی، میتوانید نحوه تولید مرزهای MIME را تعریف کنید. کاراکتر عام {#} در قالب با یک عدد مرز افزایشی جایگزین میشود که امکان ایجاد رشتههای مرز پویا و قابل خواندن را فراهم میکند.
اگر 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--
حفظ پیوستهای TNEF در فایلهای EML
قطعه کد زیر نشان میدهد چگونه یک فایل 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)
ذخیره بهعنوان 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)
حفظ تاریخها در فایلهای MSG
این MsgSaveOptions class به شما اجازه میدهد پیام منبع را بهعنوان فایل پیام Outlook (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)
ذخیره بهعنوان 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)
تنظیمات تبدیل سفارشی MHTML
این MhtSaveOptions کلاسی که گزینههای اضافی برای ذخیره پیامهای ایمیل به قالب MHTML فراهم میکند. شمارنده MhtFormatOptions امکان نوشتن اطلاعات ایمیل اضافی به MHTML خروجی را فراهم میکند. فیلدهای اضافی زیر میتوانند نوشته شوند:
- NONE - هیچ تنظیم خاصی مشخص نشده است.
- WRITE_HEADER - نشان میدهد که اطلاعات هدر باید نوشته شود.
- WRITE_OUTLINE_ATTACHMENTS - نشان میدهد که پیوستهای outline باید نوشته شوند.
- 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 - نشان میدهد که متن از رویداد تقویم باید در خروجی مhtml نوشته شود.
- 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 - نشان میدهد که فیلدهای خاص Task باید در خروجی 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)
شامل کردن رویدادهای تقویم در تبدیل به MHTML
این 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)
صادر کردن ایمیل به MHT بدون تصاویر درونخط
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)
صادر کردن ایمیل به MHT با منطقه زمانی سفارشی
MailMessage کلاس فراهم میکند TimeZoneOffset ویژگی برای تنظیم منطقه زمانی سفارشی هنگام صادر کردن به 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)
ذخیره بهعنوان 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)
ذخیره بهعنوان قالب Outlook (.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)