Add users and mailboxes in a domain with C #

Source: Internet
Author: User
Tags ldap domain
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;

Cdos. 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
{
Increase user
Pcuser=new Personclass ();
Pcuser.firstname=loginname;
Pcuser.lastname= "";
pcuser.fields["userAccountControl"].            value=66048; Set this user password never expires
pcuser.fields["userPrincipalName"].            Value=loginname; Set the user's logon account
PcUser.Fields.Update ();
PcUser.DataSource.SaveTo (Strldap,null,adodb. Connectmodeenum.admodereadwrite,
ADODB. Recordcreateoptionsenum.adcreateoverwrite,adodb.
Recordopenoptionsenum.adopensource,p_strdomainloginname,p_strdomainloginpassword);

Add mailbox
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;
}
Note the reference to CDOs and CDOEXM.
Welcome to communicate with each other.




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.