How to send and outlook email from windows server published web app using C# code behind


Hello I am tired of trying to understand what is causing this problem and fixing it with out any positive result I had to come to you to get some help.

The problem is the following I had a we app which sends a email using Microsoft.Office.Interop.Outlook.

When I am debuggin the app using vs iis expres it works fine i mean it sends its email perfectly fine.

How ever after I publish the app on a server I am getting the following error:

Server Error in '/' Application.
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)).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.UnauthorizedAccessException: 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)). 

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 

To grant ASP.NET access to a file, right-click the file in File Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

The method which triggers the error is the following:

using Outlook = Microsoft.Office.Interop.Outlook;

string rqstornt=BS.BSMaintenance.Instance.GetEmployeeNTUserByID(int.Parse(DdlSwapRequestor.SelectedValue));
                    string emailRqstor = BS.BSMaintenance.Instance.GetUserEmail(rqstornt);
                    string providernt= BS.BSMaintenance.Instance.GetEmployeeNTUserByID(int.Parse(DdlSwapProvider.SelectedValue));
                    string emailProvider = BS.BSMaintenance.Instance.GetUserEmail(providernt);
                    string requestor=DdlSwapRequestor.SelectedItem.Text;
                    string provider = DdlSwapProvider.SelectedItem.Text;
                    string startdate = DpStartDate.Text;
                    string enddate = DpEndDate.Text;

                    if (!string.IsNullOrEmpty(emailRqstor)&(!string.IsNullOrEmpty(emailProvider)))
                        Outlook.Application outlookApp = new Outlook.Application();
                        Outlook.MailItem mail = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                        mail.Subject = "Swap Request Date: "+DateTime.Now.ToString();
                        mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;

                        string swrequestor= HttpUtility.UrlEncode(Encrypt(requestor));
                        string swprovider = HttpUtility.UrlEncode(Encrypt(provider));
                        string swstdt = HttpUtility.UrlEncode(Encrypt(startdate));
                        string swedt = HttpUtility.UrlEncode(Encrypt(enddate));

                        mail.HTMLBody = "<h1>Hi</h1><br/>The employe: " + requestor + " is requesting schedule swap with " + provider + " from " + startdate + " to " + enddate + "<br/> If you accept the schedule swap please go to the link below in order to approve the swap.<br/><br/>" +
                        "Click the link to approve/reject it: "+ string.Format("{0}&prvd={1}&swstd={2}&swed={3}", swrequestor, swprovider, swstdt, swedt);

                        //mail.To = ";;;";
                        mail.To = ";";
                        mail.CC = emailRqstor+";"+emailProvider+";";

                        PnlError.Visible = false;
                        PnlSucess.Visible = true;
                        LbMsjSucess.Text = "You have sucessfully requested the a scheduled swap. ";

Hope the code and error I provided you is enough so you can understand what is the cause of this problem and that you can help me to resolve it.

Thanks in advance for all your help.

asked on Stack Overflow Mar 7, 2019 by C-Zar • edited Mar 11, 2019 by Tab Alleman

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0