Posted By

sherazam on 04/13/16


Tagged

email show paging server Net with from api inbox messages retrieve the support listing emails Exporting


Versions (?)

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


 / Published in: C#
 

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

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.

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

Report this snippet  

You need to login to post a comment.