参考:http://lloydsheng.com/2010/03/exchange-webservice-managed-api.html
1.安装 Exchange Web Services Managed API,当前版本1.1
2.授权一个账户有模拟其他用户的权限,打开Exchange 2010 服务器在命令行中输入
New-ManagementRoleAssignment -Name:impersonationAssignmentName -Role:ApplicationImpersonation -User:serviceAccount
serviceAccount:就是我们要模拟其他人的账户
///////////////////////////////////////////////////////////////////////////////////////
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using Microsoft.Exchange.WebServices.Data;
namespace EWS_test
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
EwsConfig config = new EwsConfig();
config.ExchangeVersion=ExchangeVersion.Exchange2010_SP1;
config.EWSServiceUrl="https://********/EWS/exchange.asmx";
config.ExchangeAdministrator = "<UserName>";
config.ExchangeAdministratorPassword = "<PWD>";
config.DomainName = "<domainName>";
config.OtherUserName = "<otherUserName>";
//下面这句屏蔽服务器证书验证,防止页面报“根据验证过程,远程证书无效”的错误
ServicePointManager.ServerCertificateValidationCallback =
delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
ExchangeService service = new ExchangeService(config.ExchangeVersion);
service.Credentials = new NetworkCredential(config.ExchangeAdministrator, config.ExchangeAdministratorPassword, config.DomainName);
service.Url = new Uri(config.EWSServiceUrl);
//前提打开Exchange 2010服务器在命令行中输入:
//New-ManagementRoleAssignment -Name:impersonationAssignmentName -Role:ApplicationImpersonation -User:<UserName>
service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, config.OtherUserName);
int unRead=Folder.Bind(service, WellKnownFolderName.Inbox).UnreadCount;
HttpContext.Current.Response.Write(config.OtherUserName+"未读邮件数:"+unRead);
}
public struct EwsConfig
{
public ExchangeVersion ExchangeVersion;
public string EWSServiceUrl;
public string ExchangeAdministrator;
public string ExchangeAdministratorPassword;
public string DomainName;
public string OtherUserName;
}
}
}