Fetch Messages from Exchange Server Mailbox using WebDav

Fetch Messages from an Exchange Server Mailbox

To fetch messages from Exchange Server Mailbox:

  1. Create an instance of type ExchangeClient.
  2. Specify the server name, user name, password, and domain.
  3. Call the listMessages method to get the ExchangeMessageInfoCollection.
  4. Loop through the ExchangeMessageInfoCollection collection to get ExchangeMessageInfo.getUniqueUri values.
  5. Call ExchangeClient.fetchMessage() and pass ExchangeMessageInfo.getUniqueUri as parameter.

The following code snippet connects to the Exchange Server mailbox and fetches all the messages.

// 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", "username", "password", "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();
// Now get the message details using FetchMessage()
MailMessage msg = client.fetchMessage(strMessageURI);
// Display message details
System.out.println("Subject: " + msg.getSubject());
System.out.println("HTML Body: " + msg.getHtmlBody());
System.out.println("Number of attachments: " + msg.getAttachments().size());
for (Attachment att : msg.getAttachments()) {
System.out.println("Attachment Name: " + att.getName());
}
}