Error in connecting to an exchange server RRS feed

  • Question

  • I have written a VB.NET application which needs to go to my exchange server and fetch my emails. The code is below.

    Dim loRequest As System.Net.HttpWebRequest
            Dim loResponse As System.Net.HttpWebResponse
            Dim loCredentials As System.Net.CredentialCache
            Dim lsRootUri, lsQuery As String
            Dim laBytes() As Byte
            Dim loRequestStream As System.IO.Stream
            Dim loResponseStream As System.IO.Stream
            Dim loXmlDoc As System.Xml.XmlDocument
            Dim myProxy As IWebProxy


                lsQuery = "<?xml version=""1.0""?>" & _
                "<D:searchrequest xmlns:D = ""DAV:"" xmlns:m=""urn:schemas:httpmail:"">" & _
                "<D:sql>SELECT ""urn:schemas:httpmail:hasattachment"", ""DAV:displayname"", ""urn:schemas:httpmail:from"", ""urn:schemas:httpmail:subject"", ""urn:schemas:httpmail:htmldescription"" FROM """ & lsRootUri & """" & _
                " WHERE ""DAV:ishidden"" = false AND ""DAV:isfolder"" = false AND ""urn:schemas:httpmail:hasattachment"" = true" & _
                " AND ""urn:schemas:httpmail:read"" = false" & _

                loCredentials = New System.Net.CredentialCache

                loCredentials.Add(New System.Uri(lsRootUri), "NTLM", New System.Net.NetworkCredential("userid", "password"))
                loRequest = CType(System.Net.WebRequest.Create(lsRootUri), System.Net.HttpWebRequest)
                'loRequest.Credentials = loCredentials.GetCredential(New System.Uri(lsRootUri), "NTLM")
                myProxy = loRequest.Proxy
                myProxy.Credentials = New NetworkCredential("username", "password", "domain")

                loRequest.PreAuthenticate = True
                loRequest.AuthenticationLevel = Security.AuthenticationLevel.MutualAuthRequired
                'loRequest.Credentials = loCredentials

                loRequest.Proxy = myProxy
                loRequest.Method = "SEARCH"
                laBytes = System.Text.Encoding.UTF8.GetBytes(lsQuery)
                loRequest.ContentLength = laBytes.Length
                loRequestStream = loRequest.GetRequestStream
                loRequestStream.Write(laBytes, 0, laBytes.Length)

                loRequest.ContentType = "text/xml"
                loRequest.Headers.Add("Translate", "F")
                loRequest.PreAuthenticate = True
                loRequest.AuthenticationLevel = Security.AuthenticationLevel.MutualAuthRequested

                loResponse = CType(loRequest.GetResponse, System.Net.HttpWebResponse)
                loResponseStream = loResponse.GetResponseStream
                loXmlDoc = New System.Xml.XmlDocument
                ''loXmlDoc.Save(c_sTempFolder & "temp2.xml")

            Catch ex As Exception


            End Try

    When I run this code, I get an error message saying "Unauthorised" if I give my servername as the URI. I change the code to use the IP address of my server, then I get a different error saying "Proxy Authentication required". I use a proxy server to which has the same credentials as my exchange server. Am I missing something in the code or should some setting in my exchange server need to be changed? Please help me in this problem.
    Tuesday, January 20, 2009 7:28 AM


  • Thank you for contacting us and your interest in Exchange development.  This forum is for discussion around the technical content of the Exchange Server Protocols documentation.  

    There is a very helpful community of Exchange developers on the Exchange Development forum that may be able to assist you with your solution. I suggest you post your question to the TechNet Forum » Exchange Server » Development.

    When posting to this forum please provide background information about your environment and proposed solution such as:

    • Exchange Server version.
    • Description of your Exchange and IIS topology.

    Providing this background information will better enable other developers to help you with your solution. Also feel free to answer other posts on the forum.

    Developer Consultant
    Tuesday, January 20, 2009 5:35 PM