Programmatically retrieve unread messages from the Inbox

This example retrieves unread email messages from the Outlook Inbox and displays the number of items.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Example

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Outlook.MAPIFolder inbox =
        this.Application.ActiveExplorer().Session.GetDefaultFolder
        (Outlook.OlDefaultFolders.olFolderInbox);

    Outlook.Items unreadItems = inbox.
        Items.Restrict("[Unread]=true");

    MessageBox.Show(
        string.Format("Unread items in Inbox = {0}", unreadItems.Count));
}