Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# InvalidCastException-无法强制转换类型为';Microsoft.Office.Interop.Outlook.ApplicationClass';_C#_.net_Excel_Outlook - Fatal编程技术网

C# InvalidCastException-无法强制转换类型为';Microsoft.Office.Interop.Outlook.ApplicationClass';

C# InvalidCastException-无法强制转换类型为';Microsoft.Office.Interop.Outlook.ApplicationClass';,c#,.net,excel,outlook,C#,.net,Excel,Outlook,我编写了此代码,以使用outlook从.net应用程序发送附加文件,代码如下: Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.Application session = new Microsoft.Office.Interop.Outlook

我编写了此代码,以使用outlook从.net应用程序发送附加文件,代码如下:

Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.Application session = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace ns = outlook.Session;
            Outlook.MailItem mail = outlook.Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
            mail.Subject = txtSubject.Text;
            mail.To = txtTo.Text;
            mail.Subject = txtSubject.Text;
            mail.Body = txtBody.Text;
            mail.Attachments.Add(@"c:\Users\admin\Desktop\Excel.txt",
                    Outlook.OlAttachmentType.olByValue, Type.Missing,
                    Type.Missing);
            Outlook.Accounts accounts = outlook.Session.Accounts;

            foreach (Outlook.Account account in accounts)
            {
                // When the e-mail address matches, send the mail.
                if (string.Equals(account.SmtpAddress, txtFrom.Text, StringComparison.OrdinalIgnoreCase))
                {
                    mail.SendUsingAccount = account;
                    mail.Save();
                    ((Outlook._MailItem)outlook).Send();
                    lblStatus.Text = "Report Sent";
                    break;
                }
            }
但是当它到达Send()方法调用时,我得到以下错误:

Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' 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)).

你投错了对象

outlook
定义为
Microsoft.Office.Interop.outlook.Application

你需要改变

((Outlook._MailItem)outlook).Send();
致:

((Outlook._MailItem)mail).Send();