Listing Messages from Exchange Server using WebDav
A list of the email messages in an Exchange mailbox can be fetched by calling the ExchangeClient.listMessages() method. Get the basic information about messages, such as subject, from, to and message ID, using the listMessages() method.
This article shows how to connect to an Exchange Server and list the emails in a mailbox directly or using WebDav. It further shows how to list messages from different folders and how to list messages by ID.
List Exchange Server Messages
To list the messages in an Exchange mailbox:
- Create an instance of the ExchangeClient class.
- Call the listMessages method and create a message collection.
- Loop through the collection and display message information.
The code snippet below connects to the Exchange mailbox and get the list of messages from the Inbox folder.
// 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://MachineName/exchange/Username", "username", "password", "domain"); | |
// Call ListMessages method to list messages info from Inbox | |
ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri()); | |
// Loop through the collection to display the basic information | |
for (ExchangeMessageInfo msgInfo : msgCollection) { | |
System.out.println("Subject: " + msgInfo.getSubject()); | |
System.out.println("From: " + msgInfo.getFrom()); | |
System.out.println("To: " + msgInfo.getTo()); | |
System.out.println("Sent Date: " + msgInfo.getDate()); | |
System.out.println("Read?: " + msgInfo.isRead()); | |
System.out.println("Message ID: " + msgInfo.getMessageId()); | |
System.out.println("Unique URI: " + msgInfo.getUniqueUri()); | |
System.out.println("=================================="); | |
} |
Listing Messages from Different Folders
The above code snippets list all the messages in the Inbox folder. It is possible to get the list of messages from other folders as well. The ExchangeClient.listMessages() method accepts a folder URI as a parameter. As long as the folder URI is valid, you can get the list of messages from that folder.
Use ExchangeClient.getMailboxInfo().xxxFolderUri property to get the URI of different folders. The rest of the code is the same as for getting a list of 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://MachineName/exchange/Username","username", "password", "domain"); | |
// Get folder URI | |
String strFolderURI = ""; | |
strFolderURI = client.getMailboxInfo().getInboxUri(); | |
strFolderURI = client.getMailboxInfo().getDeletedItemsUri(); | |
strFolderURI = client.getMailboxInfo().getDraftsUri(); | |
strFolderURI = client.getMailboxInfo().getSentItemsUri(); | |
// Get list of messages from the specified folder | |
ExchangeMessageInfoCollection msgCollection = client.listMessages(strFolderURI); |