How to Fetch Email Messages from Exchange Server with Paging Option in .NET Apps

This technical tip explains how .NET developers can Fetch Email Messages from an Exchange Server Mailbox inside .NET Apps. Listing Messages on an Exchange Server used the listMessages() method to get a list of messages from an Exchange Server mailbox. The listMessage() 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 am instance of the Aspose.Emai.Mail.MailMessage class. The MailMessage class then provides message details like body, headers and attachments.Find out more about the MailMessage API, or find out how to manage emails with the MailMessage class.
//your code here...The following code snippets connect to the Exchange Server mailbox and fetches all the messages. //[Java Code Sample] // 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()); //Console.WriteLine("HTML Body: " + msg.HtmlBody); // How many attachments System.out.println("Number of attachments: " + msg.getAttachments().size()); // List the attachments for (Attachment att : msg.getAttachments()) { System.out.println("Attachment Name: " + att.getName()); } } //Below are code snippets that fetches messages using EWS: //[C# Code Sample] // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "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(); // Now get the message details using FetchMessage() MailMessage msg = client.fetchMessage(strMessageURI); // Display message details System.out.println("Subject: " + msg.getSubject()); //Console.WriteLine("HTML Body: " + msg.HtmlBody); // How many attachments System.out.println("Number of attachments: " + msg.getAttachments().size()); // List the attachments for (Attachment att : msg.getAttachments()) { System.out.println("Attachment Name: " + att.getName()); } } //Enumerating Messages with Paging in EWS //[C# Code Sample] // Create instance of ExchangeWebServiceClient class by giving credentials IEWSClient client = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain"); // Call ListMessages method to list messages info from Inbox ExchangeMessageInfoCollection msgCollection = client.listMessages(client.getMailboxInfo().getInboxUri()); try { int itemsPerPage = 5; ////////////////////////////////////////////////////// List<ExchangeMessageInfoCollection> pages = new List<ExchangeMessageInfoCollection>(); ExchangeMessageInfoCollection pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage); pages.Add(pagedMessageInfoCol); while (!pagedMessageInfoCol.LastPage) { pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1); pages.Add(pagedMessageInfoCol); } ////////////////////////////////////////////////////// pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage); while (!pagedMessageInfoCol.LastPage) { client.ListMessages(client.MailboxInfo.InboxUri, pagedMessageInfoCol, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1); } } finally { } //[VB.NET Code Sample] ' Create instance of ExchangeWebServiceClient class by giving credentials Dim client As IEWSClient = EWSClient.getEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain") ' Call ListMessages method to list messages info from Inbox Dim msgCollection As ExchangeMessageInfoCollection = client.listMessages(client.getMailboxInfo().getInboxUri()) Try Dim itemsPerPage As Integer = 5 '''/////////////////////////////////////////////////// Dim pages As New List(Of ExchangeMessageInfoCollection)() Dim pagedMessageInfoCol As ExchangeMessageInfoCollection = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage) pages.Add(pagedMessageInfoCol) While Not pagedMessageInfoCol.LastPage pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1) pages.Add(pagedMessageInfoCol) End While '''/////////////////////////////////////////////////// pagedMessageInfoCol = client.ListMessages(client.MailboxInfo.InboxUri, itemsPerPage) While Not pagedMessageInfoCol.LastPage client.ListMessages(client.MailboxInfo.InboxUri, pagedMessageInfoCol, itemsPerPage, pagedMessageInfoCol.LastItemOffset.Value + 1) End While Finally End Try

Url: http://www.aspose.com/.net/email-component.aspx

Language: C# | User: Sheraz Khan | Created: Oct 28, 2015 | Tags: Fetch Messages from an Exchange Server, get a list of messages from an Exchange Server, connect to the Exchange Server mailbox, Enumerating Messages with Paging in EWS, .NET Email component Fetch Messages from an Exchange Server , get a list of messages from an Exchange Server connect to the Exchange Server mailbox Enumerating Messages with Paging in EWS .NET Email component Exchange Web Services