开发者

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

开发者 https://www.devze.com 2023-03-14 18:45 出处:网络
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 worki

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.Applicati开发者_如何学Goon 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号