昨天已经搞定了从服务器端得到Mailbox的size,但是这样不是很方便,需要在Exchange服务器上发布代码,今天继续研究从客户端得到这个Size,历经磨难终于成功:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.exchange2007;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;

namespace ConsoleApplication1
{
    class EWS
    {
        public static void Run()
        {
            ServicePointManager.ServerCertificateValidationCallback =
            delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
            {
                return true;
            };
            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.RequestServerVersionValue = new RequestServerVersion();
            esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007;
            esb.Credentials = new NetworkCredential("Administrator", "password", "domain.com");
            esb.Url = "https://exchange2007/EWS/Exchange.asmx";

            Console.WriteLine("Administrator:" + GetMailboxSize(esb, "Administrator@domain.com"));
            Console.WriteLine("cherami:" + GetMailboxSize(esb, "cherami@domain.com"));
            Console.WriteLine("ES1Test1:" + GetMailboxSize(esb, "ES1Test1@domain.com"));

            Console.ReadLine();
        }

        public static long GetMailboxSize(ExchangeServiceBinding esb, string emailAddress)
        {
            return GetFolderTotalSize(esb, DistinguishedFolderIdNameType.root, emailAddress);
        }

        public static BaseFolderIdType[] GetFolderIds(DistinguishedFolderIdNameType id, string emailAddress)
        {
            DistinguishedFolderIdType folderIdType = new DistinguishedFolderIdType();
            folderIdType.Id = id;
            folderIdType.Mailbox = new EmailAddressType();
            folderIdType.Mailbox.EmailAddress = emailAddress;
            return new BaseFolderIdType[] { folderIdType };
        }

        public static FolderResponseShapeType GetFolderResponseShapeType()
        {
            // Identify the folder properties to return.
            FolderResponseShapeType properties = new FolderResponseShapeType();
            PathToUnindexedFieldType ptuft = new PathToUnindexedFieldType();
            ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation;
            //Return folder size property
            PathToExtendedFieldType folderSizePath = new PathToExtendedFieldType();
            folderSizePath.PropertyTag = "0x0e08";
            folderSizePath.PropertyType = MapiPropertyTypeType.Integer;
            BasePathToElementType[] ptufts = new BasePathToElementType[] { ptuft, folderSizePath };
            properties.AdditionalProperties = ptufts;
            properties.BaseShape = DefaultShapeNamesType.AllProperties;
            return properties;
        }

        public static long GetFolderTotalSize(ExchangeServiceBinding esb, DistinguishedFolderIdNameType id, string emailAddress)
        {
            return GetFolderSize(esb, id, emailAddress) + GetAllSubFolderSize(esb, id, emailAddress);
        }

        public static long GetFolderSize(ExchangeServiceBinding esb, DistinguishedFolderIdNameType id, string emailAddress)
        {
            long size = 0;

            // Form the get folder request.
            GetFolderType request = new GetFolderType();
            request.FolderIds = GetFolderIds(id, emailAddress);
            request.FolderShape = GetFolderResponseShapeType();
            try
            {
                // Send the request and get the response.
                GetFolderResponseType response = esb.GetFolder(request);
                ArrayOfResponseMessagesType aormt = response.ResponseMessages;
                ResponseMessageType[] rmta = aormt.Items;

                foreach (ResponseMessageType rmt in rmta)
                {
                    if (rmt.ResponseClass == ResponseClassType.Success)
                    {
                        FolderInfoResponseMessageType firmt;
                        firmt = (rmt as FolderInfoResponseMessageType);
                        BaseFolderType[] folders = firmt.Folders;

                        foreach (BaseFolderType folder in folders)
                        {
                            size = size + Int64.Parse(folder.ExtendedProperty[0].Item.ToString());
                            //Console.WriteLine(folder.DisplayName + ":" + folder.ExtendedProperty[0].Item.ToString());
                        }
                    }
                    else
                    {
                        Console.WriteLine("Response class is not success" + id.ToString());
                    }
                }
            }
            catch (WebException ex)
            {
                throw ex;
            }
            return size;
        }

        public static long GetAllSubFolderSize(ExchangeServiceBinding esb, DistinguishedFolderIdNameType id, string emailAddress)
        {
            long size = 0;
            // Form the finder folder request.
            FindFolderType request = new FindFolderType();
            request.ParentFolderIds = GetFolderIds(id, emailAddress);
            request.FolderShape = GetFolderResponseShapeType();
            request.Traversal = FolderQueryTraversalType.Deep;
            try
            {
                // Send the request and get the response.
                FindFolderResponseType response = esb.FindFolder(request);
                ArrayOfResponseMessagesType aormt = response.ResponseMessages;
                ResponseMessageType[] rmta = aormt.Items;

                foreach (FindFolderResponseMessageType rmt in rmta)
                {
                    if (rmt.ResponseClass == ResponseClassType.Success)
                    {
                        BaseFolderType[] folders = rmt.RootFolder.Folders;

                        foreach (BaseFolderType folder in folders)
                        {
                            size = size + Int64.Parse(folder.ExtendedProperty[0].Item.ToString());
                            //Console.WriteLine(folder.DisplayName + ":" + folder.ExtendedProperty[0].Item.ToString());
                        }
                    }
                    else
                    {
                        throw new ApplicationException("Response class is not success");
                    }
                }
            }
            catch (WebException ex)
            {
                throw ex;
            }
            return size;
        }
    }
}

(Visited 39 times, 1 visits today)