How to Retrieve Email Messages from Server with Paging Support inside .NET Apps
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.
//your code here...
// 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
Url: http://www.aspose.com/.net/email-component.aspx
Language: C# | User: Sheraz Khan | Created: Apr 13, 2016 | Tags: Retrieve Email Messages from Server listing Messages with Paging Support listing messages from the server .NET Email API Exporting Emails show Email from Inbox