【问题标题】:InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass'InvalidCastException - 无法转换类型为“Microsoft.Office.Interop.Outlook.ApplicationClass”的 COM 对象
【发布时间】:2015-01-08 09:00:04
【问题描述】:

我已经编写了这段代码来使用我的 .net 应用程序中的 Outlook 发送附件,代码如下:

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)).

【问题讨论】:

    标签: c# .net excel outlook


    【解决方案1】:

    你投错了对象

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

    你需要改变

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

    到:

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

    Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2020-12-02
      • 2011-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多