Fetch Messages from Exchange Server Mailbox using WebDav
Contents
[
Hide
]
Listing Messages on an Exchange Server used the listMessages() method to get a list of messages from an Exchange Server mailbox. The listMessages() method gets basic information about messages, for example, the subject, the message ID, from and to.
To get the complete message details, Aspose.Email.Exchange provides the ExchangeClient.fetchMessage() method. This method accepts the message URI as a parameter and returns an instance of the MailMessage class. The MailMessage class then provides message details like the body, headers, and attachments.
Fetch Messages from an Exchange Server Mailbox
To fetch messages from Exchange Server Mailbox:
- Create an instance of type ExchangeClient.
- Specify the server name, user name, password, and domain.
- Call the listMessages method to get the ExchangeMessageInfoCollection.
- Loop through the ExchangeMessageInfoCollection collection to get ExchangeMessageInfo.getUniqueUri values.
- Call ExchangeClient.fetchMessage() and pass ExchangeMessageInfo.getUniqueUri as parameter.
The following code snippet connects to the Exchange Server mailbox and fetches all the messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()); | |
} | |
} |