0

我正在使用以下代码从 Exchange 2003 服务器检索电子邮件。一切都在星期五工作,现在失败了。

通过一些调查,我将其范围缩小到 targetDate 变量。似乎日期是在 4 月,它会从服务器返回 400 失败。我已经评论了这条线并尝试了各种日期 2012-3-29、2012-4-1、2012-4-10(今天),而 4 月份的日期似乎失败了。

某种病态的愚人节玩笑?

代码本身来源于这篇文章: http: //www.codeproject.com/Articles/42458/Using-Exchange-2003-with-Webdav-Send-Retrieve-Atta

    public XmlDocument GetMailAll()
    {
        HttpWebRequest request = default(HttpWebRequest);
        HttpWebResponse response = default(HttpWebResponse);
        string rootUri = null;
        string query = null;
        byte[] bytes = null;

        Stream requestStream = default(Stream);
        Stream responseStream = default(Stream);
        XmlDocument xmlDoc = default(XmlDocument);
        xmlDoc = new XmlDocument();
        try
        {
            DateTime targetDateTime = DateTime.Today.AddDays(-5);
            String targetDate = ""+targetDateTime.Year + "-" + targetDateTime.Month + "-" + targetDateTime.Day;

            rootUri = server + "/Exchange/" + alias + "/" + inbox;
            query = "<?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\","  //Return full body (not necessary right now)
                        + "\"urn:schemas:httpmail:datereceived\", \"urn:schemas:httpmail:read\" FROM \"" + rootUri
                        + "\" WHERE \"DAV:ishidden\" = false "
                        + "AND \"DAV:isfolder\" = false " 
                        //+ "AND \"urn:schemas:httpmail:read\" = false"
                        + "AND \"urn:schemas:httpmail:datereceived\" >= CAST(\"" + targetDate + "T00:00:000Z\" AS 'dateTime.tz')"
                        + "</D:sql></D:searchrequest>";
            request = (HttpWebRequest)WebRequest.Create(rootUri);
            request.Timeout = 5000;
            request.Credentials = new NetworkCredential(alias, password, domain);
            request.Method = "SEARCH";
            request.ContentType = "text/xml";
            request.Headers.Add("Translate", "F");
            bytes = System.Text.Encoding.UTF8.GetBytes(query);
            request.ContentLength = bytes.Length;

            requestStream = request.GetRequestStream();
            requestStream.Write(bytes, 0, bytes.Length);
            requestStream.Close();
            response = (HttpWebResponse)request.GetResponse();

            authCookies = new List<Cookie>();
            foreach(Cookie cookie in response.Cookies)
            {
                authCookies.Add(cookie);
            }

            responseStream = response.GetResponseStream();
            xmlDoc.Load(responseStream);
            responseStream.Close();
        }
        catch (WebException ex)
        {
            if (ex.Response == null)
            {
                throw new Exception();
            }
            else if ((ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.Unauthorized)
            {
                throw new ExchangeCatastrophicException();
            }
            else
            {
                throw new ExchangeFailedException();
            }
        }
        catch (Exception ex)
        {
            throw;
        }
        return xmlDoc;
    }
4

1 回答 1

0

终于让它工作了。要转换的确切 targetDate 包括 T00:00:..Z 部分必须与http://msdn.microsoft.com/en-us/library/aa123600%28v=exchg.65%29.aspx上的规范匹配

我现在有:

DateTime targetDateTime = DateTime.Today;
string targetDate = targetDateTime.ToString("yyyy-MM-dd");

剪断

+ "AND \"urn:schemas:httpmail:datereceived\" >= CAST(\"" + targetDate + "T00:00:00Z" + "\" AS 'dateTime.tz')"

请注意,秒部分是两个零而不是三个。

于 2012-05-02T14:37:01.700 回答