1
Answer

Iterate throw E-mail items in Outlook

michal adamkiewiczm

michal adamkiewiczm

12y
3.2k
1
Hi Guys,

I'm new soo I don't really is this a good topic board (shouldn't I wrote on Office Interoperability?), but I think that my problem is so silly for someone who know's .NET and C#, that I will try to ask:).

The idea is that I must only iterate for every mail in Outlook 2007 inbox folder, and those mail which passed my conditions, I must moved to subfoler named "Archive". This is only a few lines of code, but even with this a have problem:(, and the worst thing is that I need this to my work for tommorrow. My code work fine.....but only for 1 mail...and I dont't know why? For example I sent to my e-mail account set up in Outlook 3 mails which are consistend with my conditions (for example every 3 mails have no subject and they have only 1 attachment), and run my application in VS 2008, then I look in Outlook and I see that only one of those three mails have been moved, If I run app, next mail will be moved to Archive subfolder, and like that....I don't know why he don't do this at once as I iterate using foreach? It's not logical for me:(

This is my very small code:


 Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace names = outlook.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.MAPIFolder odbiorcza = names.GetDefaultFolder(OlDefaultFolders.olFolderInbox);
            Microsoft.Office.Interop.Outlook.MAPIFolder archive = names.GetDefaultFolder(OlDefaultFolders.olFolderInbox).Folders["Archive"];

       
                if (odbiorcza.Items.Count > 0)
                {
                    foreach (Microsoft.Office.Interop.Outlook.MailItem mail in odbiorcza.Items)
                    {
                        if (mail.Subject == null || mail.Subject.StartsWith("WWY"))
                        {
                            MessageBox.Show(mail.SenderName.ToString());

                            if (mail.Attachments.Count == 1)
                            {
                                mail.Move(archive);
                            }
                        }
                    }


                }



I would be very gratefully for every advice and tip, really!
And sorry for my language, but I'm just learning english

Best Greets
Michael

Answers (1)