我写了这个代码来查看我的Outlook邮箱中未读的项目,这里是代码:
Microsoft.Office.Interop.Outlook.Application app; Microsoft.Office.Interop.Outlook.Items items; Microsoft.Office.Interop.Outlook.NameSpace ns; Microsoft.Office.Interop.Outlook.MAPIFolder inBox; Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application(); app = application; ns = application.Session; inBox = ns.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInBox); items = inBox.Items; foreach (Microsoft.Office.Interop.Outlook.MailItem mail in items) { if (mail.UnRead == true) { MessageBox.Show(mail.Subject.ToString()); } }
但是在foreach循环中,我得到这个错误:
“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)).”
我不得不绕过像你这样的问题一样.
原文链接:https://www.f2er.com/windows/365465.htmlforeach (Object _obj in _explorer.CurrentFolder.Items) { if (_obj is MailItem) { MyMailHandler((MailItem)_obj); } }
希望有帮助.
这里的问题是_explorer.CurrentFolder.Items可以包含更多的对象,而不仅仅是MailItem(PostItem是其中之一).