0
votes

I am creating a button in asp.net c# that when clicked will open up Outlook window.

I am referencing to the Microsoft.Office.Interop.Outlook dll, and using this in using statement:

using Outlook = Microsoft.Office.Interop.Outlook;

This the code.

        private void CreateMailItem()            
        {
            try

            {
                var outlookApp = new Outlook.Application();

                var mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);

                //var mailItem = (Outlook.MailItem)
                //    Application.CreateItem(Outlook.OlItemType.olMailItem);
                mailItem.Subject = "This is the subject";
                mailItem.To = "[email protected]";
                mailItem.Body = "This is the message.";
                mailItem.Importance = Outlook.OlImportance.olImportanceLow;
                mailItem.Display(false);
            }
            catch (Exception)
            {

                throw;
            }           
        }

I get error on the very first line, var outlookApp = new Outlook.Application(); The exception says:

{"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))."}

4
What does exception say?Imad
Sorry. Forgot to add the exception. Will edit the text. It says: {"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))."}Ilyas
Similar office issue here. Are you trying to send the email from the server or the client? If it's from the server you might be better off doing this over SMTP.Phil
I need to send it from the client, but right now I am testing on my development server where I have installed Outlook (2010)Ilyas

4 Answers

0
votes

The exception you've posted is thrown when a referenced dll or depencies of this dll are not correctly installed.

In this case, it seems outlook or office is not with the correct version which you referenced, on your test machine?

0
votes

Can't post this as comment.

I would like to know why you prefer the use of Outlook Interop? I am using the mailto:// protocol if I wanted my program to send email on the user's current email client, though I use this on WinForms.

like http://www.rapidtables.com/web/html/mailto.htm

0
votes

Outlook, just like any Office app, cannot be used from a service (such as IIS). Even if you did make it work, the new message window will be displayed on the server where the user will not see it anyway.

You can try to run a client-side JavaScritp code, but then you'd be limited IE only, Outlook would need to be locally installed, and your site must be trusted to be able to create COM objects in a script.

0
votes

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. Read more about that in the Considerations for server-side Automation of Office article.

Consider using System.Net.Mail namespace for creating and sending emails in ASP.NET.