How to close outlook application opened by another user - c#

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.

Related

Outlook API GetItemFromID() fails with error "Could not open the item.Try again." when using multiple outlook accounts

I am using two outlook accounts from the same exchange server (i.e. same domain). We have an outlook add-in which we use to archive some emails from outlook to an external web application. While composing a new mail, it's possible to attach some files from external application to the mail being composed using the add-in.
Now things work perfectly when I have a single account on outlook. But after configuring another account and trying to do the same from secondary account, outlook gives error:
Outlook Initiator exception:
System.Runtime.InteropServices.COMException (0x80040107): Could not
open the item. Try again. at
Microsoft.Office.Interop.Outlook.NameSpaceClass.GetItemFromID(String
EntryIDItem, Object EntryIDStore)
I tried to dig into the code and found that the call to GetItemFromID() gives the exception shown above.
object item = ns.GetItemFromID(objectID, storeId);
The first parameter objectID (which is EntryIDItem) is different for different accounts but second parameter storeId is same.
I also tried to see the body of the method GetItemFromID() using disassembler but the method is defined as an extern method that means it's defined outside of the current assembly as un-managed code.
Tried to search for anything similar to that on stackoverflow but no luck. Can anybody please help me on this?
Try to leave only the first parameter. The EntryIDStore parameter is optional. See NameSpace.GetItemFromID for more information.
Note, the Entry ID changes when an item is moved into another store, for example, from your Inbox to a Microsoft Exchange Server public folder, or from one Personal Folders (.pst) file to another .pst file. Solutions should not depend on the EntryID property to be unique unless items will not be moved.

OpenFileDialog window not showing

I have a problem with the OpenFileDialog from Windows Forms (it does not matter if I use is in a console application, win forms or wpf) (C#).
I have a small test project with a button that, when pressed, will ask the user to select an image (using OpenFileDialog) and send its path to a process method. The process method is in c++ native code and accessed using c++ cli (CLR). This method send a request to a web service and waits for the response (the web service is local, so the response is fast).
The problem is this: if I press the button 2 times (select an image + processing and after it's finished I process another image), the 3rd time the window will not show, it gets stuck at ShowDialog.
If I run it from WPF it gives me this error:
DisconnectedContext occurred
Message: Managed Debugging Assistant 'DisconnectedContext' has detected a problem in 'd:\Project\WpfApplication1.vshost.exe'.
Additional information: Transition into COM context 0x1b09d5d0 for this RuntimeCallableWrapper failed with the following error: The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED)). This is typically because the COM context 0x1b09d5d0 where this RuntimeCallableWrapper was created has been disconnected or it is busy doing something else and cannot process the context transition. No proxy will be used to service the request on the COM component and calls will be made to the COM component directly. This may cause corruption or data loss. To avoid this problem, please ensure that all COM contexts/apartments/threads stay alive and are available for context transition, until the application is completely done with the RuntimeCallableWrappers that represents COM components that live inside them.
Any ideas how to solve this or what it means ?
I found the problem (it was not from the webservice communication): Somewhere in my native code I was uninitializing COM, but .NET UI needs COM to be up and running.
So all I had to do is remove the unitialization of COM from my native code.

Error handling in C# for SAP GUI Logon (GuiSession)

I'm new to the SAP Gui API; but I manage to create a simple program (with C#) to automate data entry into SAP with reference from how-do-i-automate-sap-gui-with-c-sharp.
For the past few days I have tried to enhance the program by putting in error handling mechanism so that the program will able to show friendly message, if connection time or invalid logon info provided during logon. Catch all system/error messages from SAP and display them in my program.
However I have tried many ways to catch the error message via GuiSession.ErrorList and GuiSession.Error but it is not working - either return null or the program failed to compile.
Any help/advice on this will be appreciated. Thanks.

System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID

I am replacing MS Word MergeFields with data from SQL Server in a simple asp.net c# application and it works fine locally but when i publish it often i get the following:
System.Runtime.InteropServices.COMException (0x80080005): Retrieving the COM class factory for component with CLSID {00020906-0000-0000-C000-000000000046} failed due to the following error: 80080005.
I tried giving both IIS_IUSRS and NETWORK SERVICE full access, and also set the identity to interactive in the DCOM config but it didn't fix anything.
For nasty permissions stopping you from doing what you expect, (I've encountered in the past too). Sometimes I've had to change my DCOM/COM+ permissions to an object too. At the DCOM Config tab screen for your project, under security, make sure BOTH "Launch and Activation" and "Access" have been customized to explicitly include the user account you intend to allow.
Now, one more thing -- just to rule out all others. I've even gone the extent (for testing / confirmation only) is that on the "Identity" tab, you can select the "Interactive User", "Launching User" or "This User". I would put in your actual account that you KNOW has permissions (ensure your account is also in the launch and activation... and access permissions on previous tab). And also to enter/confirm your password.
If that still fails, then I would try adding the ADMIN account for permissions and try that... just for purposes of yes, its permissions based on a user. Once confirmed, you can scale it back, and even create some simplified restricted user who's primary purpose is to run this utility, and this new user has its adequate permissions to do so.
Once that is done, you should be good to go.
Now, that said, I've run into another flakey thing and its been a while, so it may not be an issue any more. Periodically, during certain "Windows" updates, the functionality would just stop working. However, if all I did was go back into user maintenance, do change password for the account (even if just keeping it the same original password) and saving it, then going back into DCOM and enter/re-confirm saving it there too, all became functional again.
You should share the folder where you save the document that is created from the template with the NETWORK SERVICE user.
Change the dcomcnfg identity to interactive instead of launching user.
Reference: Error : 80070005, there is something in this forum about
Retrieving the COM class factory for component with CLSID {7979FAA5-3A2A-4E50-A2C9-286C6F34C1D3} failed due to the following error: 80080005

Exchange WS 'The specified object was not found in the store.' error

We're using Exchange 2007 WS to process mail folders and are hitting various problems if we try and forward a message we've already received. Our process is:
Windows Service monitors mailbox folder, on finding a new mail we process the information and move the item to a 'Processed folder' and store the Exchange Message Id.
Users may opt to forward the mail externally. We use the Exchange API to find the item using the Message Id we stored earlier, and then again use the API to forward.
Except finding the mail again is proving rather flaky. We regularly get the following error:
The specified object was not found in the store.
Is there a better/more reliable way we can achieve the same? The documentation for Exchange WS is rather sparse.
This is a bug in microsoft exchange manage API. here is a link for more information
http://maheshde.blogspot.com/2010/09/exchange-web-service-specified-object.html
Are you saving the Message ID of the newly found message or the message once it has been moved to the 'Processed' folder? The id will change when it moves to a new folder.
The method recommended in the book Inside Microsoft Exchange Server 2007 Web Services is to grab the PR_SEARCH_KEY (0x300B, Binary) of the newly discovered item, then move it to the 'Processed' folder. You can then search for it in the new folder based on the PR_SEARCH_KEY and get it's new Message id to forward it.
I have come to the conclusion that this happens to me is because while my app is processing the emails, someone else is fiddling with an email at the same time.
So to cure the problem, I put it the code in a try catch and see if the exception is == the that object not found in store, if so I just skip it and move on to the next item. So for has no issues.
I wrote a program that reads the emails in inbox downloads attachments to the specified folder, wrote the email info and the saved path to the database, and finally deletes the email. I run this program as a windows service. After all tests are finished I run this program to the main server and run it. Program runs successfully but sometimes I got this error. I checked everything and finally I found that I forgot to stop service on my computer. 2 programs that runs on my computer and on real server checking the same mailbox at the same time. If you get this error make sure that only one program can process at the same mailbox.

Categories

Resources