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

Comments (3) -

On 5/20/2008 10:27:23 AM smallshot Japan wrote:

smallshot

hi henning, i tried your code using VS2005 and I got the error:

The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?

I've already referenced System.Management but it is still giving the error.

Alos if it is not too much trouble, could you show the same example in VB.Net?

thanks!

On 10/4/2011 3:03:21 AM JuneC wrote:

JuneC

Very useful, Thank you Henning!

smallshot, here's the code for VB.NET (2010)
----------------------------------------------------------------
Imports System
Imports System.Management

Module Module1
    Sub Main()
        Dim scope As New ManagementScope("\\.\root\MicrosoftExchangeV2")
        Dim query As New ObjectQuery("select * from Exchange_Mailbox")
        Dim searcher As New ManagementObjectSearcher(scope, query)
        Dim mailbox As ManagementObject

        For Each mailbox In searcher.Get
            Console.WriteLine("Mailbox Name: {0}", mailbox("MailboxDisplayName"))
            Console.WriteLine("Size: {0} bytes", mailbox("Size"))
            Console.WriteLine("Size: {0} bytes", mailbox("StorageLimitInfo"))

            Console.WriteLine()
        Next
        Console.ReadLine()
    End Sub
End Module
----------------------------------------------------------------

On 5/20/2008 11:12:14 PM hkrause wrote:

hkrause

Hi SmallShot,

The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?
the example uses C# 3.0... so you'll have to replace the var keywords with their respective types:
ManagementScope scope = new ManagementScope("\\\\.\\root\\MicrosoftExchangeV2");
and so on...