C# 为什么我会收到来自 Office 的 Outlook 库的异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12861072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:43:43  来源:igfitidea点击:

Why am I receiving exception from Office's Outlook library?

c#outlookoffice-interop

提问by Zee

I have an application that calls

我有一个应用程序调用

Email hello = new Email(appropriate constructor);
hello.Email_Send();

I'm receiving the 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)).

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

from System.Runtime.InteropServices.COMException.

System.Runtime.InteropServices.COMException.

using O = Microsoft.Office.Interop.Outlook;    
class Email
{
    public void Email_Send()
    {
        O.Application outlook = new O.Application(); //Exception thrown here.
        O.MailItem message = outlook.CreateItem(O.OlItemType.olMailItem);
        message.To = Receiver;
        message.CC = Sender;
        message.Subject = Subject;
        message.Body = "This is an automated message sent at " + DateTime.Now.ToString("HH:mm:ss") + " about " + Body_Topic + System.Environment.NewLine + Body_Content ;
        message.Send();
    }
}

This error has never happened previously, and there has been no change to the code that I know of. http://support.microsoft.com/kb/825118doesn't seem to fit the my symptoms - My computer doesn't stop responding, etc. Any help diagnosing the issue would be greatly appreciated!

这个错误以前从未发生过,我所知道的代码也没有改变。http://support.microsoft.com/kb/825118似乎不符合我的症状 - 我的电脑没有停止响应等。任何诊断问题的帮助将不胜感激!

采纳答案by Zee

This error was caused by visual studio being run as admin. Outlook doesn't allow separate users to access the same mailbox (I had the outlook application open on desktop). Even though I have local admin access w/ my domain user, running VS as admin must associate the process to a different user object? Not exactly sure how this works, but... Resolved.

此错误是由以管理员身份运行的 Visual Studio 引起的。Outlook 不允许单独的用户访问同一个邮箱(我在桌面上打开了 Outlook 应用程序)。即使我拥有域用户的本地管理员访问权限,以管理员身份运行 VS 也必须将进程关联到不同的用户对象?不完全确定这是如何工作的,但是...已解决。

回答by lukiller

I ran into the same issue, and as previously said: if Visual Studio is running as Administrator then Outlook prevents another instance with a different user. My VS solution is starting several projects, and I need it to run as Administrator, so what I did is run Outlook as administrator while debugging. This solved my problem.

我遇到了同样的问题,如前所述:如果 Visual Studio 以管理员身份运行,则 Outlook 会阻止另一个具有不同用户的实例。我的 VS 解决方案正在启动几个项目,我需要它以管理员身份运行,所以我所做的是在调试时以管理员身份运行 Outlook。这解决了我的问题。