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
ALogvinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MATTHEW_LCommented:
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.

http://www.exchangeserver2003.com/articles/showarticle.asp?id=1148632910
0
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.
0
MATTHEW_LCommented:
I agree 100%.

Good luck in your search.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

MATTHEW_LCommented:
0
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.
0
MATTHEW_LCommented:
What about at the bottom, seemed like something in C#
0
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.
0
sppenceCommented:
you can do it using the system.management 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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sppenceCommented:
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).
0
ALogvinAuthor Commented:
I'm trying it now, and will let you know...
0
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.

Thanks!!!
0
NSDCTCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.