We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

How to: Perform Actions When an E-Mail Message Is Received

Updated: April 2011

This example performs custom actions when the user receives an e-mail message.

Applies to: The information in this topic applies to application-level projects for Outlook 2007 and Outlook 2010. For more information, see Features Available by Office Application and Project Type.


Outlook.NameSpace outlookNameSpace;
Outlook.MAPIFolder inbox;
Outlook.Items items;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    outlookNameSpace = this.Application.GetNamespace("MAPI");
    inbox = outlookNameSpace.GetDefaultFolder(
            Microsoft.Office.Interop.Outlook.
            OlDefaultFolders.olFolderInbox);

    items = inbox.Items;
    items.ItemAdd +=
        new Outlook.ItemsEvents_ItemAddEventHandler(items_ItemAdd);
}

void items_ItemAdd(object Item)
{
    string filter = "USED CARS";
    Outlook.MailItem mail = (Outlook.MailItem)Item;
    if (Item != null)
    {
        if (mail.MessageClass == "IPM.Note" &&
                   mail.Subject.ToUpper().Contains(filter.ToUpper()))
        {
            mail.Move(outlookNameSpace.GetDefaultFolder(
                Microsoft.Office.Interop.Outlook.
                OlDefaultFolders.olFolderJunk));
        }
    }

}


Date

History

Reason

April 2011

Updated code example as per some recent customer feedback.

Customer feedback.

Show: