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:
- Inbox:
[ "A", "B", "C" ]
; - Retrieve message
0
: "A"
; - Move
"A"
to another folder; - Inbox:
[ "B", "C" ]
; - Retrieve message
1
: "C"
- Move
"C"
to another folder; - Inbox:
["B"]
- 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.
do
{
findMailItems = service.FindItems(findFolderItems.Folders[0].Id, searchFilter, view);
if (findMailItems.TotalCount > 0)
{
foreach (var item in findMailItems.Items)
{
item.Move(folderId);
}
}
}
while (findMailItems.MoreAvailable);