טעינת ושמירת הודעה
אבחון פורמטים של קבצים
ממשק Aspose.Email API מספק את היכולת לאבחן את פורמט הקובץ של הודעה מסוימת. ניתן להשתמש במתודה 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 להגדרות נוספות בעת שמירה 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 - מציין שיש לכתוב קבצים מצורפים מבניים (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 מציג את אירועי היומן ל‑output 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)
ייצוא מייל ל-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)