title: Programmatically reading emails from Exchange Server 2010 mailbox
body: <p>we have a c# application that reads an email Inbox currently hosted on Exchange 2003 using the http service. </p>
  
  <p>Now the mailbox is to be migrated to an Exchange 2010 server, so we are testing our code to confirm it will still work.</p>
  
  <p>We are getting an error 'Bad request' with the below code (which tries to get all the mail):
          public static XmlDocument GetUnreadMailAll()
          {
              HttpWebRequest loRequest = default(HttpWebRequest);
              HttpWebResponse loResponse = default(HttpWebResponse);
              string lsRootUri = null;
              string lsQuery = null;
              byte[] laBytes = null;</p>
  
  <pre><code>        Stream loRequestStream = default(Stream);
          Stream loResponseStream = default(Stream);
          XmlDocument loXmlDoc = default(XmlDocument);
          loXmlDoc = new XmlDocument();
          try
          {
              lsRootUri = strServer + "/Exchange/" + strAlias + "/" + strInboxURL;
              lsQuery = "&lt;?xml version=\"1.0\"?&gt;"
                  + "&lt;D:searchrequest xmlns:D = \"DAV:\" xmlns:m=\"urn:schemas:httpmail:\"&gt;"
                  + "&lt;D:sql&gt;SELECT "
                  + "\"urn:schemas:httpmail:to\", "
                  + "\"urn:schemas:httpmail:displayto\", "
                  + "\"urn:schemas:httpmail:from\", "
                  + "\"urn:schemas:httpmail:fromemail\", "
                  + "\"urn:schemas:httpmail:subject\", "
                  + "\"urn:schemas:httpmail:textdescription\", "
                  //+ "\"urn:schemas:httpmail:htmldescription\", "
                  + "\"urn:schemas:httpmail:hasattachment\", "
                  + "\"urn:schemas:httpmail:attachmentfilename\", "
                  + "\"urn:schemas:httpmail:senderemail\", "
                  + "\"urn:schemas:httpmail:sendername\", "
                  + "\"DAV:displayname\", "
                  + "\"urn:schemas:httpmail:datereceived\", "
                  + "\"urn:schemas:httpmail:read\", "
                  + "\"DAV:id\" "
                  + "FROM \"" + lsRootUri
                  + "\" WHERE \"DAV:ishidden\" = false "
                  + "AND \"DAV:isfolder\" = false "
                  + "AND \"urn:schemas:httpmail:read\" = false "
                  + "AND \"urn:schemas:httpmail:fromemail\" != 'emailAddy@domainName.co.uk' "
                  + "&lt;/D:sql&gt;&lt;/D:searchrequest&gt;";
              loRequest = (HttpWebRequest)WebRequest.Create(lsRootUri);
              loRequest.Credentials = new NetworkCredential(strUserName, strPassword);
              loRequest.Method = "SEARCH";
              laBytes = System.Text.Encoding.UTF8.GetBytes(lsQuery);
              loRequest.ContentLength = laBytes.Length;
              loRequestStream = loRequest.GetRequestStream();
              loRequestStream.Write(laBytes, 0, laBytes.Length);
              loRequestStream.Close();
              loRequest.ContentType = "text/xml";
              loRequest.Headers.Add("Translate", "F");
              loResponse = (HttpWebResponse)loRequest.GetResponse();
              loResponseStream = loResponse.GetResponseStream();
              loXmlDoc.Load(loResponseStream);
              loResponseStream.Close();
          }
  </code></pre>
  
  <p>the exception is thrown on the line <code>loResponseStream = loResponse.GetResponseStream();</code></p>
  
  <p>here is the xml that we are sending:</p>
  
  <pre><code>  &lt;?xml version="1.0" ?&gt; 
  - &lt;D:searchrequest xmlns:D="DAV:" xmlns:m="urn:schemas:httpmail:"&gt;
    &lt;D:sql&gt;SELECT "urn:schemas:httpmail:to", "urn:schemas:httpmail:displayto", "urn:schemas:httpmail:from", "urn:schemas:httpmail:fromemail", "urn:schemas:httpmail:subject", "urn:schemas:httpmail:textdescription", "urn:schemas:httpmail:hasattachment", "urn:schemas:httpmail:attachmentfilename", "urn:schemas:httpmail:senderemail", "urn:schemas:httpmail:sendername", "DAV:displayname", "urn:schemas:httpmail:datereceived", "urn:schemas:httpmail:read", "DAV:id" FROM "https://domain/Exchange/bbtest/Inbox" WHERE "DAV:ishidden" = false AND "DAV:isfolder" = false AND "urn:schemas:httpmail:read" = false AND "urn:schemas:httpmail:fromemail" != 'emailAddy@domainName.co.uk'&lt;/D:sql&gt; 
    &lt;/D:searchrequest&gt;
  </code></pre>