تحميل وحفظ الرسالة

اكتشاف صيغ الملفات

توفر Aspose.Email API القدرة على اكتشاف صيغة ملف الرسالة المقدم. يمكن استخدام طريقة DetectFileFormat في فئة FileFormatUtil لتحقيق ذلك. يمكن استخدام الفئات والطرق التالية لاكتشاف صيغة الملف المحمل.

المقتطف البرمجي التالي يوضح كيفية اكتشاف صيغ الملفات.

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 لإعدادات إضافية عند الحفظ MailMessage يمكن استخدامها لحفظ الرسائل بصيغ أخرى. يوضح المقال كيفية استخدام هذه الفئات لحفظ بريد إلكتروني نموذجي كـ

  • صيغة EML.
  • ملف Outlook MSG.
  • صيغة MHTML.
  • صيغة HTML.

تحميل EML وحفظه كـ 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 مع الحفاظ على الحدود الأصلية

المقتطف البرمجي التالي يوضح كيفية تحميل 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)

حفظ كـ EML مع الحفاظ على مرفقات TNEF

المقتطف البرمجي التالي يوضح كيفية حفظ كـ 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)

تحميل EML، حفظ إلى 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 تتيح الفئة حفظ الرسالة المصدر كملف رسالة Outlook (MSG) مع الحفاظ على التواريخ. يوضح مقطع الشيفرة التالي كيفية حفظ كـ 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)

حفظ MailMessage كـ 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 - يشير إلى أنه يجب كتابة المرفقات المخططية.
  • 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 - يشير إلى أنه يجب كتابة بايتات علامة ترتيب البايت (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)

عرض أحداث التقويم أثناء التحويل إلى MHTML

الـ MhtFormatOptions.RenderCalendarEvent يعرض أحداث التقويم إلى مخرجات MTHML. يوضح المقتطف البرمجي التالي كيفية عرض أحداث التقويم أثناء التحويل إلى 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)

تصدير البريد إلى MHTML دون الصور المدمجة

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)

تصدير البريد الإلكتروني إلى EML

المقتطع البرمجي التالي يوضح كيفية تصدير البريد الإلكتروني إلى ملف eml.

from aspose.email import MailMessage, SaveOptions

# Load the EML file
msg = MailMessage.load(data_dir + "message.eml")

# Save the Email message as EML
msg.save(data_dir + "ExporttoEml_out.eml", SaveOptions.default_eml)

حفظ الرسالة كملف HTML

الـ HtmlSaveOptions تتيح الفئة تصدير محتوى الرسالة إلى 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)