處理讀取電子郵件的應用程序。 除了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>
有幾種方法(取決於Exchange服務器等的配置)...請顯示一些代碼...你試過了什麼?什麼不工作? – Yahia 2012-03-06 14:01:33
@Yahia查看做了什麼.... – 2012-03-06 14:11:03
感謝您的更新...我懷疑「身體」可能是「僞裝」作爲附件...請參閱下面的答案... – Yahia 2012-03-06 14:20:46