Click here to Skip to main content
15,889,654 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:

C#
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.


C#
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();
Posted
Updated 3-Aug-16 4:20am
v2
Comments
Richard MacCutchan 2-Aug-16 4:45am    
The error message is telling you what needs to be done. But you really need to understand the restrictions on web sites and servers.
planetz 2-Aug-16 5:05am    
I have given full permission to iis users from outlook command button control properties in DCOM config, as I saw in another solution. But still it is not working. I am not aware of restrictions of sending mail over web. It is working fine in debugging mode.
F-ES Sitecore 2-Aug-16 5:12am    
Your code is running on the server so it is trying to send email from the server's Outlook profile, not from the person using the site. Chances are you have not set up the Outlook profile from the server, and chances are this is not what you're wanting either, you probably want the email to be sent from the client's Outlook. You can't do that and you can't automate Outlook from your asp.net code, you need a different solution like sending email via your .net code and the SmtpClient, or using Exchange services if your client's are using Exchange. You have to remember that outlook is a client application and your server code can manipulate it no more than it can Word or notepad.
planetz 2-Aug-16 5:25am    
I want to send a mail to user when user clicks a button. That's all. If there is any other way apart from outlook, I'll be glad to try it.

1 solution

As mentioned in comments, your code is running on a server and so Outlook would have to be setup on the server with a profile, etc. But this is a terrible idea, so, you asked for another way. Sending SMTP email is very easy in ASP.Net. There are tons and tons of examples online. Here is the first result when I search google, How to send email in ASP.NET C# - Stack Overflow[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900