Search code examples
c#console-applicationoffice-interopoutlook-2010

Console App to search outlook inbox with subject line filter skipping emails


I am writing a console application that searches through the current users outlook inbox. The program performs certain actions on each email based on the subject line and then moves the email to 1 of 3 subfolders. So far it has been running without throwing any errors. The problem is that it will skip over some of the emails in the inbox and I can't seem to find any logic behind why it does this. Sometimes it will skip the first email received, sometimes the last, other times it will leave 2 and move the rest. I've inserted a breakpoint in my code and stepped through each line and the skipped emails never show up. It doesn't seem to matter whether the emails are marked read or unread either. If I run the program multiple times then eventually the skipped emails are processed. What could be causing this?

        Microsoft.Office.Interop.Outlook.Application application = null;          

        if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
        {  
            application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application;
        }
        else  
        {
            //outlook not open, do nothing
        }         

        Microsoft.Office.Interop.Outlook.Items items = null;
        Microsoft.Office.Interop.Outlook.NameSpace ns = null;
        Microsoft.Office.Interop.Outlook.MAPIFolder inbox = null;
        Microsoft.Office.Interop.Outlook.Application();

        ns = application.Session;
        inbox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
        items = inbox.Items;

        try
        {                
            foreach (Object receivedMail in items)
            {                   
                if (receivedMail is MailItem)
                {                        
                    MailItem mail = receivedMail as MailItem;

                        string subject = mail.Subject.ToString().ToUpper();

                        switch (subject)
                        {
                            case "SUBJECT1":
                                DoStuff1(mail);           
                                mail.Move(inbox.Folders["folder1"]);   
                                break;
                            case "SUBJECT2":
                                DoStuff2(mail);                                    
                                mail.Move(inbox.Folders["folder2"]);
                                break;
                            default:
                                mail.Move(inbox.Folders["folder3"]);          
                                break;

                    }
                }
            }

            Console.WriteLine("Complete");
            Console.ReadLine();
        }
        catch (System.Runtime.InteropServices.COMException ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadLine();
        }
        catch (System.Exception ex)
        {
            Console.WriteLine(ex.ToString());
            Console.ReadLine();
        }   

Solution

  • If anyone happens to stumble across this I discovered a solution. I just added each email to a list first before performing any actions on them. I'm not sure why this step was necessary, maybe something to do with not being able to properly enumerate active inbox mail items.

    List<MailItem> ReceivedEmail = new List<MailItem>(); 
    
    foreach (Outlook.MailItem mail in emailFolder.Items)
    {
         ReceivedEmail.Add(mail);
    }
    
    foreach (MailItem mail in ReceivedEmail)
    { 
        //do stuff
    }