This example takes me a little further, but is throwing an exception on
the searcher.Get() in the foreach loop:
An unhandled exception of type
'System.Runtime.InteropServices.COMException' occurred in
system.management.dll
Additional information: Class not registered
I recoded this in c#, here it is:
using System;
using System.Diagnostics;
using System.Management;
namespace readExchange1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
static void Main(string[] args)
{
System.Management.ConnectionOptions connection = new
System.Management.ConnectionOptions();
connection.Impersonation =
System.Management.ImpersonationLevel.Impersonate;
connection.EnablePrivileges = true;
string cServername = "MAILXXXX";
connection.Username = "jbailo";
connection.Password = "xxxxxxxx";
connection.Authority = "ntlmdomain:XXXXX";
try
{
System.Management.ManagementScope exmangescope
= new System.Management.ManagementScope(@"\\" + cServername +
@"\root\MicrosoftExchangeV2",connection);
System.Management.ObjectQuery objquery = new
System.Management.ObjectQuery("SELECT * FROM Exchange_Mailbox WHERE
MailBoxDisplayName='jbailo'");
System.Management.ManagementObjectSearcher objsearch = new
System.Management.ManagementObjectSearcher(exmangescope,objquery);
//System.Management.ManagementObjectCollection
//queryCollection1 = objsearch.Get();
string strDisplay=string.empty;
foreach( System.Management.ManagementObject instmailbox
in objsearch.Get() ) {
strDisplay = instmailbox["MailboxDisplayName"].ToString() + " " +
instmailbox["size"].ToString();
System.Console.WriteLine(strDisplay);
}
} catch(System.Management.ManagementException me) {
Debug.WriteLine(me.ToString());}
catch(System.UnauthorizedAccessException uae){
Debug.WriteLine(uae.ToString());
}}}}
Are you connecting to an Exchange server on your local machine or another
server on your network? If so use this code:
Assuming you are doing this on an internal network and not from outside.
Pay
attention to "ENTERDOMAIN", "enterpassword", "SERVERNAME", "YOUR NAME".
Make
sure WMI has remote permissions enabled on the server. If you don't know
how
to do this or sys admin does not know how, email me off list to arrange a
remote desktop session with me.
Private Sub SimpleStuff()
Try
Dim connection As New Management.ConnectionOptions
connection.Username = "Administrator"
connection.Password = "enterpassword"
connection.Authority = "ntlmdomain:ENTERDOMAIN"
Dim scope As New Management.ManagementScope( _
"\\SERVERNAME\root\MicrosoftExchangeV2", connection)
scope.Connect()
Dim query As New Management.ObjectQuery( _
"SELECT * FROM Exchange_Mailbox WHERE
MailboxDisplayName
= 'YOUR NAME'")
Dim searcher As New
Management.ManagementObjectSearcher(scope,
query)
For Each queryObj As Management.ManagementObject In
searcher.Get()
Debug.WriteLine(queryObj("Size") & vbTab &
queryObj("TotalItems"))
'If these items are greater than 0 then exit For and do something with
it,
send an email or something
Next
Catch err As Management.ManagementException
MessageBox.Show("An error occurred while querying for WMI
data:
" & err.Message)
Catch unauthorizedErr As System.UnauthorizedAccessException
MessageBox.Show("Connection error (user name or password
might
be incorrect): " & unauthorizedErr.Message)
End Try
End Sub