Share via


Getting the Size of a Mailbox (WebDAV)

Getting the Size of a Mailbox (WebDAV)

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

There is no mailbox property that contains the total size of the mailbox. However, each folder in a user mailbox has its size stored in the https://schemas.microsoft.com/mapi/proptag/x0e080003 field, which corresponds to the PR_MESSAGE_SIZE MAPI property. The following examples use the SEARCH Method to add the values of the https://schemas.microsoft.com/mapi/proptag/x0e080003 property for each subfolder in a mailbox, thus determining the total size of the mailbox.

See Constructing Exchange Store HTTP URLs and Authentication and Security Using WebDAV for more information.

This topic contains Microsoft® Visual Basic® Scripting Edition (VBScript), Microsoft C#, and Visual Basic .NET code examples.

VBScript

Option Explicit

On Error Resume Next

' Declare variables to be used.
Dim iSum             ' As Integer
Dim strUri           ' As String
Dim strMsg           ' As String
Dim strServerName    ' As String
Dim strMailbox       ' As String
Dim strUserName      ' As String
Dim strPassword      ' As String
Dim i                ' As Integer
Dim oXMLHttp         ' As Msxml2.XMLHTTP
Dim oXMLDoc          ' As Msxml2.DOMDocument
Dim oXMLSizeNodes    ' As IXMLDOMNodeList
Dim oXMLHREFNodes    ' As IXMLDOMNodeList
Dim sQuery           ' As String

' Initialize variables.
iSum = 0

' The URI of the mailbox on the Exchange server.
strUri = "https://ServerName/exchange/UserName/non_ipm_subtree"

' Create XMLHTTP object.
Set oXMLHttp = CreateObject("Msxml2.xmlhttp")

' Error checking.
If Err.Number <> 0 Then
   wscript.Echo "Error Creating XMLHTTP object"
   wscript.Echo Err.Number & ": " & Err.Description
   Set oXMLHttp = Nothing
   wscript.quit
End If

' Specify the SEARCH method, the URI, that the request will be sent
' asynchronously, and the credentials for the mailbox.
oXMLHttp.open "SEARCH", strUri, False, "Domain\UserName", "!Password"

' Build the query string for a deep traversal search
' on the x0e080003 mapi proptag.
sQuery = "<?xml version=""1.0""?>"
sQuery = sQuery & "<g:searchrequest xmlns:g=""DAV:"">"
sQuery = sQuery & "<g:sql>SELECT ""https://schemas.microsoft.com/"
sQuery = sQuery & "mapi/proptag/x0e080003"" FROM SCOPE "
sQuery = sQuery & "('DEEP TRAVERSAL OF """ & strUri & """') "
sQuery = sQuery & "WHERE ""DAV:isfolder"" = true"
sQuery = sQuery & "</g:sql>"
sQuery = sQuery & "</g:searchrequest>"

' Set the request headers.
oXMLHttp.setRequestHeader "Content-Type", "text/xml"
oXMLHttp.setRequestHeader "Translate", "f"
oXMLHttp.setRequestHeader "Depth", "0"
oXMLHttp.setRequestHeader "Content-Length", "" & Len(sQuery)

' Send the SEARCH request.
oXMLHttp.send sQuery
If Err.Number <> 0 Then
   wscript.Echo "Error sending query."
   wscript.Echo Err.Number & ": " & Err.Description
   Set oXMLHttp = Nothing
   wscript.quit
End If

' An error occurred on the server.
If oXMLHttp.status >= 500 Then
   wscript.echo "Status: " & oXMLHttp.status
   wscript.echo "Status text: " & "An error occurred on the server."
   wscript.quit

' The SEARCH request was successful.  Display the inbox folder names, sizes,
' and total size of the mailbox.
ElseIf oXMLHttp.status = 207 Then

   ' Get the XML response body.
   Set oXMLDoc = oXMLHttp.responseXML
   wscript.echo oXMLHttp.responsetext

   ' Build a list of the https://schemas.microsoft.com/mapi/proptag/x0e080003
   ' XML nodes returned in the search request. The
   ' https://schemas.microsoft.com/mapi/proptag/ namespace is typically
   ' assigned the d: prefix in the XML response body.
   Set oXMLSizeNodes = oXMLDoc.getElementsByTagName("d:x0e080003")

   ' Build a list of the DAV:href XML nodes, corresponding to the folders
   ' in the mailbox.  The DAV: namespace is typically assgigned the a:
   ' prefix in the XML response body.
   Set oXMLHREFNodes = oXMLDoc.getElementsByTagName("a:href")

   ' Loop through the list of nodes and total up the sizes of the
   ' mailbox folders.
   For i = 0 to oXMLSizeNodes.length - 1

     ' Display the folder URL.
     wscript.Echo "Folder: " & oXMLHREFNodes.Item(i).nodeTypedValue

     ' Display the folder size.
     wscript.Echo "Size: " & oXMLSizeNodes.Item(i).nodeTypedValue & " bytes."
     wscript.echo ""

     ' Add the folder size to the running total.
     iSum = iSum + oXMLSizeNodes.Item(i).nodeTypedValue

   Next

   ' Display the total size of the mailbox in bytes.
   wscript.Echo "Mailbox Size: " & iSum & " bytes."

   ' Clean up.
   Set oXMLSizeNodes = Nothing
   Set oXMLDoc = Nothing

Else
   ' Display the SEARCH status and status text.
   wscript.echo "Status: " & oXMLHttp.status
   wscript.echo "Status text: " & oXMLHttp.statustext
   wscript.quit
End If

' Clean up.
Set oXMLHttp = Nothing


C#

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;

namespace ExchangeSDK.Snippets.CSharp
{
   class GettingSizeOfMailboxWebDAV
   {
      [STAThread]
      static void Main(string[] args)
      {
         // Variables.
         System.Net.HttpWebRequest Request = null;
         System.Net.WebResponse Response = null;
         System.Net.CredentialCache MyCredentialCache = null;
         string strMailboxURI = "https://Server/exchange/UserName/non_ipm_subtree/";
         string strUserName = "UserName";
         string strPassword = "!Password";
         string strDomain = "Domain";
         string strQuery ="";
         byte[] bytes = null;
         System.IO.Stream RequestStream = null;
         System.IO.Stream ResponseStream = null;
         XmlDocument ResponseXmlDoc = null;
         XmlNodeList HrefNodes = null;
         XmlNodeList SizeNodes = null;
         int iMailboxSize = 0;

         try
         {
            // Build the SQL query.
            strQuery = "<?xml version=\"1.0\"?>";
            strQuery += "<g:searchrequest xmlns:g=\"DAV:\">";
            strQuery += "<g:sql>SELECT \"https://schemas.microsoft.com/";
            strQuery += "mapi/proptag/x0e080003\" FROM SCOPE ";
            strQuery += "('DEEP TRAVERSAL OF \"" + strMailboxURI + "\"') ";
            strQuery += "WHERE \"DAV:isfolder\" = true";
            strQuery += "</g:sql>";
            strQuery += "</g:searchrequest>";

            // Create a new CredentialCache object and fill it with the network
            // credentials required to access the server.
            MyCredentialCache = new System.Net.CredentialCache();
            MyCredentialCache.Add( new System.Uri(strMailboxURI),
               "NTLM",
               new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
               );

            // Create the HttpWebRequest object.
            Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strMailboxURI);

            // Add the network credentials to the request.
            Request.Credentials = MyCredentialCache;

            // Specify the method.
            Request.Method = "SEARCH";

            // Encode the body using UTF-8.
            bytes = Encoding.UTF8.GetBytes((string)strQuery);

            // Set the content header length.  This must be
            // done before writing data to the request stream.
            Request.ContentLength = bytes.Length;

            // Get a reference to the request stream.
            RequestStream = Request.GetRequestStream();

            // Write the SQL query to the request stream.
            RequestStream.Write(bytes, 0, bytes.Length);

            // Close the Stream object to release the connection
            // for further use.
            RequestStream.Close();

            // Set the Content Type header.
            Request.ContentType = "text/xml";

            // Set the Translate header.
            Request.Headers.Add("Translate", "F");

            // Send the SEARCH method request and get the
            // response from the server.
            Response = (HttpWebResponse)Request.GetResponse();

            // Get the XML response stream.
            ResponseStream = Response.GetResponseStream();

            // Create the XmlDocument object from the XML response stream.
            ResponseXmlDoc = new XmlDocument();
            ResponseXmlDoc.Load(ResponseStream);

            // Build a list of the DAV:href XML nodes, corresponding to the folders
            // in the mailbox.  The DAV: namespace is typically assgigned the a:
            // prefix in the XML response body.
            HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:href");

            // Build a list of the https://schemas.microsoft.com/mapi/proptag/x0e080003
            // XML nodes returned in the search request. The
            // https://schemas.microsoft.com/mapi/proptag/ namespace is typically
            // assigned the d: prefix in the XML response body.
            SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:x0e080003");

            // Loop through the list of nodes and total up the sizes of the
            // mailbox folders.
            for(int i=0; i<HrefNodes.Count; i++)
            {
               // Display the folder URI.
               Console.WriteLine("Folder: " + HrefNodes[i].InnerText);

               // Display the folder size.
               Console.WriteLine("Size: " + SizeNodes[i].InnerText + " bytes.");
               Console.WriteLine("");

               // Add the folder size to the running total.
               iMailboxSize += Convert.ToInt32(SizeNodes[i].InnerText);
            }

            // Display the total size of the mailbox.
            Console.WriteLine("Mailbox size: " + iMailboxSize + " bytes.");

            // Clean up.
            ResponseStream.Close();
            Response.Close();

         }
         catch(Exception ex)
         {
            // Catch any exceptions. Any error codes from the SEARCH
            // method request on the server will be caught here, also.
            Console.WriteLine(ex.Message);
         }
      }
   }
}

Visual Basic .NET

Option Explicit On
Option Strict On

Module Module1

Sub Main()

   ' Variables
   Dim Request As System.Net.HttpWebRequest
   Dim Response As System.Net.HttpWebResponse
   Dim MyCredentialCache As System.Net.CredentialCache
   Dim strServer As String
   Dim strPassword As String
   Dim strDomain As String
   Dim strUserName As String
   Dim strMailboxURI As String
   Dim strQuery As String
   Dim bytes() As Byte
   Dim RequestStream As System.IO.Stream
   Dim ResponseStream As System.IO.Stream
   Dim ResponseXmlDoc As System.Xml.XmlDocument
   Dim HrefNodes As System.Xml.XmlNodeList
   Dim SizeNodes As System.Xml.XmlNodeList
   Dim iMailboxSize As Integer

   Try
      ' Initialize variables.
      strMailboxURI = "https://server/exchange/UserName/non_ipm_subtree/"
      strUserName = "UserName"
      strPassword = "!Password"
      strDomain = "Domain"
      iMailboxSize = 0

      ' Build the SQL query.
      strQuery = "<?xml version=""1.0""?>" & _
         "<g:searchrequest xmlns:g=""DAV:"">" & _
         "<g:sql>SELECT ""https://schemas.microsoft.com/" & _
         "mapi/proptag/x0e080003"" FROM SCOPE " & _
         "('DEEP TRAVERSAL OF """ & strMailboxURI & """') " & _
         "WHERE ""DAV:isfolder"" = true" & _
         "</g:sql>" & _
         "</g:searchrequest>"


      ' Create a new CredentialCache object and fill it with the network
      ' credentials required to access the server.
      MyCredentialCache = New System.Net.CredentialCache
      MyCredentialCache.Add(New System.Uri(strMailboxURI), _
         "NTLM", _
         New System.Net.NetworkCredential(strUserName, strPassword, strDomain) _
      )

      ' Create the PUT HttpWebRequest object.
      Request = CType(System.Net.WebRequest.Create(strMailboxURI), _
      System.Net.HttpWebRequest)

      ' Add the network credentials to the request.
      Request.Credentials = MyCredentialCache

      ' Specify the SEARCH method.
      Request.Method = "SEARCH"

      ' Encode the body using UTF-8.
      bytes = System.Text.Encoding.UTF8.GetBytes(strQuery)

      ' Set the content header length.  This must be
      ' done before writing data to the request stream.
      Request.ContentLength = bytes.Length

      ' Get a reference to the request stream.
      RequestStream = Request.GetRequestStream()

      ' Write the message body to the request stream.
      RequestStream.Write(bytes, 0, bytes.Length)

      ' Close the Stream object to release the connection
      ' for further use.
      RequestStream.Close()

      ' Set the Content Type header.
      Request.ContentType = "text/xml"

      ' Set the Translate header.
      Request.Headers.Add("Translate", "F")

      ' Send the SEARCH method request and get the
      ' response from the server.
      Response = CType(Request.GetResponse(), System.Net.HttpWebResponse)

      ' Get the XML response stream.
      ResponseStream = Response.GetResponseStream()

      ' Create the XmlDocument object from the XML response stream.
      ResponseXmlDoc = New System.Xml.XmlDocument
      ResponseXmlDoc.Load(ResponseStream)

      ' Build a list of the DAV:href XML nodes, corresponding to the folders
      ' in the mailbox.  The DAV: namespace is typically assgigned the a:
      ' prefix in the XML response body.
      HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:href")

      ' Build a list of the https://schemas.microsoft.com/mapi/proptag/x0e080003
      ' XML nodes returned in the search request. The
      ' https://schemas.microsoft.com/mapi/proptag/ namespace is typically
      ' assigned the d: prefix in the XML response body.
      SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:x0e080003")

      ' Loop through the list of nodes and total up the sizes of the
      ' mailbox folders.
      Dim i As Integer

      For i = 0 To HrefNodes.Count - 1

         ' Display the folder URI.
         Console.WriteLine("Folder: " & HrefNodes(i).InnerText)

         ' Display the folder size.
         Console.WriteLine("Size: " & SizeNodes(i).InnerText + " bytes.")
         Console.WriteLine("")

         ' Add the folder size to the running total.
         iMailboxSize += Convert.ToInt32(SizeNodes(i).InnerText)

      Next

      ' Display the total size of the mailbox.
      Console.WriteLine("Mailbox size: " & iMailboxSize & " bytes.")

      ' Clean up.
      ResponseStream.Close()
      Response.Close()

   Catch ex As Exception

      ' Catch any exceptions. Any error codes from the
      ' SEARCH method request on the server will be caught
      ' here, also.
      Console.WriteLine(ex.Message)

   End Try

End Sub

End Module

Send us your feedback about the Microsoft Exchange Server 2003 SDK.

This topic last updated: March 2004

Build: June 2007 (2007.618.1)

© 2003-2006 Microsoft Corporation. All rights reserved. Terms of use.