博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
采用模拟账号读取Exchange server未读邮件的注意事项(链接邮箱问题)【转】
阅读量:6844 次
发布时间:2019-06-26

本文共 5090 字,大约阅读时间需要 16 分钟。

最近做项目碰到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:
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。

但是有的邮箱却可以用进行访问。百思不得其解,后来发现原来: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:
//必须设置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; }}

 

转载地址:http://bddul.baihongyu.com/

你可能感兴趣的文章
[stm32] STM32的通用定时器TIMx系统了解
查看>>
jQuery Ajax 仿AjaxPro.Utility.RegisterTypeForAjax辅助方法
查看>>
添加php的memcached扩展模块
查看>>
W3wp.exe占用CPU及内存资源
查看>>
Rabbitmq~对Vhost的配置
查看>>
CentOS下Web服务器环境搭建LNMP一键安装包
查看>>
Redis发布订阅实现原理
查看>>
Java魔法堂:JVM的运行模式 (转)
查看>>
thinkPHP四种URL访问方式(二)
查看>>
使用InstallShield打包VS程序
查看>>
我的Android进阶之旅------>Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类...
查看>>
shell正则表达式匹配样例
查看>>
Sublime Text 的安装和配置
查看>>
数据库设计三大范式
查看>>
为了加速应用物联网,IT高管必须做的6件事
查看>>
查看MYSQL中数据表占用的空间
查看>>
栗蔚:中国云计算“风景独好”
查看>>
使用sqlparse分析SQL语句,及自己写的SQL分析语句
查看>>
搭建国际交流平台 黑龙江海外人才工作站涉及3D打印
查看>>
嵌入式工控机在舞台灯光控制中的应用
查看>>