Caricamento e parsing di file MSG

Questa sezione spiega come caricare e analizzare i file di messaggi Microsoft Outlook (.msg). L’API Aspose.Email fornisce un MapiMessage classe per caricare file MSG con diverse funzioni statiche di caricamento per differenti scenari. I seguenti frammenti di codice dimostrano come caricare file MSG da file o da stream.

Carica un file MSG da disco

Il seguente frammento di codice mostra come caricare file MSG da disco:

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)

Carica un file MSG da stream

Il seguente frammento di codice mostra come caricare file MSG da 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)

Conversione di EML in MSG preservando il formato EML incorporato

I file EML possono essere caricati in MapiMessage classe istanziando un MailMessage oggetto e passandolo a MapiMessage.from_mail_message metodo. Se il file EML contiene file incorporati, usa MapiConversionOptions.preserve_embedded_message_format per mantenere il formato dei file incorporati. Il frammento di codice qui sotto mostra come caricare file EML in MapiMessage preservando il formato dei file EML incorporati.

Il seguente frammento di codice dimostra come convertire un file EML in un file MSG preservando il formato del messaggio incorporato:

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)