Pieter van der Westhuizen

Working with Outlook special folders (Inbox, Outbox, Drafts, Sent Items, Deleted Items)

Happy 2014 everyone! We’ll start this year with an article looking at Outlook special folders.

Microsoft Outlook has a number of special folders of which the most common folders are:

Further on in this article you will learn how to work with each special folder in the most efficient manner:

What are special Outlook folders?

First off, let’s have a closer look at each of the Outlook special folders and see in what way it is different from the others.

Inbox

Inbox and Outbox folders in Outlook

When trying to imagine how most of the folders in Microsoft Outlook work, try comparing it to the classic in/out tray you’ll find in many offices. Each folder in Outlook represents a tray and in the case of the Inbox folder, it is without a doubt the IN tray.

All incoming mail lands in the Inbox, even when you specified a rule to move specific e-mails, the e-mail is first delivered to the Inbox and then moved to the destination folder.

Drafts

The Drafts folder contains items that have not yet been sent. An e-mail is saved to the Drafts folder when a user composes the e-mail and then clicks the Save button in the Outlook Mail Inspector window. Think of this folder as your in-progress tray.

Sent Items

The Sent Items folder contains all items that have been sent. When a user composes an e-mail and clicks the Send button, the e-mail is first moved to the Outbox folder and after it has been successfully sent it is moved to the Sent Items folder.

Outbox

As mentioned above, when a user sends an e-mail it is first moved to the Outbox folder. Depending on the size of the e-mail it might remain in the Outbox folder for some time. As soon as the e-mail is successfully sent it is moved to the Sent Items folder.

Deleted Items

Any items the user deletes in Outlook are moved to the Deleted Items folder. Items can be moved back to other Outlook folders from the Deleted Items folder if needs be. It is also possible to recover deleted items that are no longer in the Deleted Items folder, which will be discussed later on in this article.

Accessing an Outlook special folder

In order to access any Outlook folder’s properties, methods or items, we first need to get a reference to the Outlook MAPI namespace by using the GetNameSpace method of the Outlook.Application object. From there we get a reference to the Inbox folder by invoking the GetDefaultFolder method of the Outlook.NameSpace object. This method accepts an enum parameter of Outlook.OlDefaultFolders; which is a list of default Outlook folders. To access the Inbox we set this parameter to Outlook.OlDefaultFolders.olFolderInbox

In order to access the other special folders, you need to set the parameter value to one of the following:

  • Outlook.OlDefaultFolders.olFolderDrafts;
  • Outlook.OlDefaultFolders.olFolderSentMail;
  • Outlook.OlDefaultFolders.olFolderDeletedItems; or
  • Outlook.OlDefaultFolders.olFolderOutbox.

The following code will enable us to access the Inbox folder:

private void inboxAccessRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    Outlook.NameSpace nameSpace = null;
    Outlook.Folder inboxFolder = null;
 
    try
    {
        nameSpace = OutlookApp.GetNamespace("MAPI");
        inboxFolder = nameSpace.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
 
    }
    finally
    {
        if (inboxFolder != null) Marshal.ReleaseComObject(inboxFolder);
        if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
    }
}

Enumerating items in an Outlook special folder

Enumerating items is fairly straight forward especially when using the above mentioned code, because we already have a reference to the Inbox folder. We have a choice to either use the Items collection of the folder or if we’re using Outlook 2007, 2010 or Outlook 2013, we can use the Table object.

First, we’ll look at how to enumerate items in the Inbox folder using the Items collection:

private void inboxAccessRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    Outlook.NameSpace nameSpace = null;
    Outlook.Folder inboxFolder = null;
    Outlook.Items inboxItems = null;
    string items = string.Empty;
 
    try
    {
        nameSpace = OutlookApp.GetNamespace("MAPI");
        inboxFolder = nameSpace.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
        if (inboxFolder != null)
        {
            inboxItems = inboxFolder.Items;
            for (int i = 1; i <= inboxItems.Count; i++)
            {
                Outlook.MailItem mail = inboxItems[i] as Outlook.MailItem;
                if (mail != null)
                {
                    items += string.Format("{0}{1}", mail.Subject, Environment.NewLine);
                    Marshal.ReleaseComObject(mail);
                }
            }
            MessageBox.Show(items);
        }
    }
    finally
    {
        if (inboxItems != null) Marshal.ReleaseComObject(inboxItems);
        if (inboxFolder != null) Marshal.ReleaseComObject(inboxFolder);
        if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
    }
}

As I’ve mentioned, Outlook 2007 introduced the Table object which performs better than enumerating items in an Outlook folder using the Items collection. To use it, you invoke the GetTable method of the Outlook.Folder object.

private void accessInboxTableRibbonButton_OnClick(object sender,
    IRibbonControl control, bool pressed)
{
    Outlook.NameSpace nameSpace = null;
    Outlook.Folder inboxFolder = null;
    Outlook.Table inboxTable = null;
    string items = string.Empty;
 
    try
    {
        nameSpace = OutlookApp.GetNamespace("MAPI");
        inboxFolder = nameSpace.GetDefaultFolder(
            Outlook.OlDefaultFolders.olFolderInbox) as Outlook.Folder;
        if (inboxFolder != null)
        {
            inboxTable = inboxFolder.GetTable();
            while (!inboxTable.EndOfTable)
            {
                Outlook.Row inboxRow = inboxTable.GetNextRow();
                items += string.Format("{0}{1}",
                    inboxRow["Subject"], Environment.NewLine);
                Marshal.ReleaseComObject(inboxRow);
            }
            Marshal.ReleaseComObject(inboxTable);
            MessageBox.Show(items);
        }
    }
    finally
    {
        if (inboxFolder != null) Marshal.ReleaseComObject(inboxFolder);
        if (nameSpace != null) Marshal.ReleaseComObject(nameSpace);
    }
}

Handy tips, tricks and events

Outlook provides a number of events in order for developers to respond to a variety of actions that could occur in an Outlook folder, including the mentioned special folders.

Processing incoming items

For example, if you would like to process any incoming e-mail that arrives in the Inbox folder you could use the NewMail, NewMailEx or ItemsAdd events.

We’ve covered handling new e-mails in a series of blog articles explaining all the various techniques and drawbacks. You can read more about it in Outlook NewMail event unleashed (NewMail, NewMailEx, ItemAdd)

Processing outgoing items

Handling and processing outgoing Outlook items is easier than you might think. Outlook provides an ItemSend event which you can use to get a reference to any items that are being sent. In the following code, we get a reference to the item being sent and then check whether it is an Outlook.MailItem. We check the type because the ItemSend event can pass in a reference to a MailItem, AppointmentItem, MeetingItem, TaskItem, SharingItem or a MobileItem.

After getting a reference to the outgoing e-mail, we simply append some text to its subject.

private void adxOutlookEvents_ItemSend(object sender, ADXOlItemSendEventArgs e)
{
    if (e.Item is Outlook.MailItem)
    {
        Outlook.MailItem mail = (Outlook.MailItem)e.Item;
        mail.Subject += " Updated by My Addin";
    }
}

Restoring deleted items

Restoring deleted items can be as simple as dragging the e-mail from the Deleted Items folder to the Inbox folder. However, this is only possible if the e-mail is actually still in the Deleted Item folder.

If you’ve connected to a Microsoft Exchange account, an additional option is available to restore deleted items by right-clicking on the Deleted Items folder and selecting the “Recover Deleted Items…” option.

The 'Recover Deleted Items...' option

This will display a dialog with a list of deleted items.

Recovering Deleted Items in an Outlook Exchange based account

Take note though that not all the deleted items might be in the list as your Exchange administrator specified the retention time for deleted items, so take extra care when deleting Outlook items!

Thank you for reading. Until next time, keep coding!

Available downloads:

This sample Outlook add-in was developed using Add-in Express for Office and .net:

C# Outlook Special Folders add-in

You may also be interested in:

Post a comment

Have any questions? Ask us right now!