在我的上一片文章中,我贴出了与帐号相关的ADHelper类,以下贴出了与Exchange部分操作相关的ExchangeHelper类
using System; using System.ComponentModel.Design; using System.DirectoryServices; using System.Collections; using System.Text; using System.Diagnostics; using System.Net; using System.Runtime.InteropServices; using MSxml2; using CDOEXM; namespace ColourLife.ExchangeMail { /// /// ExchangeMail 的摘要说明。 /// public class ExchangeAccount { PRivate const string EXCHANGE_NAME = "Exchange"; public static string GetLocalDNSName() { return Dns.GetHostByName(Environment.MachineName).HostName; } /// /// 校验指定的用户的Exchange账户及密码是否正确 /// /// 指示是否使用SSL /// Exchange Server的计算机名称 /// 用户名 /// 密码 /// public static HttpStatusCode AuthorizationAccount(bool useSSL,string serverName,string userName,string passWord) { MSXML2.IxmlhttpRequest xmlHttp = new MSXML2.XMLHTTPClass(); xmlHttp.open("GET",GetUserExchangeURL(useSSL,serverName,userName),false,userName,password); xmlHttp.send(null); return (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode),xmlHttp.status.ToString()); } public static string GetUserExchangeURL(bool isUsingSSL,string serverName,string userName) { StringBuilder urlBuilder = new StringBuilder(); string protocol = isUsingSSL ? "https://" :"http://"; urlBuilder.Append(protocol).Append(serverName).Append("/").Append(EXCHANGE_NAME); urlBuilder.Append("/").Append(userName); return urlBuilder.ToString(); } /**//// /// 创建Exchange账户到指定的用户组中,同时也创建了AD账户 /// /// /// /// /// public static DirectoryEntry CreateNewExchangeAccount(string userName,string password, string organizeName,string adGroup) { return CreateNewExchangeAccount(null,null,userName,password,organizeName,adGroup); } /**//// /// 使用指定的安全信息来创建Exchange账户到指定的用户组中,同时也创建了AD账户 /// /// /// /// /// /// /// public static DirectoryEntry CreateNewExchangeAccount(string adminName,string adminPassword,string userName, string password,string organizeName,string adGroup) { //创建AD帐户 DirectoryEntry userEntry = ADHelper.CreateADAccount(adminName,adminPassword,userName,password,organizeName,adGroup); //创建Exchange邮箱 CDOEXM.IMailboxStore mailbox = (CDOEXM.IMailboxStore)userEntry.NativeObject; string machineName = Environment.MachineName; mailbox.CreateMailbox(GetFirstStoreMDB(machineName,adminName,adminPassword)); userEntry.CommitChanges(); if (adGroup != null %26amp;%26amp; adGroup.Length 0) { ADHelper.AddUserToGroup(adminName,adminPassword,userName,adGroup); } return userEntry; } /**//// /// 获取Exchange的第一个邮箱存储的LDAP路径 /// /// private static string GetFirstStoreMDB(string serverName,string userName,string password) { CDOEXM.IExchangeServer exchangeServer = new CDOEXM.ExchangeServerClass(); CDOEXM.IStorageGroup storageGroup = new CDOEXM.StorageGroupClass(); //连接到指定的Exchange数据源 exchangeServer.DataSource.Open(serverName,null,ADODB.ConnectModeEnum.adModeRead, ADODB.RecordCreateOptionsEnum.adFailIfNotExists,ADODB.RecordOpenOptionsEnum.adOpenAsync, userName,password); object[] groups = exchangeServer.StorageGroups as object[]; storageGroup.DataSource.Open(groups[0].ToString(),null,ADODB.ConnectModeEnum.adModeRead, ADODB.RecordCreateOptionsEnum.adFailIfNotExists,ADODB.RecordOpenOptionsEnum.adOpenAsync, userName,password); object[] mailboxs = storageGroup.MailboxStoreDBs as object[]; %26nbs, p; return mailboxs[0].ToString(); } /**//// /// 获取Exchange的第一个邮箱存储的LDAP路径 /// /// private static string GetFirstStoreMDB(string serverName) { return GetFirstStoreMDB(serverName,null,null); } /**//// /// 删除Exchange用户账户并同时删除AD账户 /// /// 用户名称 public static void DeleteExchangeAccount(string adminName,string adminPassword,string userName) { DeleteExchangeAccount(adminName,adminPassword,userName,true); } /**//// /// 删除Exchange用户账户并指示是否同时删除AD账户 /// /// /// /// /// public static void DeleteExchangeAccount(string adminName,string adminPassword,string userName,bool deleteADUser) { DirectoryEntry user = ADHelper.FindObject(adminName,adminPassword,"user",userName); //删除Exchange邮箱 CDOEXM.IMailboxStore mailbox = (CDOEXM.IMailboxStore)user.NativeObject; mailbox.DeleteMailbox(); user.CommitChanges(); ADHelper.DeleteADAccount(adminName,adminPassword,userName); } } }
在以上类中需要引用ADHelper类,有关ADHelper类请参考
Exchange开发(五) ADHelper类 http://news.5dmail.net/html/2005-12-26/2005122694440.htm