天天看点

非常适用的Exchange 2007 Web Services

   非常适用的Exchange 2007 Web Services,最近的项目中为了使用短信网关提供邮件短信提醒先,打开    VS2008 -> "Add Web Reference" -> ???? ,那个是Exchange2007的Service,我的是90天试用版本的Pro,直接输入:http://mailsrv 提示验证失败,找不到任何东西,烦了半天,还以为VS装坏了,在mailsrv的IIS里头发现了:

   http(s)://MyServer/ews/services.wsdl

   http(s)://MyServer/EWS/exchange.asmx?wsdl

嘿嘿,找到了,后面的地址最后都会跳到/ews/services.wsdl,这份契约里会列出所有的实用方法,读写邮件,日历,约会,会议等等,这下好办了,取出Inbox里头最新的未读邮件,顺便到AD里头把电话号码捞过来,再顺便给短信猫的代理:

CMS.dll ,OK,手机呼拉呼拉,收到邮件提醒,内容大致为邮件标题和发送时间和重要性!你不习惯邮件也不行啊,流程催办,否则年终不发奖金,最后都习惯用Email了,我们的生意又来了。。。。。

    顺便看看我最初的版本,很烂很适用:

非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
....
非常适用的Exchange 2007 Web Services
ExchangeServiceBinding exchangeServer = new ExchangeServiceBinding();
非常适用的Exchange 2007 Web Services
        ICredentials creds = new NetworkCredential("pccai", "password", "abc");
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        exchangeServer.Credentials = creds;
非常适用的Exchange 2007 Web Services
        exchangeServer.Url = @"https://mailsrv/ews/exchange.asmx";
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
非常适用的Exchange 2007 Web Services
        folderIDArray[0] = new DistinguishedFolderIdType();
非常适用的Exchange 2007 Web Services
        folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        PathToUnindexedFieldType ptuftDisplayName = new PathToUnindexedFieldType();
非常适用的Exchange 2007 Web Services
        ptuftDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        PathToExtendedFieldType pteftComment = new PathToExtendedFieldType();
非常适用的Exchange 2007 Web Services
        pteftComment.PropertyTag = "0x3004"; // PR_COMMENT
非常适用的Exchange 2007 Web Services
        pteftComment.PropertyType = MapiPropertyTypeType.String;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        GetFolderType myfoldertype = new GetFolderType();
非常适用的Exchange 2007 Web Services
        myfoldertype.FolderIds = folderIDArray;
非常适用的Exchange 2007 Web Services
        myfoldertype.FolderShape = new FolderResponseShapeType();
非常适用的Exchange 2007 Web Services
        myfoldertype.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;
非常适用的Exchange 2007 Web Services
        myfoldertype.FolderShape.AdditionalProperties = new BasePathToElementType[2];
非常适用的Exchange 2007 Web Services
        myfoldertype.FolderShape.AdditionalProperties[0] = ptuftDisplayName;
非常适用的Exchange 2007 Web Services
        myfoldertype.FolderShape.AdditionalProperties[1] = pteftComment;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        GetFolderResponseType myFolder = exchangeServer.GetFolder(myfoldertype);
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        FolderInfoResponseMessageType firmtInbox =
非常适用的Exchange 2007 Web Services
            (FolderInfoResponseMessageType)myFolder.ResponseMessages.Items[0];
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
//Response.Write(string.Format("got folder: {0}", firmtInbox.Folders[0].DisplayName));
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
//if (null != firmtInbox.Folders[0].ExtendedProperty)
非常适用的Exchange 2007 Web Services
//{
非常适用的Exchange 2007 Web Services
//     Response.Write(string.Format("Comment: {0}", firmtInbox.Folders[0].ExtendedProperty[0].Item.ToString()));
非常适用的Exchange 2007 Web Services
//}
非常适用的Exchange 2007 Web Services
//else
非常适用的Exchange 2007 Web Services
//{
非常适用的Exchange 2007 Web Services
//     Response.Write("Comment: not found");
非常适用的Exchange 2007 Web Services
//}
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();
非常适用的Exchange 2007 Web Services
        ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        PathToUnindexedFieldType ptuftBody= new PathToUnindexedFieldType();
非常适用的Exchange 2007 Web Services
        ptuftBody.FieldURI = UnindexedFieldURIType.itemAttachments;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        PathToExtendedFieldType pteftFlagStatus = new PathToExtendedFieldType();
非常适用的Exchange 2007 Web Services
        pteftFlagStatus.PropertyTag = "0x1090"; // PR_FLAG_STATUS
非常适用的Exchange 2007 Web Services
        pteftFlagStatus.PropertyType = MapiPropertyTypeType.Integer;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        FindItemType findItemRequest = new FindItemType();
非常适用的Exchange 2007 Web Services
        findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
非常适用的Exchange 2007 Web Services
        findItemRequest.ItemShape = new ItemResponseShapeType();
非常适用的Exchange 2007 Web Services
        findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
//findItemRequest.ItemShape.AdditionalProperties = new BasePathToElementType[2];
非常适用的Exchange 2007 Web Services
//findItemRequest.ItemShape.AdditionalProperties[0] = ptuftSubject;
非常适用的Exchange 2007 Web Services
//findItemRequest.ItemShape.AdditionalProperties[1] = pteftFlagStatus;
非常适用的Exchange 2007 Web Services
/**/////findItemRequest.ItemShape.AdditionalProperties[2] = ptuftBody;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        findItemRequest.ParentFolderIds = new FolderIdType[1];
非常适用的Exchange 2007 Web Services
        findItemRequest.ParentFolderIds[0] = firmtInbox.Folders[0].FolderId;
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
        FindItemResponseType firt = exchangeServer.FindItem(findItemRequest);
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
foreach (FindItemResponseMessageType firmtMessage in firt.ResponseMessages.Items)
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
{
非常适用的Exchange 2007 Web Services
if (firmtMessage.RootFolder.TotalItemsInView > 0)
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
{
非常适用的Exchange 2007 Web Services
foreach (ItemType it in ((ArrayOfRealItemsType)firmtMessage.RootFolder.Item).Items)
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
{
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("标题: {0} <br>", it.Subject));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("发件人: {0} <br>", ((cn.cwe.mail.MessageType)(it)).From.Item.Name));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("收件人: {0} <br>", it.DisplayTo));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("抄送人: {0} <br>", it.DisplayCc));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("大小: {0} <br>", it.Size.ToString()));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("重要性: {0} <br>", it.Importance.ToString()));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("是否已读: {0} <br>", ((cn.cwe.mail.MessageType)(it)).IsRead.ToString()));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("附件: {0} <br>", it.HasAttachments.ToString()));
非常适用的Exchange 2007 Web Services
                     Response.Write(string.Format("接收时间: {0} <br>", it.DateTimeReceived.ToString()));
非常适用的Exchange 2007 Web Services
if (it.Body != null)
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
{
非常适用的Exchange 2007 Web Services
                         Response.Write(string.Format("正文: {0} <br>", it.Body.Value));
非常适用的Exchange 2007 Web Services
                     }
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
if (null != it.ExtendedProperty)
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
{
非常适用的Exchange 2007 Web Services
                         Response.Write(string.Format("Prop 0x1090: {0}\n\n", it.ExtendedProperty[0].Item.ToString()));
非常适用的Exchange 2007 Web Services
                    }
非常适用的Exchange 2007 Web Services
else
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
非常适用的Exchange 2007 Web Services
{
非常适用的Exchange 2007 Web Services
                         Response.Write(string.Format("Prop 0x1090: not found"));
非常适用的Exchange 2007 Web Services
                    }
非常适用的Exchange 2007 Web Services
                }
非常适用的Exchange 2007 Web Services
            }

比起 Exchange2003那复杂的Exchange-SQL ,这个版本的Services规范多了,比如:

   findItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

给一个准备查询动作做限制,也可以是OnlyId,Default,我们只取我们想要的数据,类似 Select * from XXX的问题,这里不细细提出,同时还有:

        PathToUnindexedFieldType ptuftSubject = new PathToUnindexedFieldType();

        ptuftSubject.FieldURI = UnindexedFieldURIType.itemSubject;

不同的Path,不同的“字段”,太妙了 .....

   最后想题下那个短信猫代理dll,好几百RMB,不知有没有绿色版本的!当然了上面的做法其实用2007的POP3协议可以不用代码的,但如果想在MOSS的webpart中取出展示还是方便些。。。。。