Dinesh Patil
Dinesh Patil

Reputation: 114

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

I am using below code in the button event, so that user can send mail through self machine outlook directly (nuget Microsoft. Office. Interop.Outlook). Code is working when I am debugging below code in my localhost and send mail from outlook. But problem is when I deployed the code into web server and browse through IE from my work station, mail not send through outlook.

This error message show in log:

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)).

How can I resolve this issue?

Web application reside into web server and users will access the application from IE and then they will send mail through self machine 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);
            }
        }

Upvotes: 0

Views: 766

Answers (3)

YaBo
YaBo

Reputation: 1

I've had this issue too."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))."

Server environment:Windows server 2019&iis Local machine:Windows 10&iis

Tip:The Microsoft office doesn't support that use OutWork IIS or Asp.net So,I give you right answer(It's worked): 1、Run "win+R" ,then inuput 'Dcomcnfg' 2、As this pic: enter image description here

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49397

The Considerations for server-side Automation of Office article states the following:

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.

As a possible workaround you may consider using EWS or any other REST API (for example, Graph API) if you deal with Exchange server profiles only. See Explore the EWS Managed API, EWS, and web services in Exchange for more information.

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66215

Outlook, just like every other Office app, cannot be used from a service (such as IIS).

Upvotes: 1

Related Questions