无法转换COM对象-使用C#.NET创建新的Exchange邮件帐户

问题描述:


大家好!

我正在开发一个应用程序,它将在AD中添加新用户,将其与组关联,并在MS Exchange中创建一个邮件帐户.
我正在使用C#.NET.
正确添加了COM组件'CDOEXM'和'System.DirectoryServices'作为

引用.
成功添加了用户,但是邮件帐户

失败
我遵循了Exchange SDK示例,但是

指令存在问题:"mailbox =(IMailboxStore)NewUser.NativeObject;"

以下错误返回消息:

无法将类型为'System .__ ComObject'的COM对象转换为接口类型

'CDOEXM.IMailboxStore'.此操作失败,因为对具有IID

'{25150F41-5734-11D2-A593-00C04F990D8A}'的接口的COM组件上的QueryInterface

调用由于以下错误而失败:

不支持此类接口(HRESULT的异常:0x80004002

(E_NOINTERFACE)).


欢迎任何提示,请

谢谢和问候!

Tiago Fusco

测试源代码:

使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data ;
使用 System.Drawing;
使用 System.Text;
使用 CDOEXM;
使用 CDO;
使用 CDOWF;
使用 System.DirectoryServices;
使用 System.Web.Services;

命名空间 Exch2
{
    public 部分 class Form1:Form
    {
       public Form1()
       {
           InitializeComponent();
       }

       private void button1_Click( object 发件人,EventArgs e)
&nbsp ;      {
           //TODO:将这些项目更改为您的域或

组织的值.
          字符串defaultNC ="DC = gsp-tec,DC = com";
字符串别名="imediate2";
         字符串fullName ="Lohaine";
         字符串密码="pass @ word1";
         字符串domainName ="gsp-tec.com.br";
            //string homeMDB ="CN =邮箱存储

(GSP-SERVER),CN =管理员,DC = Com"
       &nbsp ;   //+"CN = InformationStore,CN = gsp-server,CN = Servers",
//+"CN = administrator,CN = Administrative Groups",
            //+"CN = Org,CN = Microsoft Exchange,CN = Services",
            //+"CN = Configuration,DC = gsp-tec,DC = Com";
           字符串homeMDB ="CN =邮箱存储(GSP-SERVER),CN =第一个

存储组,"
              +"CN = InformationStore,CN = GSP-SERVER,CN =服务器",
         +"CN =第一行政组,CN =行政

组,"
              ;         +"CN = GSP技术(Exchange),CN = Microsoft

Exchange,CN =服务,"
        &nbsp ;            +"CN =配置,DC = GSP-TEC,DC = COM";

//GSP-SERVER/第一存储组/邮箱存储(GSP-SERVER)

    
           CDOEXM.IMailboxStore邮箱;

//这会在用户"容器中创建新用户.
           //设置sAMAccountName和密码
//容器=新的DirectoryEntry("LDAP://cn = users," +

defaultNC);

        &nbsp ;   //string str ="CN =" + fullName;
          字符串str ="CN =" + fullName;


试试
                    {
              DirectoryEntry AD =新的DirectoryEntry("WinNT://" +
             &b                    Environment.MachineName + 计算机");

                 //DirectoryEntry AD =新的

DirectoryEntry("LDAP://CN = users,CN = builtin,DC = gsp-tec,DC = com",

"administrator","gestar01 );
                DirectoryEntry NewUser = AD.Children.Add(别名,用户");
NewUser.Invoke("SetPassword",新对象[] {密码});
NewUser.Invoke("Put",new object [] {"Description",测试

.NET中的用户"});
               NewUser.CommitChanges();
//criando邮箱
             邮箱=(IMailboxStore)NewUser.NativeObject;

mail.CreateMailbox(homeMDB);
             NewUser.CommitChanges();

            DirectoryEntry grp;

             grp = AD.Children.Find(管理员",组");
if(grp!= null)
             {
                 grp.Invoke(添加",新对象[] {

NewUser.Path.ToString()});
      }

              //MessageBox.Show(帐户创建成功");
               //Console.ReadLine();
             textBox1.Text =帐户创建成功";
            }
           catch(ex ex例外)
                                         {
              //MessageBox.Show(ex.Message);
             //Console.ReadLine();
             textBox1.Text = ex.Message;

           }
       }
   }
}

 


Hello to everyone!

I´m developing an application that will add a new user in AD, associates it

to a group and create a mail account into MS Exchange.
I´m using C# .NET.
The COM component 'CDOEXM'and 'System.DirectoryServices' are added as

reference correctly.
The user is added sucefully, but a failure occurs on the mail account

creation.
I´ve followed the Exchange SDK sample but I´ve got a problem with the

instruction : "mailbox = (IMailboxStore)NewUser.NativeObject;"

the following error message is returned:

Unable to cast COM object of type 'System.__ComObject' to interface type

'CDOEXM.IMailboxStore'. This operation failed because the QueryInterface

call on the COM component for the interface with IID

'{25150F41-5734-11D2-A593-00C04F990D8A}' failed due to the following error:

No such interface supported (Exception from HRESULT: 0x80004002

(E_NOINTERFACE)).


any tip is welcome, please

Thanks and great regards!

Tiago Fusco

the test source code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CDOEXM;
using CDO;
using CDOWF;
using System.DirectoryServices;
using System.Web.Services;

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

        private void button1_Click(object sender, EventArgs e)
        {
            //TODO: Change these items to values for your domain or

organization.
            string defaultNC = "DC=gsp-tec,DC=com";
            string alias = "imediate2";
            string fullName = "Lohaine";
            string password = "pass@word1";
            string domainName = "gsp-tec.com.br";
            //string homeMDB = "CN=Mailbox Store

(GSP-SERVER),CN=Administrators,DC=Com"
            //+ "CN=InformationStore,CN=gsp-server,CN=Servers,"
            //+ "CN=administrator,CN=Administrative Groups,"
            //+ "CN=Org,CN=Microsoft Exchange,CN=Services,"
            //+ "CN=Configuration,DC=gsp-tec,DC=Com";
            string homeMDB = "CN=Mailbox Store (GSP-SERVER),CN=First

Storage Group,"
                         + "CN=InformationStore,CN=GSP-SERVER,CN=Servers,"
                         + "CN=First Administrative Group,CN=Administrative

Groups,"
                         + "CN=GSP Tecnologia(Exchange),CN=Microsoft

Exchange,CN=Services,"
                         + "CN=Configuration,DC=GSP-TEC,DC=COM";

            //GSP-SERVER/First Storage Group/Mailbox Store (GSP-SERVER)    

     
            CDOEXM.IMailboxStore mailbox;

            //This creates the new user in the "users" container.
            //Set the sAMAccountName and the password
            // container = new DirectoryEntry("LDAP://cn=users," +

defaultNC);

            //string str= "CN="+fullName;
            string str = "CN=" + fullName;


            try
            {
                DirectoryEntry AD = new DirectoryEntry("WinNT://" +
                                    Environment.MachineName + ",computer");

                //DirectoryEntry AD = new

DirectoryEntry("LDAP://CN=users,CN=builtin,DC=gsp-tec,DC=com",

"administrator", "gestar01");
                DirectoryEntry NewUser = AD.Children.Add(alias, "user");
                NewUser.Invoke("SetPassword", new object[] { password });
                NewUser.Invoke("Put", new object[] { "Description", "Test

User from .NET" });
                NewUser.CommitChanges();
                //criando mailbox
                mailbox = (IMailboxStore)NewUser.NativeObject;

                mailbox.CreateMailbox(homeMDB);
                NewUser.CommitChanges();

                DirectoryEntry grp;

                grp = AD.Children.Find("Administrators", "group");
                if (grp != null)
                {
                    grp.Invoke("Add", new object[] {

NewUser.Path.ToString() });
                }

                //MessageBox.Show("Account Created Successfully");
                //Console.ReadLine();
                textBox1.Text = "Account Created Successfully";
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
                //Console.ReadLine();
                textBox1.Text = ex.Message;

            }
        }
     }
}

 

我有同样的问题

I have the same problem

关键是它不会每次都发生.有时创建邮箱-有时-不是

And the point is that it doesn't occur every time. Sometimes the mailbox is created - sometimes - not