39

I have an application that calls

Email hello = new Email(appropriate constructor);
hello.Email_Send();

I'm receiving the exception:

Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

from System.Runtime.InteropServices.COMException.

using O = Microsoft.Office.Interop.Outlook;    
class Email
{
    public void Email_Send()
    {
        O.Application outlook = new O.Application(); //Exception thrown here.
        O.MailItem message = outlook.CreateItem(O.OlItemType.olMailItem);
        message.To = Receiver;
        message.CC = Sender;
        message.Subject = Subject;
        message.Body = "This is an automated message sent at " + DateTime.Now.ToString("HH:mm:ss") + " about " + Body_Topic + System.Environment.NewLine + Body_Content ;
        message.Send();
    }
}

This error has never happened previously, and there has been no change to the code that I know of. http://support.microsoft.com/kb/825118 doesn't seem to fit the my symptoms - My computer doesn't stop responding, etc. Any help diagnosing the issue would be greatly appreciated!

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Zee
  • 1,780
  • 3
  • 16
  • 27
  • 6
    This error was caused by visual studio being run as admin. Outlook doesn't allow separate users to access the same mailbox (I had the outlook application open on desktop). Even though I have local admin access w/ my domain user, running VS as admin must associate the process to a different user object? Not exactly sure how this works, but... Resolved. – Zee Oct 12 '12 at 15:31
  • Please post your comment as an answer so that others can benefit. – SliverNinja - MSFT Oct 13 '12 at 15:15
  • Does this answer your question? [Outlook COMException](https://stackoverflow.com/questions/6369689/outlook-comexception) – James John McGuire 'Jahmic' Jan 21 '23 at 10:08

5 Answers5

98

This error was caused by visual studio being run as admin. Outlook doesn't allow separate users to access the same mailbox (I had the outlook application open on desktop). Even though I have local admin access w/ my domain user, running VS as admin must associate the process to a different user object? Not exactly sure how this works, but... Resolved.

Zee
  • 1,780
  • 3
  • 16
  • 27
  • Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063001-0000-0000-C000-000000000046}' failed due to the following error: Library not registered. (Exception from HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED)). – Meer Jun 29 '16 at 05:17
  • 3
    When we close the running instance of Outlook it works like a charm. Now the question, which I presume need to go in another Stack Overflow question, is to know how to make it work even if Outlook is running. – Marc Roussel May 04 '17 at 00:57
  • I have the same issue. But it comes in Windows Service where I try to use Outlook COM – Rauf Jul 25 '17 at 14:38
  • I am facing this issue in my power shell script . My issue is solved by this solution . As I opened power shell script in admin mode . – Maximious Aug 06 '20 at 17:20
5

I ran into the same issue, and as previously said: if Visual Studio is running as Administrator then Outlook prevents another instance with a different user. My VS solution is starting several projects, and I need it to run as Administrator, so what I did is run Outlook as administrator while debugging. This solved my problem.

lukiller
  • 1,107
  • 9
  • 12
  • 1
    It is probably fine to run Outlook temporarily as admin. However, running Outlook as admin always is probably a bad idea. If you received some kind of malicious email/attachment which exploited a weakness in Outlook, then you may be granting that attacker admin privilege rather than lower privilege level on your machine. – Zee Feb 18 '19 at 20:00
0

Posting one of the comments as an answer.

When we close the running instance of Outlook it works like a charm.

In my situation I'm running under VMs with special security software. On my developer desktop it worked fine when Outlook was open.

But in my secured VM I had to close outlook in order to get this to work. This is likely to due our security software.

When outlook is left running: Unhandled Exception: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).

When it is not running, it works fine.

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
0

The accepted answer is not entirely accurate. Actually, the COM will fail if the calling application does NOT have the same access level as the original process. Both need to be at the same access level.

So, if Outlook was started as Administrator and the program being debugged in Visual Studio is also administrator, it will work.

Vica-versa: if Outlook was NOT started as Administrator, then the program being debugged in Visual Studio will also need to be NON-administrator.

Also, you can get this message if not also included a COMReference to Microsoft.Office.Core.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
0

It happened to me even when Visual Studio was closed. I believe it is because Outlook was still running.

I fixed it by using a bat file for the task, which closes Outlook before running the exe:

taskkill /F /im outlook.exe
sleep 5

C:\Project\RunProcess.exe

Also, in the task scheduler (Settings tab), I checked "If the running task does not end when requested, force it to stop".

It worked for me.

Oranit Dar
  • 1,539
  • 18
  • 17