注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

忘忧草的博客

品味人生--精通IT管理和技术

 
 
 

日志

 
 
 
 

采用模拟账号读取Exchange server未读邮件的注意事项(链接邮箱问题)  

2013-06-13 17:13:47|  分类: Moss2010 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

最近做项目碰到Exchange中,用EWS API方法读取的未读邮箱(ConnectingIdType.PrincipalName设置该属性的方法)附带代码部分:

代码部分:

using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TCL.EWS
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EwsConfig config = new EwsConfig();
            config.ExchangeVersion = ExchangeVersion.Exchange2010_SP2;
            config.EWSServiceUrl = "https://XXX/EWS/exchange.asmx";
            config.ExchangeAdministrator = "XXX";
            config.ExchangeAdministratorPassword = "XXX";
            config.DomainName = "XXX";
            config.OtherUserName = "test003";
            
            //下面这句屏蔽服务器证书验证,防止页面报“根据验证过程,远程证书无效”的错误                                
            ServicePointManager.ServerCertificateValidationCallback =
                delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
            ExchangeService service = new ExchangeService(config.ExchangeVersion);
            //service.AutodiscoverUrl("test002@tcl.local");
            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;
    }
}

错误部分:一直提示:The impersonation principal name is invalid,如下图:

image

?

但是有的邮箱却可以用进行访问。百思不得其解,后来发现原来:test003@xxx.com是链接邮箱,用service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.PrincipalName, config.OtherUserName);是无法访问到的。

什么是链接邮箱?

答:链接邮箱是与外部帐户关联的邮箱。要将邮箱与外部帐户关联,可以资源林方案为例。在资源林方案中,Exchange 林中的用户对象具有邮箱,但这些用户对象无法登录。必须将 Exchange 林中那些禁用用户帐户与外部帐户林中的启用用户帐户相关联。

后来查询了MSDN,改用service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, config.OtherUserName);可以进行访问,这样就解决了链接邮箱和用户邮箱的问题,可以正常读取未读邮件。代码部分如下:

using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace TCL.EWS
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            EwsConfig config = new EwsConfig();
            config.ExchangeVersion = ExchangeVersion.Exchange2010_SP2;
            config.EWSServiceUrl = "https://XXX/EWS/exchange.asmx";
            config.ExchangeAdministrator = "XXX";
            config.ExchangeAdministratorPassword = "XXX";
            config.DomainName = "XXXX";
            //必须设置成邮箱,不用是用户
            config.OtherUserName = "test003@XXX.XXX";
            
            //下面这句屏蔽服务器证书验证,防止页面报“根据验证过程,远程证书无效”的错误                                
            ServicePointManager.ServerCertificateValidationCallback =
                delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };
            ExchangeService service = new ExchangeService(config.ExchangeVersion);
            //service.AutodiscoverUrl("test002@tcl.local");
            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>
            //必须设置ConnectingIdType.SmtpAddress
            service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, 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;
    }
}
  评论这张
 
阅读(626)| 评论(0)
推荐

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017