I have a situation,I have an application which sends mail using OUTLOOK,problem is while I'm trying to send mails from the application which is opened as Administrator it throws 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)).
Is there any way to solve it programmatically?
You could call Process.GetProcessesByName()
passing in OUTLOOK
or whatever the actual process name is.
This gets you an array of process. You can then iterate through each of these and check the user that started the process. If the user isn't the ones you want you can call Process.Kill()
and terminate the process.
Getting the user might be tricky, you can use WMI for this (you could also use WMI to list the processes).
Check this answer here for getting the user name.
However I would rethink how you send your mail. The last thing I'd want as a user is watching my Outlook disappear because your application is trying to send an e-mail.
Perhaps a little late to the party on this one but there are two possible approaches here.
As highlighted by Lloyd, you can attempt to connect to an existing Outlook process for the 'current' user (if one exists) by calling
Process.GetProcessByName("OUTLOOK");
This gets an array of Outlook processes and you can iterate through these until the instance you're looking for.
Alternatively, (or if no process is found), you can initialize a new instance of the Outlook application, connect to the default MAPI namespace and send your email that way. Presumably you're using objects defined in the Microsoft.Office.Interop.Outlook library?
You may need to call the Logon/Logoff routines against the namespace and proceed that way, remembering to correctly dispose of any new instances after use, even acquiring the process ID of the new app instance and calling .Kill on that.
User contributions licensed under CC BY-SA 3.0