טעינה ופענוח קבצי MSG

סעיף זה מסביר כיצד לטעון ולפענח קבצי הודעת Microsoft Outlook (.msg). ה‑API של Aspose.Email מספקת MapiMessage מחלקה לטעינת קבצי MSG עם מספר פונקציות טעינה סטטיות למקרים שונים. קטעי הקוד הבאים מדגימים כיצד לטעון קבצי MSG מקובץ או מ‑stream.

טען קובץ MSG מהדיסק

קטע הקוד הבא מראה כיצד לטעון קבצי MSG מהדיסק:

from aspose.email.mapi import MapiMessage

# Create an instance of MapiMessage from file
msg = MapiMessage.load("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)

טען קובץ MSG מ‑Stream

קטע הקוד הבא מראה כיצד לטעון קבצי MSG מ‑stream:

from aspose.email.mapi import MapiMessage
import io

# Read the file into a byte array
file_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.load(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 מכיל קבצים מוטבעים, השתמש ב‑ MapiConversionOptions.preserve_embedded_message_format כדי לשמור על הפורמט של קבצים מוטבעים. קטע הקוד שלהלן מראה כיצד לטעון קובצי EML ל‑MapiMessage תוך שמירה על פורמט קבצי ה‑EML המוטבעים.

קטע הקוד הבא מדגים כיצד להמיר קובץ EML לקובץ MSG תוך שמירה על פורמט ההודעה המוטבעת:

from aspose.email import MailMessage, EmlLoadOptions
from aspose.email.mapi import MapiMessage, MapiConversionOptions, OutlookMessageFormat

eml_file = "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)