我在尝试用C#发送电子邮件时遇到InvalidCastException错误

本文关键字:电子邮件 遇到 InvalidCastException 错误 | 更新日期: 2023-09-27 18:00:16

我正试图从我的C#(winform)应用程序向地址展望发送一封电子邮件,但我在下面的行中收到了一个错误:

Outlook.MailItem mail =(Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);

我得到以下错误:

无法强制转换类型为的COM对象"微软。办公室Interop。见解ApplicationClass'到接口类型"微软。办公室Interop。Outlook_应用程序"。此操作失败因为接口的COM组件上的QueryInterface调用IID"{00063001-0000-0000-C000-000000000046}"由于以下错误:库未注册。(异常de HRESULT:0x80040155)

我的完整代码是:

void SendEmailAuto()
        {
            try
            {
                OpenFileDialog attachment = new OpenFileDialog();
                attachment.Title = "Select a file to send";
                attachment.ShowDialog();
                Outlook._Application _app = new Outlook.Application();
                Outlook.MailItem mail =mail= (Outlook.MailItem)_app.CreateItem(Outlook.OlItemType.olMailItem);
                mail.To = 'paul.m@adt.org";
                mail.Subject = "Text";
                mail.Body = "Funding Request Team";
                if (attachment.FileName.Length > 0)
                {
                    mail.Attachments.Add(attachment.FileName, Outlook.OlAttachmentType.olByValue, 1, attachment.FileName);
                    mail.Importance = Outlook.OlImportance.olImportanceHigh;
                    ((Outlook.MailItem)mail).Send();
                }
                else
                {
                    MessageBox.Show("You are kindly requested to attach a document.", "MISSING FILE", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
            }
        }

我在尝试用C#发送电子邮件时遇到InvalidCastException错误

请检查:http://www.fieldstonsoftware.com/support/support_gsyncit_2013.shtml,这可能是注册表问题。