【发布时间】:2015-08-07 09:38:12
【问题描述】:
我创建了一个 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 现在已打开,如果我再次尝试创建应用程序实例,我会成功,然后可以成功发送电子邮件。
我对这个变通办法不满意,并且希望了解 COM 异常的原因,看看我是否可以解决它。
请注意,这是内部公司电子邮件,因此我无法访问 SMTP 服务器,因此我必须使用 Outlook 进行发送。
有人知道这里发生了什么吗?
【问题讨论】:
标签: winforms c#-4.0 com office-interop outlook-2007