Check the Mailbox size of a remote user via C#

At my company we have over 5000 mailboxes. We currently have a older VBScript that runs on a daily basis that logs onto each mailbox, checks the total size of the mailbox, compares it to a hardcoded "limit" then sends the user an e-mail if they are getting close to full letting them know our companies mailbox policies and how to get help. I know Exchange 2003 (native) that we have has a capability similar to this, but you are unable to edit the message that they recieve so we can not use it... Exchange's message specifically tells people to use PST files which are against company policy. This program takes roughly 4-6 hours a day to run... its rather slow.

I've been tasked with updating this program... currently it will not run in a WindowsXP environment is is running on a legacy Windows2000 box.

I have been searching for quite some time and could not find an answer. Is there a way, useing C# which is my company standard, for me to get the current mailbox size of a user? I can easily pull their limits using DirectoryServices, but I just cant break into getting their current size.

I can not install anything on our Exchange servers themselves, nor can I modify any Exchange DLL's to modify their message. We have a Exchange 2003 native environment with about 6 servers.
LVL 11
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

sppenceConnect With a Mentor Commented:
you can do it using the namespace.  

Here's a sample (replace SERVERNAME with the name of your Exchange server - watch for line wrap...) - obviously, add your own code to the foreach to do something with the values once you get them.

            UInt64 mailboxsize;
            string storagegroupname;
            string storename;
            string mailboxdisplayname;

            System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher();
            searcher.Scope = new System.Management.ManagementScope("\\\\SERVERNAME\\root\\MicrosoftExchangeV2");
            searcher.Query = new System.Management.ObjectQuery("Select * From Exchange_Mailbox");
            System.Management.ManagementObjectCollection results = searcher.Get();
            foreach (System.Management.ManagementObject mailbox in results)
               mailboxsize = Convert.ToUInt64(mailbox.Properties["Size"].Value);
               storagegroupname = mailbox.Properties["StorageGroupName"].Value.ToString();
               storename = mailbox.Properties["StoreName"].Value.ToString();
               mailboxdisplayname = mailbox.Properties["MailboxDisplayName"].Value.ToString();

FYI, I've included a few of the properties availabe on a mailbox here, but there's over a dozen properties on the WMI object - you can see a complete list using WBEMTEST or I believe you can use the GetText method of the "mailbox" object above to show the full MOF text with all the properties and their values.

hope this helps.
I'm not a C# programmer at all so I can't help you there.

However, it is a shame that you cannot make 1 .dll modification.  Changing the native warning message actually isn't very difficult.  You could then run all of this straight through Exchange with no external applications or code.

Here is the article in case you are interested.  This .dll would have to be copied to each Exchange server, but would work nicley.  I have used this method in the past.
ALogvinAuthor Commented:
It is a matter of support. If we change 1 little DLL, Microsoft will just blame every little flaw in Exchange on that. We pay a lot for our support contract with MS to have them just dump the problem because we modified their stuff. God forbid they not hard-code in things like this.
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

I agree 100%.

Good luck in your search.
ALogvinAuthor Commented:
Yah, I've read that one a few times. All VBS, nothing in .NET at all. I cant run this on an Exchange server either, due to the reasons stated above.
What about at the bottom, seemed like something in C#
ALogvinAuthor Commented:
Nope. WebDav = VBScript, Visual Basic, Visual C.  There is a link for ADO, but ADO has to be run on the Exchange Server, and there are no C# methods, only C++ and VBS.
I forgot to mention above - you don't have to run this from the Exchange server, but the user you run this as does have to have rights to read that information from WMI (an account that is an admin on your Exchange server should work).
ALogvinAuthor Commented:
I'm trying it now, and will let you know...
ALogvinAuthor Commented:
That looks like it will work. I have a test domain that I am a full admin in, and it worked great there. It wont work in my production domain, but the only difference is my rights, so im sure once i get that resolved it will work great there too.

Is there a way that this can be done for a specific mailbox on the exchange server? The code appears to be searching the entire exchange server and I only want to check a specific mailbox. I'm also getting permission denied which I am hoping would go away if it was specific to the box that I care about (I do have permission to that). The credentials on the email box are not he same as the admin account on the server that this would run on so I may also need to be able to pass a specific username and passoword. Is this possible? Please let me know.
All Courses

From novice to tech pro — start learning today.