简体   繁体   中英

Programmatically reading emails from Exchange Server 2010 mailbox

we have ac# application that reads an email Inbox currently hosted on Exchange 2003 using the http service.

Now the mailbox is to be migrated to an Exchange 2010 server, so we are testing our code to confirm it will still work.

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;

        Stream loRequestStream = default(Stream);
        Stream loResponseStream = default(Stream);
        XmlDocument loXmlDoc = default(XmlDocument);
        loXmlDoc = new XmlDocument();
        try
        {
            lsRootUri = strServer + "/Exchange/" + strAlias + "/" + strInboxURL;
            lsQuery = "<?xml version=\"1.0\"?>"
                + "<D:searchrequest xmlns:D = \"DAV:\" xmlns:m=\"urn:schemas:httpmail:\">"
                + "<D:sql>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' "
                + "</D:sql></D:searchrequest>";
            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();
        }

the exception is thrown on the line loResponseStream = loResponse.GetResponseStream();

here is the xml that we are sending:

  <?xml version="1.0" ?> 
- <D:searchrequest xmlns:D="DAV:" xmlns:m="urn:schemas:httpmail:">
  <D:sql>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'</D:sql> 
  </D:searchrequest>

and from MSDN the answer is that WebDAV is deprecated after Exchange 2007 , and replaced by Exchange Web Services

here are a couple of links:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM