NESHOM
NESHOM

Reputation: 929

Creating an Outlook application/mail item without administrator right using a C# application that has administrator privilage

I have written a C# code that needs to have administrator right. Using this application, I am trying to create an Outlook application/mail item in order to open an outlook compose window. However, Outlook 2013 is already running without administrator right. I am using the following code, but since the created application/mail item and the already running instance of outlook have different privileges, it raises exception.

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
Microsoft.Office.Interop.Outlook.MailItem mailItem = app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

Is there anyway to create the application/mail item as current user instead of administrator (something similar to running a process as current user by providing username and password of the current user)?

Outlook needs to be running without administrator right in order to have a functional indexing service.

Here is the exception details:

System.Runtime.InteropServices.COMException was unhandled
HResult=-2146959355
Message=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)).
Source=mscorlib
ErrorCode=-2146959355

Upvotes: 0

Views: 1232

Answers (3)

NESHOM
NESHOM

Reputation: 929

One solution I came up with is to execute a secondary C# application as current user by providing username and password of the current user. This application is then responsible for creating Outlook mail/application item and opening compose message. This way the already running Outlook and the created mail/application items are both without admin right.

I simplified this further and even avoided using any secondary C# application by simply re-executing my original C# application but without admin right and passing input argument to it to create Outlook mail/application items.

This seems to be working perfectly.

Upvotes: 1

Eugene Astafiev
Eugene Astafiev

Reputation: 49455

You need to run Outlook with admin privileges if you need to access a running instance from your application. The security context should correspond to your application.

Anyway, you may consider using Exchange Web Services instead. See EWS Managed API, EWS, and web services in Exchange for more information. Also you may consider using .net BCL for creating and sending emails.

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

No, if the security contexts are different, COM marshaling would explicitly refuse to work. This is a security feature designed to avoid a situation when security rights are elevated without an explicit user consent.

Upvotes: 2

Related Questions