Title: Determine if the selected email is from inbox or sent items
Tags: c#;outlook;vsto;outlook-addin
Question: I am programming an Outlook Add-in and need to determine whether a selected email is from ```Inbox``` or ```Sent Items``` so that I can tag the email with folder="Inbox" or "Sent" when I save it in my database. 

I understand that I can compare the folder name to Inbox or Sent Items and determine the folder, however, how do I determine when the email selected is sitting in one of the sub-folders in the inbox. Is there a ```FolderType``` property to check whether the selected email's folder is inbox or sent (similar to identifying an item type with ```OlItemType```)?


Here is the accepted answer: You need to look at the ```MailItem.Parent``` and cast it to a ```Outlook.Folder```. Once you have the ```Folder```, you can access the display name via ```Folder.Name```. If you want to determine whether the selected item is a subfolder of ```Inbox```, you would need to recursively call up the ```Parent``` tree until ```Parent``` is null to find the root parent folder.

```Outlook.Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer();
Outlook.MailItem mailItem = explorer.Selection.OfType<Outlook.MailItem&gt;().First();
Outlook.Folder parentFolder = mailItem.Parent as Outlook.Folder;
if (parentFolder.Parent == null) // we are at the root
{
  string folderName = parentFolder.Name;
}
else
  // .. recurse up the parent tree casting parentFolder.Parent as Outlook.Folder...
```

You should obviously add error handling and object disposal to this sample code. 



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:08/13/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><outlook><vsto><outlook-addin>', 'Title': 'Determine if the selected email is from inbox or sent items', 'ViewCount': 4412, 'id': 11941288, 'user_AboutMe': '', 'user_AccountId': 1392483.0, 'user_DisplayName': 'amey1908', 'user_Reputation': 157.0, 'user_Views': 25.0, 'user_id': 1322955.0}}