在我的上一片文章中,我贴出了与帐号相关的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
{
/// <summary>
/// ExchangeMail 的摘要说明。
/// </summary>
public class ExchangeAccount
{
private const string EXCHANGE_NAME = "Exchange";
public static string GetLocalDNSName()
{
return Dns.GetHostByName(Environment.MachineName).HostName;
}
/// <summary>
/// 校验指定的用户的Exchange账户及密码是否正确
/// </summary>
/// <param name="useSSL">指示是否使用SSL</param>
/// <param name="serverName">Exchange Server的计算机名称</param>
/// <param name="userName">用户名</param>
/// <param name="password">密码</param>
/// <returns></returns>
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();
}
/**//// <summary>
/// 创建Exchange账户到指定的用户组中,同时也创建了AD账户
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="organizeName"></param>
/// <param name="adGroup"></param>
public static DirectoryEntry CreateNewExchangeAccount(string userName,string password,
string organizeName,string adGroup)
{
return CreateNewExchangeAccount(null,null,userName,password,organizeName,adGroup);
}
/**//// <summary>
/// 使用指定的安全信息来创建Exchange账户到指定的用户组中,同时也创建了AD账户
/// </summary>
/// <param name="adminName"></param>
/// <param name="adminPassword"></param>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="organizeName"></param>
/// <param name="adGroup"></param>
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 && adGroup.Length > 0)
{
ADHelper.AddUserToGroup(adminName,adminPassword,userName,adGroup);
}
return userEntry;
}
/**//// <summary>
/// 获取Exchange的第一个邮箱存储的LDAP路径
/// </summary>
/// <returns></returns>
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[];
&nbs, p; return mailboxs[0].ToString();
}
/**//// <summary>
/// 获取Exchange的第一个邮箱存储的LDAP路径
/// </summary>
/// <returns></returns>
private static string GetFirstStoreMDB(string serverName)
{
return GetFirstStoreMDB(serverName,null,null);
}
/**//// <summary>
/// 删除Exchange用户账户并同时删除AD账户
/// </summary>
/// <param name="userName">用户名称</param>
public static void DeleteExchangeAccount(string adminName,string adminPassword,string userName)
{
DeleteExchangeAccount(adminName,adminPassword,userName,true);
}
/**//// <summary>
/// 删除Exchange用户账户并指示是否同时删除AD账户
/// </summary>
/// <param name="adminName"></param>
/// <param name="adminPassword"></param>
/// <param name="deleteADUser"></param>
/// <param name="userName"></param>
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