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

来源:互联网 发布:acrobat 2018 for mac 编辑:程序博客网 时间:2024/06/05 01:15
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
        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。
欢迎互相交流。

域中增加用户和邮箱';return true">
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击