在c#中运行时如何连接到Outlook 2010? [英] How to connect to outlook 2010 while its running in c#?

查看:194
本文介绍了在c#中运行时如何连接到Outlook 2010?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是向Winform客户端添加一个电子邮件至..."按钮,该按钮将打开一个新的Outlook邮件窗口并附加一个文件,以便用户可以转发它.如果没有运行Outlook,我可以使Outlook集成正常工作.这是一个C#.NET 4.0 Winforms应用程序,使用Outlook 14.0互操作库,针对在Windows 7 64位计算机上运行的Outlook 2010 32位.由于其他原因,我已经将该应用程序编译为x86,因此我怀疑它是32/64位问题.这是我的代码:

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

如果Outlook没有运行,则它可以正常工作.一旦打开,我会在尝试创建Outlook.Application对象的第一行收到以下错误:

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:

检索COM类工厂以获取 具有CLSID的组件 {0006F03A-0000-0000-C000-000000000046} 由于以下错误而失败: 80080005服务器执行失败 (来自HRESULT的异常:0x80080005 (CO_E_SERVER_EXEC_FAILURE).

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?

推荐答案

这是由于该进程的特权所致.我通常以管理员身份运行Visual Studio,但是如果以前未同时以admin身份启动Outlook,则COM调用将失败.

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.

简单的解决方案. 以管理员身份运行或以普通特权级别运行.

这篇关于在c#中运行时如何连接到Outlook 2010?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆