Search code examples
outlookinteropoffice-interopmvcmailer

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);
            }
        }

Solution

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