MSG फ़ाइल को लोड करना, देखना और पार्स करना
यह विषय बताता है कि Microsoft Outlook संदेश फ़ाइल (*.msg) को कैसे लोड किया जाए। MapiMessage क्लास MSG फ़ाइलों को लोड करने के लिए उपयोग की जाती है, और विभिन्न परिदृश्यों के लिए कई स्थैतिक लोडिंग फ़ंक्शन प्रदान करती है। निम्नलिखित कोड स्निपेट दिखाता है कि MSG फ़ाइलों को फ़ाइल या स्ट्रीम से कैसे लोड किया जाए।
Try it out!
नि:शुल्क के साथ ऑनलाइन ईमेल फ़ाइलें पार्स करें Aspose.Email पार्सर ऐप.
MSG फ़ाइलें लोड करना
निम्नलिखित कोड स्निपेट दिखाता है कि MSG फ़ाइलों को कैसे लोड किया जाए।
from aspose.email.mapi import MapiMessage
# Create an instance of MapiMessage from file
msg = MapiMessage.from_file("message.msg")
# Get subject
print("Subject: " + msg.subject)
# Get from address
print("From: " + msg.sender_email_address)
# Get body
print("Body: " + msg.body)
# Get recipients information
recipients = ", ".join([r.email_address for r in msg.recipients])
print("Recipients: " + recipients)
# Get attachments
for att in msg.attachments:
print(att.file_name)
print(att.display_name)
स्ट्रीम से लोड करना
निम्नलिखित कोड स्निपेट दिखाता है कि स्ट्रीम से फ़ाइल कैसे लोड करें।
from aspose.email.mapi import MapiMessage
import io
# Read the file into a byte array
file_path = dir_path + "message.msg"
with open(file_path, "rb") as file:
bytes_data = file.read()
# Create a memory stream from the byte array
stream = io.BytesIO(bytes_data)
stream.seek(0)
# Create an instance of MapiMessage from the stream
msg = MapiMessage.from_stream(stream)
# Get subject
print("Subject: " + msg.subject)
# Get from address
print("From: " + msg.sender_email_address)
# Get body
print("Body: " + msg.body)
EML को MSG में बदलना और एम्बेडेड EML फ़ॉर्मेट बरकरार रखना
EML फ़ाइलें लोड की जा सकती हैं MapiMessage क्लास को एक … इंस्टैंसिएट करके MailMessage ऑब्जेक्ट को बनाकर उसे MapiMessage.from_mail_message मेथड में पास करें। यदि EML फ़ाइल में एम्बेडेड EML फ़ाइलें हैं, तो एम्बेडेड EML फ़ाइलों के फ़ॉर्मेट को बरकरार रखने के लिए MapiConversionOptions.PreserveEmbeddedMessageFormat का उपयोग करें। नीचे दिया गया कोड स्निपेट दिखाता है कि एम्बेडेड EML फ़ॉर्मेट को बनाए रखते हुए EML फ़ाइलों को MapiMessage में कैसे लोड किया जाए।
from aspose.email import MailMessage, EmlLoadOptions
from aspose.email.mapi import MapiMessage, MapiConversionOptions, OutlookMessageFormat
eml_file = dir_path + "message.eml"
# Load the EML file
eml_options = EmlLoadOptions()
eml = MailMessage.load(eml_file, eml_options)
# Create MapiConversionOptions
conversion_options = MapiConversionOptions()
conversion_options.format = OutlookMessageFormat.UNICODE
# Preserve Embedded Message Format
conversion_options.preserve_embedded_message_format = True
# Convert EML to MSG with options
msg = MapiMessage.from_mail_message(eml, conversion_options)