【问题标题】:I got an InvalidCastException error while trying to send a Email in C#尝试在 C# 中发送电子邮件时出现 InvalidCastException 错误
【发布时间】:2016-12-11 20:13:21
【问题描述】:

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

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

我收到以下错误:

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

【问题讨论】:

  • 您是使用 Outlook 发送电子邮件,还是使用网络服务器?如果您还是从服务器发送它,为什么不直接删除 Outlook 中间人?
  • 也许this question 可能有帮助?
  • Takarii..感谢您的回复。我正在使用 Outlook 向另一个 Outlook 地址发送邮件。从展望到展望

标签: c# winforms


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2018-05-16
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多