1

I have an Outlook 2013 Add-in,

Outlook.MAPIFolder inboxFolder;
Outlook.Items mailInboxItems;

private void ThisAddIn_Startup(object sender, EventArgs e)
{
   ... other code ---

   mailInboxItems = inboxFolder.Items;
   mailInboxItems.ItemAdd += mailInboxItems_ItemAdd;
}

private void mailInboxItems_ItemAdd(object item)
{
   Outlook.MailItem emailMessage = (Outlook.MailItem)item; // cast error
   ProcessEmail(emailMessage);
}

An exception is thrown when, of course, the item coming in is not of type Outlook.MailItem:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'.

How can I check that the parameter "item" is of only a valid type, i.e. Outlook.MailItem to avoid any exceptions?

emily_bma
  • 301
  • 1
  • 15
  • [How to: Safely Cast by Using as and is Operators (C# Programming Guide)](http://msdn.microsoft.com/en-us/library/cc488006.aspx) – J. Steen Jan 12 '15 at 09:16
  • @J.Steen - Oh, great tip and now I'll just have to check for null! Would you happen to know how to narrow my scope to just MailItem? Thank you! – emily_bma Jan 12 '15 at 09:21

4 Answers4

2

You can use the "is" and "as" operators in C#. See How to: Programmatically Determine the Current Outlook Item for more information.

Also the Outlook object model provides the MessageClass property - a string representing the message class for the Outlook item. Under the hood the message class is used to identify what inspector to use in Outlook for displaying the item.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
1

To your folder you can add different kind of items like MailItem, AppointmentItem and so. To work with MailItems use code like this:

var emailMessage = item as MailItem;
if(emailMessage == null)
{
    retrun;
}

// here you can use emailMessage as MailItem
ProcessEmail(emailMessage);
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
0

First check item is mailitem like

  if(item is Outlook.MailItem){
    Outlook.MailItem emailMessage =item as Outlook.MailItem
    ProcessEmail(emailMessage);
  }

it will work as your expectation.

Shyam sundar shah
  • 2,473
  • 1
  • 25
  • 40
0

There are actually, at least, 16 Outlook item types. Documentation itemizing the different types is a bit hard to find, but one reference is here.

So, for example, if you wanted to move all the items from one folder to another, you could do something like:

    Items items = source.Items;

    foreach (object item in items)
    {
        switch (item)
        {
            case AppointmentItem appointmentItem:
                appointmentItem.Move(destination);
                break;
            case ContactItem contactItem:
                contactItem.Move(destination);
                break;
            case DistListItem distListItem:
                distListItem.Move(destination);
                break;
            case DocumentItem documentItem:
                documentItem.Move(destination);
                break;
            case JournalItem journalItem:
                journalItem.Move(destination);
                break;
            case MailItem mailItem:
                mailItem.Move(destination);
                break;
            case MeetingItem meetingItem:
                meetingItem.Move(destination);
                break;
            case NoteItem noteItem:
                noteItem.Move(destination);
                break;
            case PostItem postItem:
                postItem.Move(destination);
                break;
            case RemoteItem remoteItem:
                remoteItem.Move(destination);
                break;
            case ReportItem reportItem:
                reportItem.Move(destination);
                break;
            case TaskItem taskItem:
                taskItem.Move(destination);
                break;
            case TaskRequestAcceptItem taskRequestAcceptItem:
                taskRequestAcceptItem.Move(destination);
                break;
            case TaskRequestDeclineItem taskRequestDeclineItem:
                taskRequestDeclineItem.Move(destination);
                break;
            case TaskRequestItem taskRequestItem:
                taskRequestItem.Move(destination);
                break;
            case TaskRequestUpdateItem taskRequestUpdateItem:
                taskRequestUpdateItem.Move(destination);
                break;
        }

        Marshal.ReleaseComObject(item);
    }

The type matching in the case statements requires C# 7 or higher.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78