读取Exchange邮件或任务(第一种方法)

第一种

企业邮件系统Exchange往往会与企业业务协作平台SharePoint一起搭配使用,如在SharePoint企业门户上显示当前登录用户所有未读邮件或最新的待办任务信息等,以便于用户可以在门户上选择未读邮件或待办任务直接打开Exchange邮件系统显示信息的正文,而不需要用户再去通过OutLook客户端或OWA方式去登录邮件系统去查看信息,从而提高了工作效率。

 

而要在SharePoint门户上显示邮件系统上未读邮件或任务需要通过代码编程来实现,将这个功能开发成一个自定义的WebPart后部署到门户上去。

 

接下来介绍如何通asp.net代码来读取Exchange邮件或任务:通过Exchange Web ServiceExchange Web Services Managed API

 

一、通过Exchange Web Service来读取

 

1、首先,在项目上添加Web Service引用,这个Web ServiceURL 地址格式如:https://Exchange邮件系统的服务器名/EWS/Exchange.asmx

 

2、引入如下命名空间:

using System.Net;

using System.Net.Security;

using System.Security.Cryptography.X509Certificates;

 

3、编写代码读取邮件信息:

 

复制代码
            //忽略SSL证书请求必须的,不添加在执行时会报错,错误信息好像是(记不清了)“客户端响应错误………html / text”
            ServicePointManager.ServerCertificateValidationCallback =
                    delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
                    { return true; };

            //创建Exchange服务绑定对象
                ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
            //创建安全身份凭证
                ICredentials creds = new NetworkCredential("username", "password", "domain");
            //建立信任连接
                exchangeServer.Credentials = creds;
                exchangeServer.Url = "https://Exchange邮件系统的服务器名/EWS/Exchange.asmx";
            
            //定义邮件的收件箱
                DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
                folderIDArray[0] = new DistinguishedFolderIdType();
                folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox; //读取inbox文件夹下所有邮件,不包括子文件夹
            //folderIDArray[0].Id = DistinguishedFolderIdNameType.tasks;//读取Task文件夹下的所有任务,不包括子文件夹

            //设置要读取所需满足的条件
                PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
                ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;

                PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
                pteftComment.PropertyTag = "0x3004"; 
                pteftComment.PropertyType = MapiPropertyTypeType.String;

                GetFolderType myfoldertype = new GetFolderType();
                myfoldertype.FolderIds = folderIDArray;
                myfoldertype.FolderShape = new FolderResponseShapeType();
                myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
                myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
                myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
                myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;
            
            //获取Exchange邮件系统中指定类型文件夹信息
                GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);

                FolderInfoResponseMessageType firmtInbox =
                    (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];

                PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
                ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;

                PathToUnindexedFieldType ptuftBody = new PathToUnindexedFieldType();
                ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;

                PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
                pteftFlagStatus.PropertyTag = "0x1090"; 
                pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;

            //  定义FindItemType对象,准备获取收件箱中的集合
                FindItemType findItemRequest = new FindItemType();
                findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
                findItemRequest.ItemShape = new ItemResponseShapeType();
                findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.Default;

                findItemRequest.ParentFolderIds = new FolderIdType[1];
                findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;

            //获取指定收件箱中的邮件集合
                FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);

                MessageType mt = new MessageType();  //邮件类型
                int newEmail = 0;//Unread email number
                foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
                {
                    if (firmtMessage.RootFolder.TotalItemsInView > 0)
                    {
                        foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
                        {
                            mt = it as MessageType;
                            if (mt != null)
                            {
                                //this.TextBox1.Text += string.Format(string.Format("是否已读: {0} 
", mt.IsRead.ToString()));
                                if (!mt.IsRead) //判断邮件是否是已读还是未读
                                    newEmail++;
                                else
                                    continue;
                            }

                            #region Eg codes

                            Response.Write(string.Format("

标题: {0} 
", it.Subject));
                            Response.Write(string.Format("

标题: {0} 
", it.Subject) + string.Format("发件人: {0} 
", mt.From.Item.Name.ToString()));
                            
                            #endregion
                        }

                        Response.Write(newEmail.ToString());

                    }
                }
复制代码
 

转载于:https://www.cnblogs.com/dengyuxuan/p/7525885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值