获取Outlook日历邮件

时间:2014-06-01 11:49:24

标签: c#

在我的收件箱中有一些日历邮件(来自日历的会议请求)。当应用程序从收件箱中提取日历邮件的邮件时,它会抛出以下错误:

  

无法将“System .__ ComObject”类型的COM对象强制转换为接口   输入'Microsoft.Office.Interop.Outlook.MailItem'。这个操作   失败,因为QueryInterface调用了COM组件   与IID'{00063034-0000-0000-C000-000000000046}'的接口失败   出现以下错误:未支持此类接口(例外情况)   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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在投射的项目可能是不同类型 - ContactItem,AppointmentItem,MeetingItem,TaskItem。检查类型,然后投射并使用它。

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

-Vimal