Home > Code > C# > How to Retrieve Email Messages from Server with Paging Support inside .NET Apps

How to Retrieve Email Messages from Server with Paging Support inside .NET Apps

by sher azam   on Apr 13, 2016   Category: C#   |  Views: 440    |  Points: 25   |  Gold 


This technical tip explains how to .NET developers can list email messages with paging support inside their .NET Applications. In scenarios, where the email server contains a large number of messages in mailbox, it is often desired to list or retrieve the messages with paging support. Aspose.Email API's ImapClient lets you retreive the messages from the server with paging support.

// Listing Messages with Paging Support

//C# Code Samples

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
///<summary>
///This example shows the paging support of ImapClient for listing messages from the server
///Available in Aspose.Email for .NET 6.4.0 and onwards
///</summary>
using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password"))
{
try
{
int messagesNum = 12;
int itemsPerPage = 5;
MailMessage message = null;
//Create some test messages and append these to server's inbox
for (int i = 0; i < messagesNum; i++)
{
message = new MailMessage(
"from@domain.com",
"to@domain.com",
"EMAILNET-35157 - " + Guid.NewGuid().ToString(),
"EMAILNET-35157 Move paging parameters to separate class");
client.AppendMessage(ImapFolderInfo.InBox, message);
}

//List messages from inbox
client.SelectFolder(ImapFolderInfo.InBox);
ImapMessageInfoCollection totalMessageInfoCol = client.ListMessages();
//Verify the number of messages added
Console.WriteLine(totalMessageInfoCol.Count);

//////////////////RETREIVE THE MESSAGES USING PAGING SUPPORT////////////////////////////////////

List<ImapPageInfo> pages = new List<ImapPageInfo>();
ImapPageInfo pageInfo = client.ListMessagesByPage(itemsPerPage);
Console.WriteLine(pageInfo.TotalCount);
pages.Add(pageInfo);
while (!pageInfo.LastPage)
{
pageInfo = client.ListMessagesByPage(pageInfo.NextPage);
pages.Add(pageInfo);
}
int retrievedItems = 0;
foreach (ImapPageInfo folderCol in pages)
retrievedItems += folderCol.Items.Count;
Console.WriteLine(retrievedItems);
}
finally
{
}
}

//VB.NET Code Samples

' For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
'<summary>
'This example shows the paging support of ImapClient for listing messages from the server
'Available in Aspose.Email for .NET 6.4.0 and onwards
'</summary>
Using client As New ImapClient("host.domain.com", 993, "username", "password")
Try
Dim messagesNum As Integer = 12
Dim itemsPerPage As Integer = 5
Dim message As MailMessage = Nothing
'Create some test messages and append these to server's inbox
For i As Integer = 0 To messagesNum - 1
message = New MailMessage("from@domain.com", "to@domain.com", "EMAILNET-35157 - " + Guid.NewGuid().ToString(), "EMAILNET-35157 Move paging parameters to separate class")
client.AppendMessage(ImapFolderInfo.InBox, message)
Next

'List messages from inbox
client.SelectFolder(ImapFolderInfo.InBox)
Dim totalMessageInfoCol As ImapMessageInfoCollection = client.ListMessages()
'Verify the number of messages added
Console.WriteLine(totalMessageInfoCol.Count)

'///////////////RETREIVE THE MESSAGES USING PAGING SUPPORT////////////////////////////////////

Dim pages As New List(Of ImapPageInfo)()
Dim pageInfo As ImapPageInfo = client.ListMessagesByPage(itemsPerPage)
Console.WriteLine(pageInfo.TotalCount)
pages.Add(pageInfo)
While Not pageInfo.LastPage
pageInfo = client.ListMessagesByPage(pageInfo.NextPage)
pages.Add(pageInfo)
End While
Dim retrievedItems As Integer = 0
For Each folderCol As ImapPageInfo In pages
retrievedItems += folderCol.Items.Count
Next
Console.WriteLine(retrievedItems)
Finally
End Try
End Using

Overview: Aspose.Email for .NET

Aspose.Email for .NET is a set of components allowing developers to easily implement email functionality within their ASP.NET web applications, web services & Windows applications. It Supports Outlook PST, EML, MSG & MHT formats. It allows developers to work with SMTP, POP3, FTP & MS Exchange servers. It supports mail merge, iCalendar, customized header & body, header information, embedded files, Twitter & many more. It makes it easy to work with HTML or plain text emails & their attachments.

- Homepage of Aspose.Email for .NET: http://www.aspose.com/.net/email-component.aspx

- Download Aspose.Email for .NET: http://www.aspose.com/community/files/51/.net-components/aspose.email-for-.net/default.aspx



Post Code  |  Code Snippet Home

User Responses


No response found, be the first to review this code snippet.

Submit feedback about this code snippet

Please sign in to post feedback

Latest Posts