संदेश लोड करना और सहेजना
फ़ाइल फ़ॉर्मेट की पहचान
Aspose.Email API प्रदान करता है कि प्रदान किए गए संदेश फ़ाइल के फ़ॉर्मेट को पहचानने की क्षमता। FileFormatUtil क्लास का DetectFileFormat मेथड इसका उपयोग कर सकता है। नीचे दी गई क्लासेज़ और मेथड्स का उपयोग लोड किए गए फ़ाइल फ़ॉर्मेट को पहचानने के लिए किया जा सकता है।
- एनम 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)
TNEF अटेचमेंट्स को संरक्षित रखते हुए EML सहेजना
निम्नलिखित कोड स्निपेट दिखाता है कि TNEF अटैचमेंट को संरक्षित रखते हुए EML के रूप में कैसे सहेजें।
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 कैलेंडर इवेंट्स को आउटपुट 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 क्लास MHT में निर्यात करते समय अनुकूलित टाइमज़ोन सेट करने के लिए TimeZoneOffset प्रॉपर्टी प्रदान करता है। नीचे दिया गया कोड स्निपेट दिखाता है कि अनुकूलित टाइमज़ोन के साथ ईमेल को 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)