Save Messages from Exchange Server Mailbox using WebDav
This article shows how to get messages from an Exchange Server mailbox and save them to disk in EML and MSG formats.
Save Messages from an Exchange Server Mailbox to EML
To get messages and save in EML format:
- Create an instance of the ExchangeClient class.
- Provide the server name, username, password, and domain.
- Call the ExchangeClient.listMessages() method to get an instance of the ExchangeMessagesInfoCollection collection.
- Loop through the ExchangeMessagesInfoCollection collection to get the unique URI for each message.
- Call the ExchangeClient.saveMessage() method and pass the unique URI as a parameter.
- Provide a saveMessage() method with a path to where you want to save the file.
Save Messages to an OutputStream
Instead of saving EML files to disk, it is possible to save it to an OutputStream. This is useful when you want to save the stream to some storage location like a database. Once the stream has been saved to a database, you can reload the EML file into the MailMessage class.
The code snippets below save messages from an Exchange Server mailbox to a memory stream.
Save Messages in MSG Format
The ExchangeClient.saveMessage() method can directly save the message to EML format. To save the messages to MSG format, first, call the ExchangeClient.fetchMessage() method which returns an instance of the MailMessage class. Then call the MailMessage.save() method to save the message to MSG.
The code snippet below gets messages from an Exchange Server mailbox and saves them to MSG format.