首页 > 开发 > 综合 > 正文

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

2024-07-21 02:27:47
字体:
来源:转载
供稿:网友
        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。
欢迎互相交流。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表