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:

  1. Create an instance of the ExchangeClient class.
  2. Provide the server name, username, password, and domain.
  3. Call the ExchangeClient.listMessages() method to get an instance of the ExchangeMessagesInfoCollection collection.
  4. Loop through the ExchangeMessagesInfoCollection collection to get the unique URI for each message.
  5. Call the ExchangeClient.saveMessage() method and pass the unique URI as a parameter.
  6. 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.