none
WebDAV 'Access is Denied' exception (C#)

    Question

  • Hi,

    I'm trying to program in C# to access a MS Exchange 2003 Server. 

    I got this sample code, but the System.UnauthorizedAccessException: 'Access is denied' exception keeps popping up. 

    The code is as follows,

    using System;
    using System.Reflection;
    
    //How to list folders and items in a mailbox on an Exchange 2000 Server
    //computer by using WebDAV in Visual C#
    
    namespace WebDav
    {
        class Class1
        {
            public static void Main(String [] args)
            {
                try
                {
                    MSXML2.XMLHTTP30 oXMLHttp = new MSXML2.XMLHTTP30();
    
                    //TODO: To list items, uncomment "const bool bListItems = false;"
                    const bool bListFolders = true;
                    //const bool bListItems = false;
                    String sUrl;
    
                    //TODO: Replace ExchServer with your Exchange server and UserAlias
                    //with your user alias.
                    sUrl= "http://www.entermo.com/exchange/username/Inbox";;
    
    
                    oXMLHttp.open("SEARCH", sUrl, false, "entermo.com\\username",
                    "password");
    
                    //TODO: Use this query to list items.
                    /*String sQuery;
                    sQuery = "<?xml version='1.0'?>" +
                    "<g:searchrequest xmlns:g='DAV:'>" +
                    "<g:sql>SELECT \"DAV:displayname\" " +
                    "FROM SCOPE('SHALLOW TRAVERSAL OF \"" + sUrl + "\"')" +
                    " WHERE \"DAV:isfolder\" = " + bListItems.ToString()+
    
                    "</g:sql>" +
                    "</g:searchrequest>";
                    */
    
    
                    //TODO: Use this query to list folders.
                    String sQuery;
                    sQuery = "<?xml version='1.0'?>" +
                    "<g:searchrequest xmlns:g='DAV:'>" +
                    "<g:sql>SELECT \"DAV:displayname\" " +
                    "FROM SCOPE('SHALLOW TRAVERSAL OF \"" + sUrl + "\"')" +
                    " WHERE \"DAV:isfolder\" = " + bListFolders.ToString() +
                    "</g:sql>" +
                    "</g:searchrequest>";
    
    
                    oXMLHttp.setRequestHeader("Content-Type", "text/xml");
                    oXMLHttp.setRequestHeader("Translate", "f");
                    oXMLHttp.setRequestHeader("Content-Length", "" + sQuery.Length);
                    Console.WriteLine(sQuery);
                    oXMLHttp.send(sQuery);
    
                    Console.WriteLine(oXMLHttp.status);
                    Console.WriteLine(oXMLHttp.statusText);
                    Console.WriteLine(oXMLHttp.responseText);
                }
                catch (Exception e)
                {
                    Console.WriteLine("{0} Exception caught.", e);
                }
                Console.ReadLine();
            }
        }
    }

    I was wondering if this has to do with the server certificates not being stored or something like that?

    Help would be most appreciated.

    Thank you!
    Monday, February 22, 2010 7:38 AM

All replies