【问题标题】:Issue launching Outlook using Office Interop使用 Office 互操作启动 Outlook 的问题
【发布时间】:2019-08-14 07:44:31
【问题描述】:

我正在用 C# 构建一个 ASP webforms 应用程序,我正在尝试启动 Outlook 以从客户端计算机发送电子邮件。

我正在使用以下示例,这是我在网上找到的。

    public void sendEmail(object sender, EventArgs e)

{

        // Create the Outlook application.
        Outlook.Application oApp = new Outlook.Application();

        // Get the MAPI namespace.
        Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

        // Log on by using the default profile or existing session (no dialog box).
        oNS.Logon(Missing.Value, Missing.Value, false, true);

        // Create a new mail item.
        Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);

        // Set HTMLBody. 
        oMsg.HTMLBody = "Test";

        //Subject line
        oMsg.Subject = "Test Subject";

        // Add a recipient.
        Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;

        // Change the recipient in the next line if necessary.
        Outlook.Recipient oRecip;

        oRecip = (Outlook.Recipient)oRecips.Add("yyyyy@mydomain.co.uk");
        oRecip.Resolve();

        // Send.
        oMsg.Send();

        //Log off.
        oNS.Logoff();
    }

我在这行代码中遇到问题:

oNS.Logon(Missing.Value, Missing.Value, false, true);

即使我配置了 Outlook 配置文件,当它运行这行代码时,我的应用也会启动 Outlook 并显示“欢迎使用 Outlook 2016”对话框。

预期的行为是它使用现有配置文件启动 Outlook。

如果 Outlook 已经打开,我会收到一条错误消息:

System.Runtime.InteropServices.COMException: '正在检索 COM 类 具有 CLSID 的组件的工厂 {0006F03A-0000-0000-C000-000000000046} 由于以下原因而失败 错误:80080005 服务器执行失败(来自 HRESULT 的异常: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))。'

如果 Outlook 已打开,则预期行为是让我的应用程序使用现有的 Outlook 进程。

非常感谢任何帮助。

【问题讨论】:

    标签: c# asp.net outlook office-interop


    【解决方案1】:

    Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时出现的行为和/或死锁。

    如果您要构建在服务器端上下文中运行的解决方案,您应该尝试使用已确保无人值守执行安全的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方案。如果您使用服务器端解决方案中的 Office 应用程序,该应用程序将缺少许多成功运行所需的功能。此外,您将承担整体解决方案稳定性的风险。在Considerations for server-side Automation of Office 文章中了解更多信息。

    作为一种可能的解决方法,您可以考虑使用 Outlook 所基于的低级 API - 扩展 MAPI。或者只是围绕该 API 的任何包装器,例如 Redemption。

    如果您只处理 Exchange 配置文件,您可以考虑使用 Exchange Web 服务 (EWS),请参阅 Start using web services in Exchange

    【讨论】:

      猜你喜欢
      • 2013-03-27
      • 2011-08-23
      • 2013-06-09
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 2011-09-26
      相关资源
      最近更新 更多