Calling the Outlook COM library using xp_cmdshell from within SQL server - c#

First off, forgive me if this question appears amateur or has been answered in the past - I'm pretty new to programming in general and have tried to search for an answer.
I'm trying to write an executable that will access an Outlook mailbox, pick up an unread email and insert its details into a database for our records.
I've written a proof of concept application that I can run fine but when I try and call it through a stored procedure in Sql server I hit the error message:
Unhandled Exception: 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_
I imagine I'm hitting a security issue along the way but maybe, due to my inexperience, this simply isn't possible with Microsoft security settings?
So far I've tried:
-Using regsvr32 to register the .dll
-Changing the permissions of the .dll
-Setting up a Sql server proxy account that is the same login as the one I use to access the VM where the server is hosted
-Trying to run it using both late and early bound methods
Any advice would be greatly appreciated as I'm pretty stumped!
Thanks in advance guys.

Just because something can be done, doesn't mean it should :) Going from SQL Server to Outlook via xp_cmdshell has so many possibilities for failure that I wouldn't even contemplate it. You might get it working on one machine then find that you just can't get it working on another.
The simple way to do this would be a small .Net app; query Outlook + write to database. If you want it to work on a continual basis either use a scheduler or create a Windows service to perform the task every n minutes.

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.
I'd suggest choosing another model of calling/working with Outlook. For example, you can automate Outlook from a standalone application. See How to automate Outlook from another program for more information. Also you may consider developing an Outlook add-in which can synchronize emails on the fly (when they arrives).

Related

Excel interop error- while running from windows service. Microsoft excel cannot open or save any more doc

Hi I have created windows service c# to create an excel file. When I scheduled and running under my id in dev environment it is working fine. But when i run the same service with different interactive Id which is also an administrator account in the server, the service is failing at the line Excel.WORKBOOK.saveas(object) with the below error
"Microsoft Excel cannot open or save any more documents because there is not enough available memory or disk space." I have tried many solutions provided in net to provide permissions in comconfig and to create desktop folders in c:\windows\sys32 and syswow64.
But still unable to find it.. only with my id the service is running fine but any other interactive if it is failing. Please help
The Considerations for server-side Automation of Office article states the following:
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.
I'd suggest using the Welcome to the Open XML SDK 2.5 for Office if you deal only with open XML documents, otherwise you may consider using third-party components designed for the server-side execution.

C# Outlook Retrieving the COM class factory for component with CLSID failed due to the following error: 80080005

I am getting the below error when I try to access Outlook folder to download an attachment from my inbox.
My code working perfectly when I run from Windows Form application. But when I use Windows Service to auto scan the Outlook folder, I am getting this issue.
*PollingService encountered an error 'Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80080005.'
For more information, see Help and Support Center at http://go.microsoft.com/fwlink/events.asp.*
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.
As a workaround you may consider using a low-level API on which Outlook is built - Extended MAPI or any other third-party wrapper around that API (such as Redemption).

Retrieving the COM class factory for component with CLSID error: 80070005 Access is denied

I am trying to use my asp.net application to open the outlook. It is working fine on my machine. now i am deploying the app to the server. i got this exception
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)).
I have been trying to solve this exception for three days. i googled a lot.
What I tried
they said that i have to go to DCOM CONFIG, i went there but I didn't find the microsoft office outlook entry.
then i opened a cmd and I typed mcc -32 and then i added the component server. after that i saw the outlook entry. i went to the property and then to the security tab, then i change the three of the options there to give access to the everyone user. but i kept having this problem
Note that I did that changes to these two entries
Microsoft Outlook Commadn Button
outlook message attachment
then i changed the user of the application bool to the Administrator account of the server, but i kept having the problem
please help me, i tried a lot really a lot to solve , but i couldn't
I am trying to use my asp.net application to open the outlook.
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.
As a workaround you can use the low-level API - Extended MAPI. Or any other third-party wrappers around that API (for example, Redemption).

Using Microsoft.Office.Interop.Outlook without Outlook Client installed

I'm trying to enumerate Outlook Global Address List and I don't have Outlook Client installed on a development server.
I'm getting following error
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
Additional information: Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
on the following line
Application app = new Microsoft.Office.Interop.Outlook.Application();
I assume it's because Outlook Client is not installed. Is it possible to use Microsoft.Office.Interop.Outlook without it?
Anton,
I assume it's because Outlook Client is not installed. Is it possible to use Microsoft.Office.Interop.Outlook without it?
Interop files are used for marshalling calls between managed/unmanaged boundaries. There is no the unmananged side in your case (where to marshal the call).
As already was suggested, you can use EWS instead. See EWS Managed API, EWS, and web services in Exchange for more information.
I don't have Outlook Client installed on a development server.
Moreover, 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.
You can read more about that in the Considerations for server-side Automation of Office article.
It is not possible to use the Interop assemblies without its associated application installed where you need to use it. The Interop assemblies are used primarily as an advanced application automation system.
If you are using Exchange Server 2007 or later, you could consider using the technique described in this article which uses Exchange Web Services (EWS) to access the Global Address List instead: Querying the Global Address List (GAL) via Exchange Web Services (EWS)

DCOM Failure of Office Automation

I developed a C# program that pulls data from a SQL Server database and then generates Word documents which include the data. I've set the program up to run as part of an SSIS job on a Windows Server 2003 box. The program runs as user SQLSVC which doesn't have administrative privileges. When I log in as SQLSVC and run the program it executes correctly. However, when the program is run automatically as an SSIS job it freezes at the following line of code
Microsoft.Office.Interop.Word.Application word =
new Microsoft.Office.Interop.Word.Application();
When I kill the process forcibly, the following error message is generated.
Error Message: System.Runtime.InteropServices.COMException (0x80080005):
Retrieving the COM class factory for component
with CLSID {000209FF-0000-0000-C000-000000000046}
failed due to the following error: 80080005.
at IepGlance.Program.CreateNewIepFiles(Dictionary`2
iepDictionary, EasyIepDataContext dbContext)
at IepGlance.Program.Main(String[] args)
As far as I can figure out the problem is with DCOM permissions. I've used dcomcnfg to add all possible DCOM permissions to user SQLSVC, but this hasn't helped. Are there any other possible solutions?
Run program "dcomcnfg -32". ( You cant find the word and excel components if it runs under 64bit)
Go to "Console Root/Component Services/Computer/My Computer/DCOM Config/" Look up Microsoft Word and Excel and choose properties.
Go to Security and select "Customize" under "Configuration Permissions. (If needed you might want to change the other permissions as well, but I didn't need to) Add "IIS_IUSRS" and give it "Full Control".
Now go to "Identity" and select "The interactive user".
Couple of things here.
First, this thread on SO may help.
Second, Microsoft warns that 'complications' may arise when you automate Office through an unattended process. Here's the punchline (bolded in the original):
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.
The linked article discusses workarounds.
It depends whether you are on a 64-bit machine and if you are 32-bit Office or 64-bit office, but in either scenario you need to go into the Admin Tools > Component Services and under DCOM config you need to find 'Microsoft Word 97 - 2003 Document', and then set the 'identity' to the user that is running your app pool. or simply disable the security on it.
If you are running a 64-bit server with 32-bit Office, you may have trouble finding this DCOM setting. You will need to run "MMC -32" from the command prompt to launch 32-bit MMC and then add 'Component Services' from the 'File > Add / Remove Snap-ins' menu.
Hope this helped
One point from my side,
I was scheduling the batch job in one of our server and it was throwing the same error whereas it was running in my local.
I tried all of the above steps but in my case actual scenario was different.
Whenever schedule the batchjob, do not select the option [Run whether user is logged on or not] because this requires the admin access and if you are not, you are in problem.

Categories

Resources