Beispiel #1
0
        /// <summary>
        /// Выполняет поиск входящих писем с подтверждением об обработке файлов за указанный период
        /// </summary>
        /// <param name="begin">Дата начиная с которой будет происходить поиск писем</param>
        /// <returns></returns>
        public static ItemType[] GetMessages(DateTime begin)
        {
            ExchangeServiceBinding bind = new ExchangeServiceBinding();

            bind.Credentials = new NetworkCredential(AppHelper.Configuration.Exchange.Username.GetDecryptedString(),
                                                     AppHelper.Configuration.Exchange.Password.GetDecryptedString(), AppHelper.Configuration.Exchange.Domain);
            bind.Url = "https://" + AppHelper.Configuration.Exchange.ServerName + "/EWS/Exchange.asmx";

            FindItemType findType = new FindItemType();

            findType.Traversal           = ItemQueryTraversalType.Shallow;
            findType.ItemShape           = new ItemResponseShapeType();
            findType.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;

            DistinguishedFolderIdType folder = new DistinguishedFolderIdType();

            folder.Id = DistinguishedFolderIdNameType.inbox;
            findType.ParentFolderIds = new BaseFolderIdType[] { folder };

            IsEqualToType            isEq  = new IsEqualToType();
            PathToUnindexedFieldType uPath = new PathToUnindexedFieldType();

            uPath.FieldURI = UnindexedFieldURIType.messageFrom;
            FieldURIOrConstantType constType  = new FieldURIOrConstantType();
            ConstantValueType      constValue = new ConstantValueType();

            constValue.Value        = AppHelper.Configuration.Exchange.SenderName;
            constType.Item          = constValue;
            isEq.Item               = uPath;
            isEq.FieldURIOrConstant = constType;

            IsGreaterThanOrEqualToType isGrEq = new IsGreaterThanOrEqualToType();
            PathToUnindexedFieldType   uPath2 = new PathToUnindexedFieldType();

            uPath2.FieldURI = UnindexedFieldURIType.itemDateTimeSent;
            FieldURIOrConstantType constType2  = new FieldURIOrConstantType();
            ConstantValueType      constValue2 = new ConstantValueType();

            constValue2.Value         = string.Format("{0}-{1}-{2}T00:00:00Z", begin.Year, begin.Month.ToString("D2"), begin.Day.ToString("D2"));
            constType2.Item           = constValue2;
            isGrEq.Item               = uPath2;
            isGrEq.FieldURIOrConstant = constType2;

            AndType and = new AndType();

            and.Items = new SearchExpressionType[] { isEq, isGrEq };

            findType.Restriction      = new RestrictionType();
            findType.Restriction.Item = and;

            FindItemResponseType        findResp = bind.FindItem(findType);
            FindItemResponseMessageType resMes   = findResp.ResponseMessages.Items[0] as FindItemResponseMessageType;

            if (resMes.ResponseClass != ResponseClassType.Success)
            {
                throw new Exception("Ошибка при получении ответа от сервера Exchange:\n" + resMes.MessageText);
            }
            ItemType[] messages = (resMes.RootFolder.Item as ArrayOfRealItemsType).Items;

            return(messages);
        }
    static FolderIdType FindFolder(ExchangeServiceBinding esb, DistinguishedFolderIdType fiFolderID, String fnFldName)
    {
        FolderIdType rvFolderID = new FolderIdType();

        // Create the request and specify the travesal type
        FindFolderType findFolderRequest = new FindFolderType();
        findFolderRequest.Traversal = FolderQueryTraversalType.Deep;

        // Define the properties returned in the response
        FolderResponseShapeType responseShape = new FolderResponseShapeType();
        responseShape.BaseShape = DefaultShapeNamesType.Default;
        findFolderRequest.FolderShape = responseShape;

        // Identify which folders to search
        DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
        folderIDArray[0] = new DistinguishedFolderIdType();
        folderIDArray[0].Id = fiFolderID.Id;

        //Add Restriction for DisplayName
        RestrictionType ffRestriction = new RestrictionType();
        IsEqualToType ieToType = new IsEqualToType();
        PathToUnindexedFieldType diDisplayName = new PathToUnindexedFieldType();
        diDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName;
        FieldURIOrConstantType ciConstantType = new FieldURIOrConstantType();
        ConstantValueType cvConstantValueType = new ConstantValueType();
        cvConstantValueType.Value = fnFldName;
        ciConstantType.Item = cvConstantValueType;
        ieToType.Item = diDisplayName;
        ieToType.FieldURIOrConstant = ciConstantType;
        ffRestriction.Item = ieToType;
        findFolderRequest.Restriction = ffRestriction;

        // Add the folders to search to the request
        findFolderRequest.ParentFolderIds = folderIDArray;

        try
        {
            // Send the request and get the response
            FindFolderResponseType findFolderResponse = esb.FindFolder(findFolderRequest);

            // Get the response messages
            ResponseMessageType[] rmta = findFolderResponse.ResponseMessages.Items;

            foreach (ResponseMessageType rmt in rmta)
            {
                // Cast to the correct response message type
                FindFolderResponseMessageType ffResponse = (FindFolderResponseMessageType)rmt;

                foreach (FolderType fFoundFolder in ffResponse.RootFolder.Folders)
                {
                    rvFolderID = fFoundFolder.FolderId;
                }
            }
        }
        catch (Exception e)
        {
            string problem = e.Message;
        }

        return rvFolderID;
    }