简体   繁体   English

C# outlook: Unable to cast COM object of type 'System._ComObject' to Interface type 'Microsoft.Office.Interop.Outlook.MailItem.'

[英]C# outlook : Unable to cast COM object of type 'System._ComObject' to Interface type 'Microsoft.Office.Interop.Outlook.MailItem.'

I'm new to c#.我是 c# 的新手。

I have been trying on my own to fix this issue since 1 week until i've got no other alternative than to ask for help here.自 1 周以来,我一直在尝试自己解决此问题,直到我别无选择,只能在这里寻求帮助。

My goal is to create a console app which would look for Unread Mails on the secondary mailbox[mailboxB@gmail.com]/second outlook account and reply those mails based on the Subject.我的目标是创建一个控制台应用程序,它将在辅助邮箱 [mailboxB@gmail.com]/second outlook 帐户上查找未读邮件并根据主题回复这些邮件。

The code works fine on my Machine .该代码在我的 Machine 上运行良好

Unfortunately when the same exe is deployed on other machine , the following error gets generated:不幸的是,当在其他机器上部署相同的 exe 时,会生成以下错误:

unable to cast com object of 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)) unable to cast com object of 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出现以下错误:不支持此类接口(来自 HRESULT 的异常:0x80004002 (E_NOINTERFACE))

Please note i've tried the following to resolve the issue but the issue still prevails:请注意,我已尝试以下方法来解决问题,但问题仍然存在:

  1. Restarted Outlook as administrator.以管理员身份重新启动 Outlook。

    Change the compatibility of the app under properties-> compatibility to run as administrator在属性->兼容性下更改应用程序的兼容性以管理员身份运行

  2. Checked the outlook version which is 16.0 and the same ref is being used for in the code.检查了 outlook 版本,它是 16.0,并且在代码中使用了相同的 ref。

Please find my code below:请在下面找到我的代码:

            Outlook.Application oApp = new Outlook.Application();
            Outlook.NameSpace oNS = (Outlook.NameSpace)oApp.GetNamespace("MAPI");
            
            MAPIFolder theInbox = oNS.Folders["mailboxB@gmail.com"].Folders["Inbox"];
            
            Outlook.Items unreadItems = theInbox.Items.Restrict("[Unread]=true");
            string t = "Test Mail";
            try
            {
                foreach (Outlook.MailItem it in unreadItems)
                {
                    if (it.Subject == t)
                    {
                        Outlook.MailItem replyMail = it.Reply();
                        it.HTMLBody = DateTime.Now.ToString("HH:mm:ss");
                        
                        
                        replyMail.Send();
                        it.Unread=false;
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

Any sort of help would be highly appreciated.任何形式的帮助将不胜感激。

You can have items other than MailItem in your Inbox folder, eg ReportItem or MeetingItem .您的收件箱文件夹中可以包含MailItem以外的项目,例如ReportItemMeetingItem

Change your code to将您的代码更改为

foreach (object obj in unreadItems)
{
   if (obj is Outlook.MailItem it)
   {
      ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“System._ComObject”类型的 COM object 转换为“microsoft.Office.Interop.Excel.Application”” - unable to cast COM object of type 'System._ComObject' to 'microsoft.Office.Interop.Excel.Application'" 无法将类型为“System.__ComObject”的 COM 对象转换为接口类型“Interop.PeachwServer.Application” - Sage SDK 2018 CreateCustomerDemo c# - Unable to cast COM object of type 'System.__ComObject' to interface type 'Interop.PeachwServer.Application' - Sage SDK 2018 CreateCustomerDemo c# 无法将类型为“ System.String”的对象转换为类型为“ Microsoft.Office.Interop.Outlook.Store” - Unable to cast object of type 'System.String' to type 'Microsoft.Office.Interop.Outlook.Store InvalidCastException-无法转换类型为“ Microsoft.Office.Interop.Outlook.ApplicationClass”的COM对象 - InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' 在C#中加载COM对象抛出异常“无法将类型为'System .__ ComObject'的COM对象转换为接口类型...”,但C ++或VB没有 - Loading COM object in C# throws exception “Unable to cast COM object of type 'System.__ComObject' to interface type …”, but C++ or VB not :'无法将类型为'System .__ ComObject'的COM对象转换为接口类型 - : 'Unable to cast COM object of type 'System.__ComObject' to interface type 如何修复“无法转换类型为‘Microsoft.Office.Interop.Outlook.ApplicationClass’的 COM 对象/RPC 服务器不可用”? - How can I fix “Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' / The RPC server is unavailable”? Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' {00063001-0000-0000-C000-000000000046} HRESULT: 0x80040155 - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' {00063001-0000-0000-C000-000000000046} HRESULT: 0x80040155 C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' - C#: some of machines are throwing following error: Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' 无法将类型'System .__ ComObject'的COM对象转换为IRTDUpdateEvent的接口类型 - Unable to cast COM object of type 'System.__ComObject' to interface type for IRTDUpdateEvent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM