读取Exchange的用户未读邮件数的几种方法

时间:2024-10-20 14:06:56

http://www.cnblogs.com/nbpowerboy/p/3539422.html

可以使用ExchangeServiceBinding获取邮件,他相当于outlook, 来获取服务器的未接受的邮件.

至于 “该帐户无权模拟所请求的用户 ” ,是由于在exchange服务器上面没有配置模拟账户.

需要在Exchange Management Shell上执行: New-ManagementRoleAssignment –Name:impersonationAssignmentName –Role:ApplicationImpersonation –User:administrator

见: Configuring Exchange Impersonation:http://msdn.microsoft.com/en-us/library/bb204095.aspx

设置后, 就不会有该帐户无权模拟所请求的用户问题了。

 public static int GetUnReadMailCountByUserMailAddress(string UserMailAddress)
{
int unRead = ;
string url = "https://{0}/ews/exchange.asmx";
url = string.Format(url, m_ExchangeMachine);
try
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
service.Credentials = new NetworkCredential(m_ServerUser, m_Password, m_Domain);
service.Url = new Uri(url);
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, UserMailAddress);
unRead = Folder.Bind(service,WellKnownFolderName.Inbox).UnreadCount; }
catch (Exception ex)
{
return unRead;
throw;
}
return unRead;
}