C # Outlook does not move all emails

I use Outllok Interop to move emails from one folder to another (after receiving all attachments, but this works), but does not copy all emails. I tried to wait, but it does not affect. First it will move 6, then 3, then 1. Can someone tell me why it is not moving them all?

The corresponding code is given below:

Application oOutlook = new Application();
NameSpace oNs = oOutlook.GetNamespace("MAPI");

Recipient oRep = oNs.CreateRecipient("ContentHelp");
MAPIFolder inbox = oNs.GetSharedDefaultFolder(oRep, OlDefaultFolders.olFolderInbox);

MAPIFolder nihSub = inbox.Folders["NIH"];
MAPIFolder nihArchive = inbox.Folders["NIHarchive"];
Items nihItems = nihSub.Items;
MailItem moveMail = null;
//inboxItems = inboxItems.Restrict("[Unread] = false");

int increment = 0;

try
{
    foreach (object collectionItem in nihItems)
    {
        moveMail = collectionItem as MailItem;
        if (moveMail != null)
        {
            Console.WriteLine("Moving {0}", moveMail.Subject.ToString());
            string titleSubject = (string)moveMail.Subject;
            moveMail.Move(nihArchive);
        }
    }
}
+3
source share
3 answers

The index gets reset every time you loop in moving, so you won't be more than half of the items. Use a While loop or a countdown from olItems.Count to 1.

+5
source

The back loop is a step from max to min.

IE:

for(int i = 10; i>0; i--)
{
     Console.WriteLine(i);
}

- : ( , Outlook, )

        for (int i=nihItems.count; i >= 0; i--)
        {
            moveMail collectionItem = nihItems[i] as MailItem

            if (moveMail != null)
            {
                Console.WriteLine("Moving {0}", moveMail.Subject.ToString());
                string titleSubject = (string)moveMail.Subject;
                moveMail.Move(nihArchive);
            }
         }
+2

, :

Outlook.Folder FolderInbox = NameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
List<Outlook.MailItem> MailItemList = new List<Outlook.MailItem>();
foreach (Outlook.MailItem item in FolderInbox.Items.OfType<Outlook.MailItem>())
    MailItemList.Add(item);
foreach (Outlook.MailItem item in MailItemList)
    ProcessMail(item);
0

Source: https://habr.com/ru/post/1720272/


All Articles