【问题标题】:Read Exchange Server 2003 account emails阅读 Exchange Server 2003 帐户电子邮件
【发布时间】:2023-03-15 06:01:01
【问题描述】:

正在开发阅读电子邮件的应用程序。 除了 webdav 之外,还有其他方法可以将 Exchange Server 2003 中的所有电子邮件发送到我的本地计算机。 webdav 的问题在于它无法获取未发送电子邮件的正文。

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>

【问题讨论】:

  • 有几种方法(取决于 Exchange 服务器的配置等)...请显示一些代码...您尝试了什么?到底是什么不工作?
  • @Yahia 查看做了什么......
  • 感谢更新...我怀疑“正文”可能被“伪装”为附件...请在下面查看我的答案...

标签: c# exchange-server


【解决方案1】:

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

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

还有一点:

在处理Undeliverable 邮件时,我体验到正文有时作为附件提供 - 在 WebDAV 中,这需要通过 X-MS-ENUMATT 动词访问(但请注意:像 winmail.dat 这样的特定“附件”是自动被展示的 Outlook“解码”)。

【讨论】:

  • 非常感谢您的回复,但是有没有免费的开源库。我可以使用 webdav 获取电子邮件附件并从那里获取吗?
  • @AI25 我不知道有任何免费的开源库用于此...在 Exchange WebDAV 协议 IIRC 中有一个动词名称 ENUMATT 或类似名称可以帮助...检查 Exchange 文档在 MSDN 上...但如果附件是 winmail.dat 则它的格式未记录!
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2013-12-01
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 2010-10-13
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多