程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 用C#在域中增加用戶和郵箱

用C#在域中增加用戶和郵箱

編輯:關於C語言
 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。
歡迎互相交流。
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved