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


/ Published in: C#
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. // Listing Messages with Paging Support
  2.  
  3. //C# Code Samples
  4.  
  5. // For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
  6. ///<summary>
  7. ///This example shows the paging support of ImapClient for listing messages from the server
  8. ///Available in Aspose.Email for .NET 6.4.0 and onwards
  9. ///</summary>
  10. using (ImapClient client = new ImapClient("host.domain.com", 993, "username", "password"))
  11. {
  12. try
  13. {
  14. int messagesNum = 12;
  15. int itemsPerPage = 5;
  16. MailMessage message = null;
  17. //Create some test messages and append these to server's inbox
  18. for (int i = 0; i < messagesNum; i++)
  19. {
  20. message = new MailMessage(
  21. "EMAILNET-35157 - " + Guid.NewGuid().ToString(),
  22. "EMAILNET-35157 Move paging parameters to separate class");
  23. client.AppendMessage(ImapFolderInfo.InBox, message);
  24. }
  25.  
  26. //List messages from inbox
  27. client.SelectFolder(ImapFolderInfo.InBox);
  28. ImapMessageInfoCollection totalMessageInfoCol = client.ListMessages();
  29. //Verify the number of messages added
  30. Console.WriteLine(totalMessageInfoCol.Count);
  31.  
  32. //////////////////RETREIVE THE MESSAGES USING PAGING SUPPORT////////////////////////////////////
  33.  
  34. List<ImapPageInfo> pages = new List<ImapPageInfo>();
  35. ImapPageInfo pageInfo = client.ListMessagesByPage(itemsPerPage);
  36. Console.WriteLine(pageInfo.TotalCount);
  37. pages.Add(pageInfo);
  38. while (!pageInfo.LastPage)
  39. {
  40. pageInfo = client.ListMessagesByPage(pageInfo.NextPage);
  41. pages.Add(pageInfo);
  42. }
  43. int retrievedItems = 0;
  44. foreach (ImapPageInfo folderCol in pages)
  45. retrievedItems += folderCol.Items.Count;
  46. Console.WriteLine(retrievedItems);
  47. }
  48. finally
  49. {
  50. }
  51. }
  52.  
  53. //VB.NET Code Samples
  54.  
  55. ' For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
  56. '<summary>
  57. 'This example shows the paging support of ImapClient for listing messages from the server
  58. 'Available in Aspose.Email for .NET 6.4.0 and onwards
  59. '</summary>
  60. Using client As New ImapClient("host.domain.com", 993, "username", "password")
  61. Try
  62. Dim messagesNum As Integer = 12
  63. Dim itemsPerPage As Integer = 5
  64. Dim message As MailMessage = Nothing
  65. 'Create some test messages and append these to server's inbox
  66. For i As Integer = 0 To messagesNum - 1
  67. message = New MailMessage("[email protected]", "[email protected]", "EMAILNET-35157 - " + Guid.NewGuid().ToString(), "EMAILNET-35157 Move paging parameters to separate class")
  68. client.AppendMessage(ImapFolderInfo.InBox, message)
  69. Next
  70.  
  71. 'List messages from inbox
  72. client.SelectFolder(ImapFolderInfo.InBox)
  73. Dim totalMessageInfoCol As ImapMessageInfoCollection = client.ListMessages()
  74. 'Verify the number of messages added
  75. Console.WriteLine(totalMessageInfoCol.Count)
  76.  
  77. '///////////////RETREIVE THE MESSAGES USING PAGING SUPPORT////////////////////////////////////
  78.  
  79. Dim pages As New List(Of ImapPageInfo)()
  80. Dim pageInfo As ImapPageInfo = client.ListMessagesByPage(itemsPerPage)
  81. Console.WriteLine(pageInfo.TotalCount)
  82. pages.Add(pageInfo)
  83. While Not pageInfo.LastPage
  84. pageInfo = client.ListMessagesByPage(pageInfo.NextPage)
  85. pages.Add(pageInfo)
  86. End While
  87. Dim retrievedItems As Integer = 0
  88. For Each folderCol As ImapPageInfo In pages
  89. retrievedItems += folderCol.Items.Count
  90. Next
  91. Console.WriteLine(retrievedItems)
  92. Finally
  93. End Try
  94. End Using

URL: http://www.aspose.com/docs/display/emailnet/Listing+Messages+with+Paging+Support

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.