Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 798
Jason Avatar asked Jun 23 '11 14:06

Jason


People also ask

Where is Outlook exe in C drive?

As we all know Outlook comes as a part of the Microsoft Office suite or Microsoft Office 365 suite. Microsoft Office applications are usually present in C:\Program Files\Microsoft Office or C:\Program Files (x86)\Microsoft Office directory.

How do I allow Outlook to run in the background?

When you launch Microsoft Outlook you will see the Microsoft Outlook icon in the system tray. Right-click on it and in the menu you need to click on “Hide when minimized”. Now instead of closing the application minimize it and it will start running in the background.


1 Answers

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.

like image 197
Jahmic Avatar answered Oct 20 '22 03:10

Jahmic