Skip to content

Iterate throw E-mail items in Outlook

michal adamkiewiczm

Asked Apr 25, 2012, 11:48 AM EDT1 answer
Share
LinkedInX
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

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi Michal,

    First add a reference to the Outlook COM object your project:


    In VS.NET right click on References and choose Add Reference.
    Select the COM tab
    Choose "Microsoft Outlook 11.0 Object Library" (this is for MS Office 2003 - I think 10.0 is for Office XP) and click Select.
    Click OK.
    Note that you can access any Outlook/Exchange object types, eg Appointments, Notes, Tasks, Emails etc - just use intellisense to select which one (eg Microsoft.Office.Interop.Outlook. ... - see definition of variable called 'item' below).


    Here's the code:


    Microsoft.Office.Interop.Outlook.Application app = null;
    Microsoft.Office.Interop.Outlook._NameSpace ns = null;
    Microsoft.Office.Interop.Outlook.PostItem item = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null;
    Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null;


    try 
    {
      app = new Microsoft.Office.Interop.Outlook.Application();
      ns = app.GetNamespace("MAPI");
      ns.Logon(null,null,false, false);


      inboxFolder = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
      subFolder = inboxFolder.Folders["MySubFolderName"]; //folder.Folders[1]; also works
      Console.WriteLine("Folder Name: {0}, EntryId: {1}", subFolder.Name, subFolder.EntryID);
      Console.WriteLine("Num Items: {0}", subFolder.Items.Count.ToString());


      for(int i=1;i<=subFolder.Items.Count;i++)
      {
        item = (Microsoft.Office.Interop.Outlook.PostItem)subFolder.Items[i];
        Console.WriteLine("Item: {0}", i.ToString());
        Console.WriteLine("Subject: {0}", item.Subject); 
        Console.WriteLine("Sent: {0} {1}" item.SentOn.ToLongDateString(), item.SentOn.ToLongTimeString());
        Console.WriteLine("Categories: {0}", item.Categories);
        Console.WriteLine("Body: {0}", item.Body);
        Console.WriteLine("HTMLBody: {0}", item.HTMLBody); 
      }
    } 
    catch (System.Runtime.InteropServices.COMException ex) 
    {
      Console.WriteLine(ex.ToString());
    }
    finally
    {
      ns = null;
      app = null;
      inboxFolder = null;
    }

    For more info:
    http://www.programminghelp.com/programming/dotnet/access-your-email-within-outlook-final-part-c/ 
    0