天天看點

非常适用的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中取出展示還是友善些。。。。。