Fetch Outlook Calendar Mails

0

In my inbox there are some calendar mail (meeting request from calendar). when application are fetching mail of calendar mail from inbox then it is throwing following error:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

public void GetOutLookEmails()
{ 
      oApp = new Outlook.Application();
      oNS = oApp.GetNamespace("MAPI");

      foreach (Outlook.MAPIFolder folder in oNS.Folders)
      {
          GetFolders(folder);
      }
}

public void GetFolders(Outlook.MAPIFolder folder)
{
    if (folder.Folders.Count == 0)
    {
        try
        {
            if (folder.DefaultItemType == Outlook.OlItemType.olMailItem)
            {
                if (folder.Name == "Inbox")
                {
                    oEmailsFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
                    Outlook.Items Inboxitems = oEmailsFolder.Items;

                    if (Inboxitems.Count > 0)
                    {
                        foreach (Outlook.MailItem mail in Inboxitems)///when compiler comes here it does not create mail object and throws error...because email contains calendar reminder so I guess I need to check if it is olCalendar event or something else that resolves error

                        {
                            if (mail != null)
                            {
                                 //here I am retrieving concerning data from emails///no issue here
                            }
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    else
    {
        foreach (Outlook.MAPIFolder subFolder in folder.Folders)
        {
            GetFolders(subFolder);
        }
    }
}
c#
asked on Stack Overflow Jun 1, 2014 by RameezAli • edited Jan 31, 2019 by Cœur

1 Answer

0

The item which you are casting may be of different type - ContactItem, AppointmentItem, MeetingItem, TaskItem. Check for the type and then cast it and use it.

https://msdn.microsoft.com/en-us/library/ms268994.aspx

-Vimal

answered on Stack Overflow Aug 4, 2015 by v.m.l

User contributions licensed under CC BY-SA 3.0