阅读Exchange Server 2003帐户电子邮件

时间:2012-03-06 13:56:53

标签: c# exchange-server

处理阅读电子邮件的应用程序。 除了webdav还有办法将所有电子邮件从Exchange Server 2003发送到我的本地计算机。 webdav的问题在于它不会成为Undelivered电子邮件的主体。

CredentialCache creds = new CredentialCache();
            creds.Add(new Uri(a), "NTLM",
                new NetworkCredential("xxxxx", "xxxxxx", "xxxxx.com"));

            List<Mail> unreadMail = new List<Mail>();
            string reqStr =

            @"<?xml version=""1.0""?>
                <g:searchrequest xmlns:g=""DAV:"">
                    <g:sql>
                        SELECT
                            ""urn:schemas:mailheader:from"",
                            ""urn:schemas:mailheader:to"",
                            ""urn:schemas:httpmail:textdescription""

                        FROM
                            ""http://xxxx.com/exchange/xxxx/Inbox/""
                        WHERE  

                             ""urn:schemas:httpmail:subject"" = 'Undeliverable: xxxx'                               

                        </g:sql>
                </g:searchrequest>";



            byte[] reqBytes = Encoding.UTF8.GetBytes(reqStr);


            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(a);
            request.Credentials = creds;
            request.Method = "SEARCH";
            request.ContentLength = reqBytes.Length;
            request.ContentType = "text/xml";
            request.Timeout = 300000;
            using (Stream requestStream = request.GetRequestStream())
            {
                try
                {
                    requestStream.Write(reqBytes, 0, reqBytes.Length);
                }
                catch
                {
                }
                finally
                {
                    requestStream.Close();
                }
            }
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            using (Stream responseStream = response.GetResponseStream())
            {
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(responseStream);


                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(document.NameTable);
                    nsmgr.AddNamespace("a", "DAV:");
                    nsmgr.AddNamespace("b", "urn:uuid:c2f41010-65b3-11d1-a29f-00aa00c14882/");
                    nsmgr.AddNamespace("c", "xml:");
                    nsmgr.AddNamespace("d", "urn:schemas:mailheader:");
                    nsmgr.AddNamespace("e", "urn:schemas:httpmail:");


                    XmlNodeList responseNodes = document.GetElementsByTagName("a:response");
                    foreach (XmlNode responseNode in responseNodes)
                    {

                        XmlNode uriNode = responseNode.SelectSingleNode("child::a:href", nsmgr);
                        XmlNode propstatNode = responseNode.SelectSingleNode("descendant::a:propstat[a:status='HTTP/1.1 200 OK']", nsmgr);
                        if (propstatNode != null)
                        {
                            // read properties of this response, and load into a data object
                            XmlNode fromNode = propstatNode.SelectSingleNode("descendant::d:from", nsmgr);
                            XmlNode descNode = propstatNode.SelectSingleNode("descendant::e:textdescription", nsmgr);
                            XmlNode toNode = propstatNode.SelectSingleNode("descendant::d:to", nsmgr);


                            // make new data object

                            Mail mail = new Mail();
                            if (uriNode != null)
                                mail.Uri = uriNode.InnerText;
                            if (fromNode != null)
                                mail.From = fromNode.InnerText;
                            if (descNode != null)
                                mail.Body = descNode.InnerText;
                            if (toNode != null)
                                mail.To = toNode.InnerText;
                            unreadMail.Add(mail);

                        }
                    }
                    var ac = unreadMail;

                }
                catch (Exception e)
                {
                    string msg = e.Message;
                }
                finally
                {

                    responseStream.Close();
                }
            }

在输出xml中,我得到未发送电子邮件的空文本描述:

<a:status>HTTP/1.1 404 Resource Not Found</a:status><a:prop><e:textdescription /></a:prop></a:propstat></a:response>

2 个答案:

答案 0 :(得分:1)

您可以尝试以与OWA相同的方式向服务器发送HTTP请求(当然在其中指定邮件ID) - 然后您将获得可以解析的HTML。

另外 - 检查原始邮件是否在“未送达”电子邮件的附件数组中。

答案 1 :(得分:1)

我看到几个与Exchange服务器通信的选项 - WebDAV很难使用,并且在以后的版本(2010)中得不到很好的支持,MS提供了EWS,但这些不适用于旧版本。

从我的POV中,您可以使用以下任何组件(商业!):

另一点:

当处理Undeliverable邮件时,我体验到身体有时作为附件提供 - 在WebDAV中,这需要通过X-MS-ENUMATT动词来访问(但是请注意:特定的“附件”,如winmail .dat由Outlook在显示屏上自动“解码”。