标签:
有的时候,在做Sharepoint开发的时候我们需要获取用户的邮件信息,通常都是通过调用EWS API来实现。
以下是实现的核心代码:
第一步需要在Exchange服务上授权一个账户有模拟其他用户的权限,在Exhcange服务上打开Exchange powershell命名窗口,输入下面命名
New-ManagementRoleAssignment -Name:impersonationAssignmentName -Role:ApplicationImpersonation -User:需要授权的账号
//模拟用户名,密码,域 string ImpersonatedUser="test"; string ImpersonatedPwd="test"; string domain="test"; //目标用户邮箱地址 string emailserverAddress="user1@test.com.cn"; //ExchangeService版本为2013 ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013); service.Credentials = new NetworkCredential(ImpersonatedUser, ImpersonatedPwd, domain); //打开目标用户的邮箱地址 service.Url = new Uri(emailserverAddress); //对目标用户进行模拟 service.ImpersonatedUserId = new Microsoft.Exchange.WebServices.Data.ImpersonatedUserId(Microsoft.Exchange.WebServices.Data.ConnectingIdType.SmtpAddress, UserMailAddress); //只读取目标用户未读的信息 SearchFilter sf = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false); //只读取前10条信息 ItemView view = new ItemView(int.Parse(top)); FindItemsResults<Item> findMailResults = service.FindItems(WellKnownFolderName.Inbox, sf, view); if (findMailResults.Count() > 0) { foreach (Item m in findMailResults) { EmailEntity em = new EmailEntity(); Microsoft.Exchange.WebServices.Data.EmailMessage e = Microsoft.Exchange.WebServices.Data.EmailMessage.Bind(service, m.Id); em.ID = e.Id.ToString(); em.Subject = e.Subject; lists.Add(em); } }
标签:
原文地址:http://www.cnblogs.com/lynn-lin/p/4627648.html