Working on application that reads emails.
Is there a way besides webdav to get all emails from exchange server 2003 to my local machine.
The problem with webdav is that it does not get’s the body of Undelivered emails.
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();
}
}
in the output xml i get empty text description for undelivered emails:
<a:status>HTTP/1.1 404 Resource Not Found</a:status><a:prop><e:textdescription /></a:prop></a:propstat></a:response>
I see several options to communicate with Exchange servers – WebDAV is rather hard to use and is not well supported in later version (2010), MS provides EWS but these don’t work with older versions.
From my POV you can use any of the following components (commercial!):
Another point:
When handling
Undeliverablemails I made the experience that the body is sometimes provided as an attachment – in WebDAV this needs to be accessed via theX-MS-ENUMATTverb (but BEWARE: specific “attachments” like winmail.dat are automagically “decoded” by Outlook on display).