ईमेल लोड करना, सहेजना और रूपांतरित करना
ईमेल फ़ाइल फ़ॉर्मेट का पता लगाना
Aspose.Email API प्रदान करती है क्षमता कि प्रदत्त संदेश फ़ाइल का फ़ाइल फ़ॉर्मेट पता लगाया जा सके। DetectFileFormat की विधि FileFormatUtil इसको प्राप्त करने के लिए क्लास का उपयोग किया जा सकता है। निम्नलिखित क्लास और मेथड का उपयोग लोड किए गए फ़ाइल फ़ॉर्मेट का पता लगाने के लिए किया जा सकता है:
- एनम 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 for .NET आपको MIME संदेशों में उपयोग होने वाले बाउंड्री टेम्प्लेट को कॉन्फ़िगर करने की अनुमति देता है। यह नीचे दिए गए माध्यम से संभव है: boundaries_template की प्रॉपर्टी EmlSaveOptions क्लास।
कस्टम टेम्प्लेट सेट करके आप निर्धारित कर सकते हैं कि MIME बाउंड्री कैसे जनरेट हों। टेम्प्लेट में {#} वैल्डकार्ड को इनक्रिमेंटल बाउंड्री नंबर से प्रतिस्थापित किया जाएगा, जिससे डायनेमिक और पठनीय बाउंड्री स्ट्रिंग्स बनेंगी।
यदि boundaries_template सेट नहीं है (डिफ़ॉल्ट None है), लाइब्रेरी अपना आंतरिक बाउंड्री फ़ॉर्मेट उपयोग करती है।
निम्नलिखित कोड उदाहरण दिखाता है कि .eml फ़ॉर्मेट में ईमेल संदेश को सहेजते समय MIME बाउंड्री स्ट्रिंग्स को कैसे कस्टमाइज़ किया जाए:
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--
EML फ़ाइलों में TNEF अटैचमेंट को संरक्षित रखना
निम्नलिखित कोड स्निपेट दिखाता है कि 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)
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 Message फ़ाइल (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)