Caricamento, Visualizzazione e Analisi di file MSG
Questo argomento spiega come caricare un file di messaggio Microsoft Outlook (*.msg). Il MapiMessage classe è usata per caricare file MSG e fornisce diverse funzioni di caricamento statiche per differenti scenari. Il frammento di codice seguente mostra come caricare file MSG da un file o da uno stream.
Caricamento di File MSG
Il frammento di codice seguente mostra come caricare file MSG.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// The path to the File directory.
String dataDir = RunExamples.getDataDir_Outlook();
// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.fromFile(dataDir + "message.msg");
// Get subject
System.out.println("Subject:" + msg.getSubject());
// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());
// Get body
System.out.println("Body" + msg.getBody());
// Get recipients information
System.out.println("Recipient: " + msg.getRecipients());
// Get attachments
for (MapiAttachment att : msg.getAttachments())
{
System.out.println("Attachment Name: " + att.getFileName());
System.out.println("Attachment Display Name: " + att.getDisplayName());
}
Il seguente esempio di codice mostra come usare MailMessage per caricare un messaggio in formato MSG.
MailMessage eml = MailMessage.load("message.msg");
Va notato che il messaggio risultante viene convertito in formato EML, inclusi gli allegati di messaggi incorporati. Non utilizzare questo metodo di caricamento se desideri preservare alcune proprietà specifiche del formato msg del messaggio originale.
Per preservare il formato originale degli allegati di messaggi incorporati, usa il MsgLoadOptions.PreserveEmbeddedMessageFormat proprietà.
MsgLoadOptions msgLoadOptions = new MsgLoadOptions();
msgLoadOptions.setPreserveEmbeddedMessageFormat(true);
MailMessage msg = MailMessage.load(stream, msgLoadOptions);
Caricamento da Stream
Il frammento di codice seguente mostra come caricare un file da uno stream.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Create an instance of MapiMessage from file
try (FileInputStream stream = new FileInputStream(dataDir + "message.msg"))
{
// Create an instance of MapiMessage from file
MapiMessage msg = MapiMessage.fromStream(stream);
// Get subject
System.out.println("Subject:" + msg.getSubject());
// Get from address
System.out.println("From:" + msg.getSenderEmailAddress());
// Get body
System.out.println("Body" + msg.getBody());
}
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.fromMailMessage metodo. Se il file EML contiene file EML incorporati, usa MapiConversionOptions.setPreserveEmbeddedMessageFormat per mantenere il formato dei file EML incorporati. Il frammento di codice seguente mostra come caricare file EML in MapiMessage preservando il formato dei file EML incorporati.
Provalo!
Converti email e archivi di messaggi online con il gratuito Applicazione di Conversione Aspose.Email.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
String dataDir = RunExamples.getDataDir_Email();
MailMessage eml = MailMessage.load(dataDir + "sample.eml", new EmlLoadOptions());
MapiConversionOptions options = MapiConversionOptions.getUnicodeFormat();
//Preserve Embedded Message Format
options.setPreserveEmbeddedMessageFormat(true);
//Convert EML to MSG with Options
MapiMessage msg = MapiMessage.fromMailMessage(eml, options);