none
How to get mails from all folders in outlook ?? RRS feed

  • General discussion

  • Hi All,

    Please help me to achieve my task , I have to option :-

    1)To get all mails from inbox only :- i know how to get from inbox only

    2)To get mails from all folders and inbox :- i don't know how to do it .

    Please help ...............


    • Edited by Sohan India Tuesday, December 3, 2013 8:26 AM
    Tuesday, December 3, 2013 8:25 AM

All replies

  • Hello Sohan,

    1. To get all items of the Inbox folder you can use the Items property of the Folder class in Outlook:

    Outlook.NameSpace ns = OutlookApp.GetNamespace("MAPI");
    Outlook.MAPIFolder inbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) as Outlook.MAPIFolder;
    Outlook.Items items = inbox.Items;
    foreach (object item in items)
    {
       Outlook.MailItem mail = item as Outlook.MailItem;
       if(mail!=null)
       {
          MessageBox.Show(mail.Subject);
       }
       Marshal.ReleaseComObject(item);
    }
    Marshal.ReleaseComObject(items);
    Marshal.ReleaseComObject(inbox);
    Marshal.ReleaseComObject(ns);
    

    2. You can use the GetRootFolder method of the Store class to get the root folder. The root folder contains all folders that you typically can see in Outlook UI:

    Outlook.NameSpace ns = OutlookApp.GetNamespace("MAPI");
    Outlook.Stores stores = ns.Stores;
    foreach (object store in stores)
    {
       Outlook.Store st = store as Outlook.Store;
       if (st != null)
       {
          Outlook.MAPIFolder folder = st.GetRootFolder();
          Outlook.Folders folders = folder.Folders;
          foreach (object fld in folders)
          {
             Outlook.MAPIFolder f = fld as Outlook.MAPIFolder;
             if (f != null)
             {
                MessageBox.Show("Folder "+f.Name + " from "+st.DisplayName);
             }
             Marshal.ReleaseComObject(fld);
          }
          Marshal.ReleaseComObject(folders);
          Marshal.ReleaseComObject(folder);
       }
       Marshal.ReleaseComObject(store);
    }
    Marshal.ReleaseComObject(stores);
    Marshal.ReleaseComObject(ns);
    Good luck with your add-in or VBA macro!
    Tuesday, December 3, 2013 11:55 AM