Lavorare con le proprietà MAPI
Accesso e impostazione della proprietà Outlook MAPI
La classe MapiProperty rappresenta una proprietà MAPI, che contiene:
- Nome: una stringa che rappresenta il nome della proprietà.
- Tag: un long che rappresenta il tag della proprietà.
- Dati: un array di byte che rappresenta i dati della proprietà.
Ottenere la proprietà MAPI usando il tag della proprietà MAPI
Per ottenere le proprietà MAPI:
- Crea un’istanza di MapiMessage caricandola da file o stream.
- Ottieni la MapiProperty da MapiMessage.Properties usando le chiavi MapiPropertyTag.
- Ottieni i dati della MapiProperty con il metodo GetX.
Il frammento di codice seguente mostra come ottenere la proprietà MAPI usando il tag della proprietà MAPI.
Impostazione delle proprietà MAPI
Il seguente frammento di codice mostra come impostare le proprietà MAPI.
dove la definizione del metodo convertDateTime è la seguente:
Lettura delle proprietà MAPI nominate da file MSG di Outlook
Aspose.Email fornisce un insieme di API per lavorare con i file MSG, inclusa l’estrazione delle proprietà MAPI nominate.
Leggere le proprietà MAPI nominate dal file MSG
Per leggere le proprietà MAPI nominate, possiamo usare la proprietà named_properties di MapiMessage classe. Il seguente esempio di codice mostra come caricare un messaggio, recuperare tutte le proprietà MAPI nominate, iterare su ogni proprietà per verificarne il valore:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Get all named MAPI properties
properties = msg.named_properties.values
# Read all properties in foreach loop
for prop in properties:
# Read any specific property
if prop.descriptor.canonical_name == "PidLidSideEffects":
print(f"{prop.descriptor.canonical_name} = {prop}")
if prop.descriptor.canonical_name == "PidLidInternetAccountName":
print(f"{prop.descriptor.canonical_name} = {prop}")
Lettura della proprietà MAPI nominata da un allegato
Aspose.Email rende possibile recuperare tutte le proprietà MAPI nominate di un allegato. Il seguente esempio di codice mostra come leggere le proprietà MAPI nominate dagli allegati:
import aspose.email as ae
msg = ae.mapi.MapiMessage.load("my.msg")
# Get all named MAPI properties
attach_properties = msg.attachments[0].named_properties.values
# Read all properties in foreach loop
for prop in attach_properties:
# Read any specific property
if prop.descriptor.name == "AttachmentOriginalUrl":
print(f"{prop.descriptor.name} = {prop.get_string()}")
if prop.descriptor.name == "AttachmentWasSavedToCloud":
print(f"{prop.descriptor.name} = {prop.get_boolean()}")
Rimuovere le proprietà da MSG e allegati
Questo esempio di codice dimostra la creazione di un messaggio Outlook MSG con contenuto nel corpo e un file di messaggio allegato. Mostra anche come eliminare una proprietà di allegato dal messaggio creato.
import aspose.email as ae
# create an MSG
msg = ae.mapi.MapiMessage("from@doamin.com", "to@domain.com", "subject", "body");
msg.set_body_content("<html><body><h1>This is the body content</h1></body></html>", ae.mapi.BodyContentType.HTML)
# load message and add it to created MSG as attachment
attachment = ae.mapi.MapiMessage.load(attach.msg")
msg.attachments.add("Outlook2 Test subject.msg", attachment)
# count of attachment properties before removal
print(f"Before removal = {msg.attachments[0].properties.count}")
# Delete anyone attachment property
msg.attachments[0].remove_property(923467779)
# count of attachment properties after removal
print(f"Before removal = {msg.attachments[0].properties.count}")