How to connect to outlook 2010 while its running in c#?

无人久伴 提交于 2019-12-06 19:05:02

问题


What I am trying to do is add an "Email To..." button to a winform client that opens a new outlook mail window and attaches a file so the user can forward it. I can get the outlook integration working just fine if outlook is not already running. This is a C# .NET 4.0 winforms app, using the Outlook 14.0 interop library, against Outlook 2010 32 bit running on windows 7 64 bit machine. I have the app already compiled to x86 for other reasons so I doubt its a 32/64 bit issue. Here is my code:

// Connect to outlook and create a new mail item
var app = new Outlook.Application();
var ns = app.GetNamespace("MAPI");
var mailItem = (Outlook.MailItem)ns.Application.CreateItem(Outlook.OlItemType.olMailItem);

// create the mail item and attach the file
mailItem.To = "";
mailItem.Subject = "Emailing: " + Path.GetFileName(_currentFilePath);
mailItem.Attachments.Add(_currentFilePath, Outlook.OlAttachmentType.olEmbeddeditem);

// show the email dialog window
mailItem.Display(true);

If outlook is not running, it works flawlessly. Once its open, I get the following error on the very first line where it tries to create the Outlook.Application object:

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)).

Any ideas what would cause this? Is this a version conflict of some sort?


回答1:


This is due to privileges of the process. I usually run Visual studio as administrator, but if outlook is not previously started as also admin, the COM call will fail.

Simple solution. Run both as administrator or run both as normal privilege level.




回答2:


I had the same problem. It is an security issue. When you run Outlook as administrator (Shift Right Click). The problem is no longer there. Disabling user account control setting might solve it.



来源:https://stackoverflow.com/questions/6455448/how-to-connect-to-outlook-2010-while-its-running-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!