用C#在域中增加用户和邮箱

80酷酷网    80kuku.com

          public int CreatUser(string loginname)
        {
            string p_strDomainLoginName="admin";
            string p_strDomainLoginPassword="";
            string p_strDomainName="myweb.com";
            string p_strDCName="DC=myweb,DC=com";
            string p_strServerName="myweb";
            int ReturnValue;

            CDO.PersonClass pcUser;
            CDOEXM.IMailboxStore MailBox;
            string strLDAP="LDAP://" + p_strDomainName + "/CN=" + loginname + ",CN=users," + p_strDCName;
            string strHomeMDBURL="LDAP://" + p_strDomainName + "/CN=Mailbox Store (" + p_strServerName + ")," +
                                "CN=First Storage Group,CN=InformationStore," +
                                "CN=" + p_strServerName + ",CN=Servers,CN=First Administrative Group," +
                                "CN=Administrative Groups,CN=First Organization," +
                                "CN=Microsoft Exchange,CN=Services,CN=Configuration," + p_strDCName;

            if (loginname.Length>0)
            {
                try
                {
                    //增加用户
                    pcUser=new PersonClass();
                    pcUser.FirstName=loginname;
                    pcUser.LastName="";
                    pcUser.Fields["userAccountControl"].Value=66048;            //设置该用户密码永不过期
                    pcUser.Fields["userPrincipalName"].Value=loginname;            //设置用户的登录帐号
                    pcUser.Fields.Update();
                    pcUser.DataSource.SaveTo(strLDAP,null,ADODB.ConnectModeEnum.adModeReadWrite ,
ADODB.RecordCreateOptionsEnum.adCreateOverwrite,ADODB.
RecordOpenOptionsEnum.adOpenSource,p_strDomainLoginName,p_strDomainLoginPassword);

                    //添加邮箱
                    MailBox=(IMailboxStore)pcUser.GetInterface("IMailboxStore");
                    MailBox.CreateMailbox(strHomeMDBURL);

                    pcUser.Email="SMTP:" + loginname + "" + p_strDomainName;
                    pcUser.Fields["mailnickname"].Value="mailnickname" + loginname;
                    pcUser.Fields.Update();
                    pcUser.DataSource.Save();
                    ReturnValue=1;
                }
                catch
                {
                    ReturnValue=-1;
                }
            }
            else
            {
                ReturnValue=-1;
          
  }
            return ReturnValue;
        }
注意引用CDO和CDOEXM。
欢迎互相交流。


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: