gpt4 book ai didi

c# - 无法转换 COM 对象 - Microsoft outlook 和 C#

转载 作者:可可西里 更新时间:2023-11-01 03:13:41 25 4
gpt4 key购买 nike

我编写了这段代码来查看我的 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))."

你能帮我解决这个错误吗?

最佳答案

前阵子我不得不解决类似你的问题。

        foreach (Object _obj in _explorer.CurrentFolder.Items)
{
if (_obj is MailItem)
{
MyMailHandler((MailItem)_obj);
}
}

希望对您有所帮助。

这里的问题是 _explorer.CurrentFolder.Items 可以包含比 MailItem (PostItem 是其中之一)更多的对象。

关于c# - 无法转换 COM 对象 - Microsoft outlook 和 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4656360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com