C#代码试图自动打开新的Outlook邮件。错误0x80020006(DISP_E_UNKNOWNNAME)

时间:2016-05-24 14:57:29

标签: c# reflection com outlook interop

我正在尝试使用C#代码打开Outlook NewMail(没有COM),但遗憾的是我获得了以下错误代码:

0x80020006 (DISP_E_UNKNOWNNAME)

c#代码就在下方。

using System;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Windows.Forms;
using Erp.Adapters;
using Erp.UI;
using Ice.Lib;
using Ice.Adapters;
using Ice.Lib.Customization;
using Ice.Lib.ExtendedProps;
using Ice.Lib.Framework;
using Ice.Lib.Searches;
using Ice.UI.FormFunctions;
using System.Net.Mail;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Linq;
using Microsoft.CSharp;
using System.IO.Compression;
using System.Reflection;
using System.Text;
using System.IO;

public class Script
{
    // ** Wizard Insert Location - Do Not Remove 'Begin/End Wizard Added Module Level Variables' Comments! **
    // Begin Wizard Added Module Level Variables **

    // End Wizard Added Module Level Variables **

    // Add Custom Module Level Variables Here **

    public void InitializeCustomCode()
    {
        // ** Wizard Insert Location- Do not delete 'Begin/End Wizard Added Variable Initialization' lines **
        // Begin Wizard Added Variable Initialization


        this.POForm.AfterToolClick += new Ice.Lib.Framework.AfterToolClickEventHandler(this.POForm_AfterToolClick);
        // End Wizard Added Variable Initialization

        // Begin Wizard Added Custom Method Calls

        // End Wizard Added Custom Method Calls
    }

    public void DestroyCustomCode()
    {
        // ** Wizard Insert Location - Do not delete 'Begin/End Wizard Added Object Disposal' lines **
        // Begin Wizard Added Object Disposal


        this.POForm.AfterToolClick -= new Ice.Lib.Framework.AfterToolClickEventHandler(this.POForm_AfterToolClick);
        // End Wizard Added Object Disposal

        // Begin Custom Code Disposal

        // End Custom Code Disposal
    }

private void POForm_AfterToolClick(object sender, Ice.Lib.Framework.AfterToolClickEventArgs args)
    {
    if(args.Tool.Key == "EmailFaxTool")
{

Assembly interopAssembly = Assembly.LoadFile(@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Outlook.dll");

 object outlookApplication = interopAssembly.CreateInstance("Microsoft.Office.Interop.Outlook.ApplicationClass");
 Type outlookApplicationType = interopAssembly.GetType("Microsoft.Office.Interop.Outlook.ApplicationClass");



dynamic mailItem = outlookApplicationType.InvokeMember("CreateItem", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, outlookApplication, new object[] { 0 });


//ADDRESS
//object recipients = outlookApplication.GetType().InvokeMember("Recipients",BindingFlags.GetProperty, null, outlookApplication, null);
//string To = "gregory.dupuy@consultencia.com";
//object[] address = new object[1];
//address[0] = To;

//SUBJECT1
//recipients.GetType().InvokeMember ("Add", BindingFlags.InvokeMethod,null, recipients, address);
//string subject = "Mail Message Subject";
//parms [0] = subject;

//SUBJECT
//outlookApplication.GetType().InvokeMember("Subject", BindingFlags.SetProperty,null, outlookApplication, new object[] { 0 });
//string msg = "Just a message saying hello";


//BODY
//outlookApplication.GetType().InvokeMember("Body", BindingFlags.SetProperty,null, outlookApplication, new object[] { 0 });

//DISPLAY OR SEND
// Invoke the Send method of the mail item.
outlookApplication.GetType().InvokeMember("Display", BindingFlags.InvokeMethod,null, outlookApplication,new object[] { true } );


{
throw new Exception("OK.");
}
}

}
}

1 个答案:

答案 0 :(得分:0)

调用MailItem.Display。 Aopplication.Display方法不存在。正如@Hans Passant在评论中提到的那样,使用“动态”是一个非常可怕的想法。