Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I have an inbox that contains 2068 mail items. I need to retreive all 2068 mails. But as per my code, this skips 1000 mails i.e initially it shows total count as 2068 and retrieves 1000 mails in the first loop and shows count as 1000 (pagesize=1000) but in second loop it shows the count as 68 and skips the 1000 mails and NextPageOffset is null. What am i doing wrong ?

What I have tried:

C#
int pageSize = 1000;
FindFoldersResults findFolderItems = service.FindFolders(WellKnownFolderName.Inbox, sourceFolderFilter, new FolderView(1));
ItemView view = new ItemView(pageSize);
do
{
findMailItems = service.FindItems(findFolderItems.Folders[0].Id, searchFilter, view);                                
if (findMailItems.TotalCount > 0)
{
    foreach (var item in findMailItems.Items)
    {
         item.Move(folderId);
    }
}
view.Offset = findMailItems.NextPageOffset.Value;
}
 while (findMailItems.MoreAvailable);
Posted
Updated 13-Feb-17 9:57am
v2

1 solution

If you consider what your code is doing, the problem should be obvious: you're moving the messages whilst you're trying to retrieve them.

Consider a simplified example with three messages, moving one at a time:
  1. Inbox: [ "A", "B", "C" ];
  2. Retrieve message 0: "A";
  3. Move "A" to another folder;
  4. Inbox: [ "B", "C" ];
  5. Retrieve message 1: "C"
  6. Move "C" to another folder;
  7. Inbox: ["B"]
  8. Retrieve message 2: Error


As with any loop which modifies the collection it's iterating over, you need to adjust the index to account for your changes.
C#
do
{
    findMailItems = service.FindItems(findFolderItems.Folders[0].Id, searchFilter, view);
    if (findMailItems.TotalCount > 0)
    {
        foreach (var item in findMailItems.Items)
        {
            item.Move(folderId);
        }
    }
    
    // NB: Don't change the view.Offset here!
}
while (findMailItems.MoreAvailable);
 
Share this answer
 
Comments
User1454 14-Feb-17 0:09am    
Thank you Richard Deeming for explaining briefly !!! I'll try it..
User1454 14-Feb-17 5:27am    
Can you please help me where to add the index so that it takes the next set of the mail list ? I tried few ways but didn't work.. :(
Richard Deeming 14-Feb-17 8:00am    
You don't. As I explained, you're moving the items to a different folder. That means what used to be message 1000 will now be message 0. If you increase the offset, you'll skip an entire page of messages.
User1454 15-Feb-17 1:31am    
Hi Richard Deeming, thanks a ton! My mistake.. understood clearly and now it works like a charm.. but found everywhere with increasing the offset. anyway thanks a lot. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900