BLOG zzy.my

合抱之木, 生于毫末; 九层之台, 起于累土; 千里之行, 始于足下。

C# 获取exchange邮箱限量

exchange用户的邮箱容量可以通过两种方式来设置:
1,在AD管理器,每个用户的属性中可以设置.
2,在exchange系统管理器,存储组属性上可以对存储组中的所有用户邮箱进行限制.

这两种方式对用户的设置自然是方便了,但对我们编程获取邮箱限量却造成了很多麻烦.
注: 公司的产品遇到了这个问题,绵延2年没解决,其间数次造访ms的技术支持,无果.汗~~~

幸好,06年代码终于出来了.

实现思路: 首先访问AD,看AD中有无设置,若无,则从AD中取出用户所在的exchange存储组路径,然后调用exchange系统管理的组件(必须在代码运行的机器上安装exchange系统管理),获取某个存储的邮箱容量限制。

using System; 
using System.Collections ; 
using System.DirectoryServices; 

//用户缓存 
private static Hashtable _UserQuotaList = new Hashtable() ; 

/// <summary> 
/// 获取用户邮箱限量,缓存处理 
/// @author:jianyi0115@163.com 
/// </summary> 
/// <param name="userAccount"></param> 
/// <returns></returns> 
public static int GetMailboxQuota( string userAccount ) 
{ 
if( _UserQuotaList.ContainsKey(userAccount) ) 
return Convert.ToInt32( _UserQuotaList[userAccount] ) ; 

int limit = GetMailboxQuotaFromServer( userAccount ); 

_UserQuotaList.Add( userAccount , limit ) ; 

return limit ; 
} 


/// <summary> 
/// 从服务器获取用户邮箱限量 
/// </summary> 
/// <param name="userAccount"></param> 
/// <returns></returns> 
public static int GetMailboxQuotaFromServer( string userAccount ) 
{ 
string sEmailAddress = userAccount + "@" + Config.MailDomain ; //用户的邮件 

// System.DirectoryServices.DirectoryEntry adsiTop = 
// new System.DirectoryServices.DirectoryEntry( "LDAP://xhdcpdom0001" ,"Centralism", "App1234"); 

System.DirectoryServices.DirectoryEntry adsiTop = 
new System.DirectoryServices.DirectoryEntry( Config.ADServerPath ,Config.ADAdministrator, Config.ADAdministratorPassword ); 

System.DirectoryServices.DirectorySearcher adsiSearcher = new DirectorySearcher(adsiTop); 

adsiSearcher.Filter = "(mail="+sEmailAddress+")" ; 

adsiSearcher.PropertiesToLoad.Add("mdboverhardquotalimit") ; 
adsiSearcher.PropertiesToLoad.Add("homemdb") ; 
adsiSearcher.PropertiesToLoad.Add("mdbusedefaults") ; 


adsiSearcher.PropertiesToLoad.Add("msExchHomeServerName") ; 

System.DirectoryServices.SearchResult adsiResult = adsiSearcher.FindOne() ; 

if( adsiResult == null ) 
{ 
adsiSearcher.Filter = "(anr=SMTP:" + sEmailAddress + ")" ; 

adsiSearcher.PropertiesToLoad.Add("mdboverhardquotalimit") ; 
adsiSearcher.PropertiesToLoad.Add("homemdb") ; 
adsiSearcher.PropertiesToLoad.Add("mdbusedefaults") ; 

adsiResult = adsiSearcher.FindOne(); 
} 

if( adsiResult == null ) 
{ 
adsiSearcher.Dispose(); 
adsiTop.Dispose() ; 
return 0 ; 
}

//使用默认设置 
if( adsiResult.Properties["mdbusedefaults"] != null && adsiResult.Properties["mdbusedefaults"][0].ToString().ToLower() == "true" ) 
{ 
string homeDb = adsiResult.Properties["homemdb"][0].ToString(); 

adsiSearcher.Dispose(); 
adsiTop.Dispose() ; 

return GetHomeDbMailboxQuota( homeDb ) ; 
} 

//非默认设置 且AD设置为空 
if( adsiResult.Properties["mdboverhardquotalimit"] == null ) 
{ 
string homeDb = adsiResult.Properties["homemdb"][0].ToString(); 

adsiSearcher.Dispose(); 
adsiTop.Dispose() ; 

return GetHomeDbMailboxQuota( homeDb ) ; 
} 
else 
{ 
string limit = adsiResult.Properties["mdboverhardquotalimit"][0].ToString(); 

adsiSearcher.Dispose(); 
adsiTop.Dispose() ; 

return Convert.ToInt32( limit ); 
} 


} 

private static Hashtable _HomeDbQuotaList = new Hashtable() ; 

/// <summary> 
/// 从获取exchange服务器存储组邮箱限量,缓存处理 
/// </summary> 
/// <param name="dn"></param> 
/// <returns></returns> 
static private int GetHomeDbMailboxQuota( string dn ) 
{ 
if( _HomeDbQuotaList.ContainsKey( dn ) ) 
return Convert.ToInt32( _HomeDbQuotaList[dn] ) ; 

string path = Config.ADServerPath ; 
if( !path.EndsWith( "/" ) ) 
path += "/"; 

path += dn ; 

CDOEXM.MailboxStoreDB mdb = new CDOEXM.MailboxStoreDBClass(); 

mdb.DataSource.Open( path , null, 
ADODB.ConnectModeEnum.adModeRead, 
ADODB.RecordCreateOptionsEnum.adFailIfNotExists , 
ADODB.RecordOpenOptionsEnum.adOpenSource, Config.ADAdministrator, Config.ADAdministratorPassword ); 

int limit = mdb.HardLimit ; 

mdb = null ; 

try 
{ 
_HomeDbQuotaList.Add( dn , limit ) ; 
} 
catch{} 

return limit; 
}


转自 http://microsoft.cnfan.net/exchange/3524_1.html

Loading