lamp

Get the Unread count of the Inbox of Exchange 2010/2007, using web services

Published 02 February 10 09:28 AM | Frederick.Chapleau

I searched a lot and, based on an article from SANDEEP APARAJIT, I was able to query for unread email from my Exchange mailbox inbox, in about 15 min. Thanks!

 

ServicePointManager.ServerCertificateValidationCallback = delegate(Object obj, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; };

ExchangeServiceBinding service = new ExchangeServiceBinding ();

service.RequestServerVersionValue = new RequestServerVersion();
service.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010;
service.EnableDecompression = true;
service.Credentials = new NetworkCredential("username", "password", "domain");
service.Url = @https://exchangeserver/EWS/exchange.asmx;

int merCurrentCount = GetUnreadFolderItemsCount(service, "Inbox");

… and the functions…

public int GetUnreadFolderItemsCount(ExchangeServiceBinding service, string folderName)
       {
           int unReadCount = -1;
           // Identify the folder properties to return.
           FolderResponseShapeType properties = new FolderResponseShapeType();
           PathToUnindexedFieldType ptuft = new PathToUnindexedFieldType();
           ptuft.FieldURI = UnindexedFieldURIType.folderManagedFolderInformation;
           PathToUnindexedFieldType[] ptufts = new PathToUnindexedFieldType[1] { ptuft };
           properties.AdditionalProperties = ptufts;
           properties.BaseShape = DefaultShapeNamesType.AllProperties;

           // Form the get folder request.
           BaseFolderIdType p_folder = FindFolderID(service, folderName);

           GetFolderType request = new GetFolderType();
           request.FolderIds = new BaseFolderIdType[1] { p_folder };
           request.FolderShape = properties;

           // Send the request and get the response.
           GetFolderResponseType response = service.GetFolder(request);

           ArrayOfResponseMessagesType aormt = response.ResponseMessages;
           ResponseMessageType[] rmta = aormt.Items;
           foreach (ResponseMessageType rmt in rmta)
           {
               if (rmt.ResponseClass == ResponseClassType.Error)
               {
                   throw new Exception(rmt.MessageText);
               }
               else
               {
                   FolderInfoResponseMessageType firmt;
                   firmt = (rmt as FolderInfoResponseMessageType);
                   BaseFolderType[] folders = firmt.Folders;

                   foreach (BaseFolderType rfolder in folders)
                   {
                       if (rfolder is FolderType)
                       {
                           FolderType myFolder;
                           myFolder = (rfolder as FolderType);
                           if (myFolder.UnreadCountSpecified)
                           {
                               unReadCount = myFolder.UnreadCount;
                           }
                       }
                   }
               }
           }
           return unReadCount;
       }

       public static FolderIdType FindFolderID(ExchangeServiceBinding service, String folderName)
       {
           DistinguishedFolderIdType objSearchRootFolder = new DistinguishedFolderIdType();
           objSearchRootFolder.Id = DistinguishedFolderIdNameType.msgfolderroot;

           FindFolderType requestFindFolder = new FindFolderType();
           requestFindFolder.Traversal = FolderQueryTraversalType.Deep;
           requestFindFolder.ParentFolderIds = new DistinguishedFolderIdType[] { objSearchRootFolder };
           requestFindFolder.FolderShape = new FolderResponseShapeType();
           requestFindFolder.FolderShape.BaseShape = DefaultShapeNamesType.IdOnly;

           //Search filter definition
           requestFindFolder.Restriction = new RestrictionType();

           #region Contains expression

           ContainsExpressionType objContainsExpression = new ContainsExpressionType();
           objContainsExpression.ContainmentMode = ContainmentModeType.FullString;
           objContainsExpression.ContainmentModeSpecified = true;
           objContainsExpression.ContainmentComparison = ContainmentComparisonType.Exact;
           objContainsExpression.ContainmentComparisonSpecified = true;

           PathToUnindexedFieldType objFieldFolderName = new PathToUnindexedFieldType();
           objFieldFolderName.FieldURI = UnindexedFieldURIType.folderDisplayName;
           objContainsExpression.Item = objFieldFolderName;

           objContainsExpression.Constant = new ConstantValueType();
           objContainsExpression.Constant.Value = folderName;

           #endregion Contains expression

           requestFindFolder.Restriction.Item = objContainsExpression;

           FindFolderResponseType objFindFolderResponse =
               service.FindFolder(requestFindFolder);

           if (objFindFolderResponse.ResponseMessages.Items.Length == 0)
               return null;

           foreach (ResponseMessageType responseMsg in
               objFindFolderResponse.ResponseMessages.Items)
           {
               if (responseMsg.ResponseClass == ResponseClassType.Success)
               {
                   FindFolderResponseMessageType objFindResponse =
                       responseMsg as FindFolderResponseMessageType;
                   foreach (
                       BaseFolderType objFolderType in objFindResponse.RootFolder.Folders)
                   {
                       return objFolderType.FolderId;
                   }
               }
           }
           return null;
       }

-f.

Filed under:

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Protected by FormShield
Refresh
Listen
Please enter the characters shown on the image


Code: