繁体   English   中英

尝试在C#中发送电子邮件时出现InvalidCastException错误

[英]I got an InvalidCastException error while trying to send a Email in C#

我想从我的C#(winform)应用程序发送一封电子邮件到地址展望但我在下面的行中遇到错误:

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

我收到以下错误:

无法将“Microsoft.Office.Interop.Outlook.ApplicationClass”类型的COM对象强制转换为接口类型“Microsoft.Office.Interop.Outlook._Application”。 此操作失败,因为由于以下错误,对IID为“{00063001-0000-0000-C000-000000000046}”的接口的COM组件的QueryInterface调用失败:库未注册。 (Exception 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;
            }
        }

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM