C# code trying to open New Outlook Mail Automatically. Error 0x80020006 (DISP_E_UNKNOWNNAME)

-1

I'm trying to open Outlook NewMail with C# code (without COM) but unfortunately I obtain the following error code:

0x80020006 (DISP_E_UNKNOWNNAME)

The c# code is just below.


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.");
}
}

}
}
c#
reflection
com
outlook
interop
asked on Stack Overflow May 24, 2016 by Komlan Dossouvi • edited May 24, 2016 by (unknown user)

1 Answer

0

Call MailItem.Display. Aopplication.Display method does not exist. As @Hans Passant mentions in the comment, using "dynamic" is a pretty terrible idea.


User contributions licensed under CC BY-SA 3.0