Microsoft.Office.Interop.Outlook 不能在 Web 服务器上运行,但可以在本地计算机上运行

Microsoft.Office.Interop.Outlook does not work on web server but works on local machine

我在按钮事件中使用了下面的代码,以便用户可以直接通过自己的机器 outlook 发送邮件 (nuget Microsoft.Office.Interop.Outlook)。当我在本地主机中调试以下代码并从 outlook 发送邮件时,代码正在运行。但问题是当我将代码部署到 Web 服务器并从我的工作站通过 IE 浏览时,邮件无法通过 Outlook 发送。

此错误消息显示在日志中:

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

我该如何解决这个问题?

Web应用程序驻留在Web服务器中,用户通过IE访问应用程序,然后通过自己的机器outlook发送邮件。

 public void SendEmailOutlook(string mailToRecipients, string mailCCRecipients, string subjectLine, [Optional] string attachments, string HTMLBody)
        {
            try
            {
                Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
                Microsoft.Office.Interop.Outlook.MailItem oMsg = oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
                Outlook.Recipients oRecips = oMsg.Recipients;
                List<string> oTORecip = new List<string>();
                List<string> oCCRecip = new List<string>();
                var ToRecip = mailToRecipients.Split(',');
                var CCRecip = mailCCRecipients.Split(',');
                foreach (string ToRecipient in ToRecip)
                {
                    oTORecip.Add(ToRecipient);
                }
                foreach (string CCRecipient in CCRecip)
                {
                    oCCRecip.Add(CCRecipient);
                }
                foreach (string to in oTORecip)
                {
                    Outlook.Recipient oTORecipt = oRecips.Add(to);
                    oTORecipt.Type = (int)Outlook.OlMailRecipientType.olTo;
                    oTORecipt.Resolve();
                }
                foreach (string cc in oCCRecip)
                {
                    Outlook.Recipient oCCRecipt = oRecips.Add(cc);
                    oCCRecipt.Type = (int)Outlook.OlMailRecipientType.olCC;
                    oCCRecipt.Resolve();
                }
                oMsg.Subject = subjectLine;
                if (attachments.Length > 0)
                {
                    string sDisplayName = "MyAttachment";
                    int iPosition = 1;
                    int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
                    var Sendattachments = attachments.Split(',');
                    foreach (var attachment in Sendattachments)
                    {
                        Outlook.Attachment oAttach = oMsg.Attachments.Add(attachment, iAttachType, iPosition, sDisplayName);
                    }
                }
                if (HTMLBody.Length > 0)
                {
                    oMsg.HTMLBody = HTMLBody;
                }
                oMsg.Save();
                oMsg.Send();
                oTORecip = null;
                oCCRecip = null;
                oMsg = null;
                oApp = null;
            }
            catch (Exception e)
            {
              //print(e.Message);
            }
        }

Outlook 与所有其他 Office 应用程序一样,无法通过服务(例如 IIS)使用。

Considerations for server-side Automation of Office 文章陈述如下:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

作为一种可能的解决方法,如果您只处理 Exchange 服务器配置文件,您可以考虑使用 EWS 或任何其他 REST API(例如,Graph API)。有关详细信息,请参阅 Explore the EWS Managed API, EWS, and web services in Exchange