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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
// Create instance of ExchangeClient class by giving credentials | |
ExchangeClient client = new ExchangeClient("http://servername/exchange/username", "username", "password", "domain"); | |
// Call ListMessages method to list messages info from Inbox | |
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri()); | |
for (ExchangeMessageInfo msgInfo : msgCollection) { | |
String strMessageURI = msgInfo.getUniqueUri(); | |
// Now save the message in disk | |
client.saveMessage(strMessageURI, dataDir + msgInfo.getMessageId() + ".eml"); | |
} |
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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
// Create instance of ExchangeClient class by giving credentials | |
ExchangeClient client = new ExchangeClient("http://ex07sp1/exchange/Administrator", "user", "pwd", "domain"); | |
// Call ListMessages method to list messages info from Inbox | |
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri()); | |
// Loop through the collection to get Message URI | |
for (ExchangeMessageInfo msgInfo : msgCollection) { | |
String strMessageURI = msgInfo.getUniqueUri(); | |
try { | |
OutputStream outputStream = new FileOutputStream(dataDir + msgInfo.getMessageId() + "_Out.eml"); | |
client.saveMessage(strMessageURI, outputStream); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
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.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java | |
// Create instance of ExchangeClient class by giving credentials | |
ExchangeClient client = new ExchangeClient("http://ex07sp1/exchange/Administrator", "user", "pwd", "domain"); | |
// Call ListMessages method to list messages info from Inbox | |
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri()); | |
for (ExchangeMessageInfo msgInfo : msgCollection) { | |
String strMessageURI = msgInfo.getUniqueUri(); | |
// Now get the message details using FetchMessage() | |
MailMessage msg = client.fetchMessage(strMessageURI); | |
// Save message as MSG | |
msg.save(dataDir + msgInfo.getMessageId() + ".msg", SaveOptions.getDefaultMsg()); | |
} |