microsoft.public.exchange.development
[Top] [All Lists]

Re: Query an Exchange server

Subject: Re: Query an Exchange server
From: "Samuel"
Date: Tue, 17 Oct 2006 11:20:27 +0400
Newsgroups: microsoft.public.exchange.development
Hello,
 
Thanks for your answer. It was usefull. I look the 2 links and tried to apply it to my exchange server but it doesn't work.
 
So I need a little more help to make it work correctly if it's possible.
For the moment, I work in C#.NET and have an Exchange server 2003. The program I want to make is on the Exchange server.
 
This is the code I use:
 
using System;
 
namespace Samples
{
    class Class1
    {
        static void Main(string[] args)
        {
            try
            {
                ADODB.Connection oCn = new ADODB.Connection();
            ADODB.Recordset oRs = new ADODB.Recordset();
 
            ADODB.Fields oFields;
            ADODB.Field oField;
 
            // TODO:
            string sFdUrl = "
http://192.168.3.1/Exchange/psam/";
   
            oCn.Provider = "exoledb.datasource";
            oCn.Open(sFdUrl, "psam", "
s@mp@yet1234", -1);
           
            if(oCn.State == 1)
            {
                Console.WriteLine("Good Connection");
            }
            else
            {
                Console.WriteLine("Bad Connection");
            }
 

            string strSql;
            strSql = "";
            strSql = "select ";
            strSql = strSql + " \"urn:schemas:mailheader:content-class\"";
            strSql = strSql + ", \"DAV:href\" ";
            strSql = strSql + ", \"urn:schemas:mailheader:content-class\" ";
            strSql = strSql + ", \"DAV:displayname\"";
            strSql = strSql + " from scope ('shallow traversal of " + "\"";
            strSql = strSql + sFdUrl + "\"') ";
            strSql = strSql + " WHERE \"DAV:ishidden\" = false";
            strSql = strSql + " AND \"DAV:isfolder\" = false";
 

            oRs.Open(strSql, oCn,
                ADODB.CursorTypeEnum.adOpenUnspecified,
                ADODB.LockTypeEnum.adLockOptimistic, 1);
 
            // As an example, you only retrieve the first message.
            // You can use a while loop through each message.
 
            // Get the first message.
            oRs.MoveFirst();
 
            // Get Recordset fields.
            oFields = oRs.Fields;
   
            string sUrl;
            oField = oFields["DAV:href"];
            sUrl = oField.Value.ToString();
 
            CDO.Message iMsg = new CDO.Message();
            iMsg.DataSource.Open(sUrl, oRs.ActiveConnection,
                ADODB.ConnectModeEnum.adModeReadWrite,
                ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
                ADODB.RecordOpenOptionsEnum.adOpenSource,
                "", "");
 
            Console.WriteLine("{0}", iMsg.Sender);
            Console.WriteLine("{0}", iMsg.Subject);
            Console.WriteLine("{0}", iMsg.TextBody);
 
            // Get message fields.
            oFields = iMsg.Fields;
 
   
            for(int i = 0; i < oFields.Count; i++)
            {
                oField = oFields[i];
                Console.WriteLine("{0} : {1}", oField.Name, oField.Value);
            }
 

            oRs.Close();
            oCn.Close();
   
            oCn = null;
            oRs = null;
            oFields = null;
            oField = null;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);
            }   
        }
    }
}
 
This code is the same I found on this site : http://support.microsoft.com/kb/310206 .
I change the name of the server, the username and the password. I also delete the "inbox" in the string url because if I let it, it doesn't work.
Maybye there's something wrong...
 
Any idea?
 
PAYET Samuel
 
 
"Henning Krause" <hkrause.no@xxxxxxxxxxxxxxxxx> a écrit dans le message de news: OC6sZbV8GHA.4996@xxxxxxxxxxxxxxxxxxxx...
> Hello,
>
> yes you can use SQL (sort of :-) )
>
> First thing you must do is to select your access method. SQL queries are
> available with the ExOleDB provider and WebDAV. Latter is more suited for
> managed code (.NET) and the only option in your scenario when you want to
> connect to the Exchange Server remotely.
>
> Other options to connect to the Exchange server are MAPI (low level),
> Outlook Object model or CDO.
>
> It really depends on the programming language you are using.
>
> With WebDAV, you can issue a SEARCH command on the mailbox and retrieve
> items based on a SQL-like query (see
>
http://msdn.microsoft.com/library/en-us/e2k3/e2k3/_webdav_search.asp and
>
http://msdn.microsoft.com/library/en-us/e2k3/e2k3/_esdk_key_tasks_search.asp).
>
> With ExOleDB (which works only locally on the Exchange server), you'll use
> ADO (See last link above).
>
> I hope, you are not to confused now.. :-)
>
> Best regards,
> Henning Krause
<Prev in Thread] Current Thread [Next in Thread>
Privacy Policy