InfiniTec - Henning Krauses Blog

Don't adjust your mind - it's reality that is malfunctioning

Getting Mailbox Information On Exchange 2003 using WMI

A recent question in the Microsoft development forum for Exchange was how to get information about mailboxes on Exchange 2003 from an ASP.NET application.

The simplest solution is to use WMI in this case. Exchange exposes certain mailbox statistics via the Exchange_Mailbox WMI class. The following code can be used to iterate through all mailboxes and print some of the properties:

   1: using System;
   2: using System.Management;
   3:  
   4: namespace MailboxSizer
   5: {
   6:     class Program
   7:     {
   8:         static void Main(string[] args)
   9:         {
  10:             var scope = new ManagementScope("\\\\.\\root\\MicrosoftExchangeV2");
  11:             var query = new ObjectQuery("select * from Exchange_Mailbox");
  12:             var searcher = new ManagementObjectSearcher(scope, query);
  13:             foreach (ManagementObject mailbox in searcher.Get())
  14:             {
  15:                 Console.WriteLine("Display name: {0}", mailbox["MailboxDisplayName"]);
  16:                 Console.WriteLine("Size: {0} bytes", mailbox["Size"]);
  17:                 Console.WriteLine("Storage Limit: {0}", (StorageLimitInfo) (uint) mailbox["StorageLimitInfo"]);
  18:  
  19:                 Console.WriteLine();
  20:             }
  21:             Console.ReadLine();
  22:         }
  23:     }
  24:  
  25:     [Flags]
  26:     internal enum StorageLimitInfo
  27:     {
  28:         BelowLimit = 1,
  29:         IssueWarning = 2,
  30:         ProhibitSend = 4,
  31:         NoChecking = 8,
  32:         MailboxDisabled = 16
  33:     }
  34: }

You need to reference the System.Management assembly for this code to work.


Posted by Henning Krause on Monday, May 19, 2008 8:00 PM, last modified on Tuesday, May 20, 2008 11:51 AM
Permalink | Post RSSRSS comment feed