Question: I am trying to iterate the subfolders and subfolders of the folders. E.g. Inbox has five subfolders so I am trying to iterate all five. But I am able to iterate only the first subfolder.

```
 Outlook.MAPIFolder inboxFolder = 
 ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
 Outlook.Stores stores = ns.Stores;

 foreach(Store store in stores)
 {

      Outlook.MAPIFolder inbox_folder = 
      store.GetDefaultFolder(outlook.olDefaultFolders.olFolderInbox);

             foreach(Outlook.MailItem item in Inbox_folder.items)
             {
                   //code for attachment
              }
}

```

When I want to iterate the subfolder of the subfolder it is returning only the first subfolder of the folder. I want to iterate the nth subfolder of the folder.

Answer: 
The code that processes the folder must call itself recursively

```
   foreach(Store store in stores)
   {
      Outlook.MAPIFolder inbox_folder = 
      store.GetDefaultFolder(outlook.olDefaultFolders.olFolderInbox);
      ProcessFolder(inbox_folder);
   }
  ...    

  private void ProcessFolder(MAPIFolder folder)
  {
     //subfolders
     foreach(MAPIFolder subFolder in folder.Folders)
     {
        ProcessFolder(subFolder);
     } 
     //items
     foreach (Object item in folder.Items)
     {
     }
  }

```
As Dmitry noted, you need to use recursion to be able to enumerate all folders. Read more about that in the [Enumerate folders](http://Enumerate%20folders) article. For example:

```
private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// Uses recursion to enumerate Outlook subfolders.
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders = folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // Write the folder path.
            Debug.WriteLine(childFolder.FolderPath);
            // Call EnumerateFolders using childFolder.
            EnumerateFolders(childFolder);
        }
    }
}               

```

Although, Microsoft shows `foreach` loops in their sample code, it is not the best way to enumerate folders. I'd recommend using the `for` loop instead. Thus, you will be able to release underlying COM objects instantly. For example, the `Fodlers` property used in the `foreach` loop returns a new instance of the corresponding class increasing the reference counter. 

Use [System.Runtime.InteropServices.Marshal.ReleaseComObject](https://msdn.microsoft.com/en-us/library/843807e2) to release an Outlook object when you have finished using it. This is particularly important if your add-in attempts to enumerate more than 256 Outlook items in a collection that is stored on a Microsoft Exchange Server. If you do not release these objects in a timely manner, you can reach the limit imposed by Exchange on the maximum number of items opened at any one time. Then set a variable to `Nothing` in Visual Basic (null in C#) to release the reference to the object. Read more about that in the [Systematically Releasing Objects](https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2007/bb623945(v=office.12)) article.