2012-03-06 67 views
0

處理讀取電子郵件的應用程序。 除了webdav之外,還有一種方法可以將來自交換服務器2003的所有電子郵件發送到本地計算機。 webdav的問題在於它沒有得到未交付電子郵件的正文。閱讀Exchange Server 2003帳戶電子郵件

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

有幾種方法(取決於Exchange服務器等的配置)...請顯示一些代碼...你試過了什麼?什麼不工作? – Yahia 2012-03-06 14:01:33

+0

@Yahia查看做了什麼.... – 2012-03-06 14:11:03

+0

感謝您的更新...我懷疑「身體」可能是「僞裝」作爲附件...請參閱下面的答案... – Yahia 2012-03-06 14:20:46

回答

1

我發現了幾個與Exchange服務器通信的選項 - WebDAV相當難以使​​用,並且在以後的版本(2010)中得不到很好的支持,MS提供了EWS,但這些不適用於舊版本。

從我的POV你可以使用任何以下組件(商業!):

還有一點:

瓦en處理Undeliverable郵件我提供了身體有時作爲附件提供的體驗 - 在WebDAV中,需要通過X-MS-ENUMATT動詞來訪問(但要注意,Outlook中將自動「解碼」像winmail.dat這樣的特定「附件」 )。

+0

非常感謝您的響應,但有沒有免費的開放源碼庫。我可以使用webdav來獲取電子郵件附件嗎? – 2012-03-06 14:26:09

+1

@ AI25我不知道任何免費的開源庫爲此...在Exchange WebDAV協議IIRC有一個動詞名稱'ENUMATT'或類似的,可以幫助...檢查MSDN上的Exchange文檔... ...但是如果附件是'winmail.dat',那麼它就有一個無證的格式! – Yahia 2012-03-06 14:28:42

1

你可以嘗試發送HTTP請求到服務器一樣OWA並(在其指定過程中的郵件ID) - 然後你會得到HTML,你可以解析。

另外 - 檢查原始郵件是否在「未傳送」電子郵件的附件數組中。