创建Outlook 2007应用程序实例时出现C#COM异常(80010001,ErrorCode -2147418111)

时间:2015-08-07 09:38:12

标签: winforms c#-4.0 com office-interop outlook-2007

我已经创建了一个Winforms应用程序来安排某些Crystal Reports的执行。此应用程序计划使用标准Windows XP计划任务功能在我的Windows域用户下执行。

我已经添加了应用程序在执行报告期间发生故障时发送电子邮件的功能,以及在当天执行了所有计划报告后的完成电子邮件。

以下代码显示了我如何创建Microsoft.Office.Interop.Outlook.Application实例的测试,然后为填充和发送创建_MailItem:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace MailTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Boolean eResult = false;
            String emailError = "";

            eResult = sendEmail("blah@blah.com", "Email Testing", "This is a test", false, out emailError);

            if (eResult)
                MessageBox.Show("Email sent!");
            else
                MessageBox.Show("Email failed->" + emailError);
        }

        public static Boolean sendEmail(String recipients, String subject, String body, Boolean highImportance, out String error)
        {
            Boolean success = true;
            error = "";

            Outlook.Application outApp = null;

            // Create an instance of the Outlook Application.
            try
            {
                outApp = new Outlook.Application();
            }
            catch (System.Runtime.InteropServices.COMException ce)
            {
                // We're going to do nothing with this at this time, as something weird happens if Outlook is not
                // open when outApp is created above, and a COM exception is raised - however, Outlook ends up
                // being opened, and creating an Application instance again succeeds. This is a workaround until I can
                // find more detail on the COM exception.
                MessageBox.Show(ce.Message);
            }
            catch (System.Exception e)
            {
                // Any other type of Exception should be reported back to the caller with a failure status.
                error = e.Message;
                return false;
            }

            // Re-try the setting of outApp in case the original attempt failed above.
            if (outApp == null)
            {
                try
                {
                    outApp = new Outlook.Application();
                }
                catch (Exception e)
                {
                    // If we fail to successfully open Outlook the second time, a different error has occurred, so
                    // we just want to return the exception message with false as result.
                    error = e.Message;
                    return false;
                }
            }

            // Now create an Outlook mail item to populate and send.
            Outlook.MailItem msg = outApp.CreateItem(Outlook.OlItemType.olMailItem);
            // etc etc

当第一次尝试创建新的Outlook应用程序,并且Outlook未在我的PC上运行时,Outlook已打开(我看到它的图标出现在系统托盘中),但以下异常是在线内提出

outApp = new Outlook.Application();

异常详细信息:

System.Runtime.InteropServices.COMException was unhandled
  Message=Creating an instance of the COM component with CLSID {0006F03A-0000-0000-C000-000000000046} from the IClassFactory failed due to the following error: 80010001.
  Source=mscorlib
  ErrorCode=-2147418111
  StackTrace:
       at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
       at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(RuntimeType serverType)
       at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(RuntimeType serverType, Object[] props, Boolean bNewObj)
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at System.Activator.CreateInstance(Type type)
       at MailTest.Form1.sendEmail(String recipients, String subject, String body, Boolean highImportance, String& error) in c:\Documents and Settings\CT100751\My Documents\Visual Studio 2010\Projects\MailTest\MailTest\Form1.cs:line 43
       at MailTest.Form1.button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\CT100751\My Documents\Visual Studio 2010\Projects\MailTest\MailTest\Form1.cs:line 25
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at MailTest.Program.Main() in c:\Documents and Settings\CT100751\My Documents\Visual Studio 2010\Projects\MailTest\MailTest\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

我已在网上搜索错误80010001和ErrorCode -2147418111的组合,但一无所获。

但是,由于Outlook现在已打开,如果我再次尝试创建Application实例,我会成功,然后可以成功发送电子邮件。

我对这种解决方法不满意,并且更愿意了解COM异常的原因,看看我是否可以解决它。

请注意,这是公司内部电子邮件,因此我无法访问SMTP服务器,因此我必须使用Outlook进行发送。

任何人都知道这里发生了什么?

1 个答案:

答案 0 :(得分:0)

0x80010001错误代码对应于RPC_E_CALL_REJECTED。请查看描述类似问题的How to: Fix 'Application is Busy' and 'Call was Rejected By Callee' Errors文章,并提供避免此类问题的示例代码。

此外,Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT服务)自动化Microsoft Office应用程序,因为Office可能会展示在此环境中运行Office时不稳定的行为和/或死锁。

如果要构建在服务器端上下文中运行的解决方案,则应尝试使用已为安全无人值守执行的组件。或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。如果从服务器端解决方案使用Office应用程序,则应用程序将缺少许多成功运行的必要功能。此外,您将承担整体解决方案稳定性的风险。

Considerations for server-side Automation of Office文章中详细了解相关内容。