טעינת, שמירה והמרת אימיילים

זיהוי פורמטים של קבצי אימייל

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 ניתן להשתמש ב‑… להגדרות נוספות בעת שמירת הודעות לפורמטים אחרים. המאמר מציג כיצד להשתמש במחלקות אלו לשמור אימייל לדוגמא בתור:

  • פורמט 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. תו המיקום {#} בתבנית יוחלף במספר גבול מת increment, מה שמאפשר מחרוזות גבול דינמיות וקריאות.

אם 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 - מציין שיש לכתוב טקסט מאירוע לוח שנה בתוצר ה‑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)

הכללת אירועי לוח שנה בהמרת 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 class מספקת את 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)