Hi,
I am trying to send outlook mail from a c# web application. When I am debugging the code, it runs fine. I have even used same for console application and it works fine too.
But when I deploy the application on IIS, I get this error:
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.
Source Error:
Line 26: protected void Button1_Click(object sender, EventArgs e)
Line 27: {
Line 28: Microsoft.Office.Interop.Outlook.Application olapp = new Microsoft.Office.Interop.Outlook.Application();
Line 29: Microsoft.Office.Interop.Outlook.MailItem msg = (Microsoft.Office.Interop.Outlook.MailItem)olapp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
Line 30: string body = "";
Source File: D:\SOFTWARE APPLICATIONS\SCORE\SCORE\WebForm1.aspx.cs Line: 28
Stack Trace:
[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)).]
System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType) +0
System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType) +79
System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj) +76
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
System.Activator.CreateInstance(Type type) +66
SCORE.WebForm1.Button1_Click(Object sender, EventArgs e) in D:\SOFTWARE APPLICATIONS\SCORE\SCORE\WebForm1.aspx.cs:28
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628442
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34280
All I understand is there is a failure to load COM object which isn't happening in debugging. So what can be the possible reason for this? Any solution or work around will do.
Thanks..!!
What I have tried:
Somehow below code not working after running from IIS.
Microsoft.Office.Interop.Outlook.Application olapp = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem msg = (Microsoft.Office.Interop.Outlook.MailItem)olapp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
string body = "";
msg.Subject = "new message";
msg.To = "xxxx@yyy.com";
body = "test msg";
msg.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;
msg.HTMLBody = body;
msg.Display();
msg.Send();