Introduction
This article describes how to use WebDav to communicate with a Microsoft 2003 Exchange Server in order to perform several mail actions. The actions described should be a good enough basis for a programmer to (if needed) write additional actions.
This project was developed using VS2008, in C# .NET 3.5.
Background
I needed to write an application which could retrieve attachments from unread emails, download them and then mark the emails as read. Sounds easy, right? Just POP the mail, do your stuff and there you are... NOT!
The first problem was getting the attachments from the email. There are solutions available on the web, but I found out that they generated too many unexpected weird errors.
The second problem was that you can't mark an email as read. The only way is to maintain a local list with read emails, or maintain a list on a database somewhere.
So I searched for an alternative way and found WebDav. Unfortunately WebDav is very badly documented on the web and good example projects are hard to find. Therefore I composed this "How To" article containing a lot of WebDav examples.
Using the Code
When examining the downloadable source code and running the application, you might find out that you have to download MSXML 4.0 from Microsoft (the downloadable application contains the interop DLL).
The application is able to perform the following tasks:
- Get an
XMLDocument
containing all unread mail URLs
- Get an
XMLDocument
containing all unread mail URLs containing attachments
- Get an
XMLDocument
containing a list of URLs for all attachments in one email
- Retrieve an attachment from an email in a
streamreader
(displayed as string
)
- Mark an email as read
- Get an
XMLDocument
with information about all folders in your mailbox
- Get the total size of your mailbox
- Retrieve contact information from your Exchange Contacts
- Send an email using your exchange account
All the above actions are included in the mail.cs class file. When initiating an action, an instance of this class is started and filled with the exchange information you entered in the Exchange Settings part:
mail = new Mail();
mail.p_strServer = Properties.Settings.Default["ExchangeServer"].ToString();
mail.p_strUserName = Properties.Settings.Default["UserName"].ToString();
mail.p_strAlias = Properties.Settings.Default["UserNameAlias"].ToString();
mail.p_strPassword = Properties.Settings.Default["Password"].ToString();
mail.p_strInboxURL = Properties.Settings.Default["InboxName"].ToString();
mail.p_strDrafts = Properties.Settings.Default["DraftsName"].ToString();
Below, you can see the content of the entire mail.cs class:
class Mail
{
public string p_strUserName;
public string p_strPassword;
public string p_strAlias;
public string p_strInboxURL;
public string p_strServer;
public string p_strDrafts;
public XmlDocument GetAttachmentsListXML(string strMailUrl)
{
XmlDocument loXmlDoc = new XmlDocument();
try
{
MSXML2.XMLHTTP40 HttpWebRequest = default(MSXML2.XMLHTTP40);
HttpWebRequest = new MSXML2.XMLHTTP40();
HttpWebRequest.open("X-MS-ENUMATTS", strMailUrl,
false, p_strUserName, p_strPassword);
HttpWebRequest.setRequestHeader("Depth", "1");
HttpWebRequest.setRequestHeader("Content-type", "xml");
HttpWebRequest.send("");
loXmlDoc.LoadXml(HttpWebRequest.responseText);
HttpWebRequest = null;
}
catch (Exception ex)
{
throw;
}
return loXmlDoc;
}
public string getAttachmentFromMail(string sAttachmentUrl)
{
string strResult = "";
try
{
MSXML2.XMLHTTP40 HttpWebRequest = default(MSXML2.XMLHTTP40);
HttpWebRequest = new MSXML2.XMLHTTP40();
HttpWebRequest.open("GET", sAttachmentUrl, false,
p_strUserName, p_strPassword);
HttpWebRequest.send("");
strResult = HttpWebRequest.responseText;
HttpWebRequest = null;
}
catch
{
throw;
}
return strResult;
}
public XmlDocument GetUnreadMailWithAttachments()
{
HttpWebRequest loRequest = default(HttpWebRequest);
HttpWebResponse loResponse = default(HttpWebResponse);
string lsRootUri = null;
string lsQuery = null;
byte[] laBytes = null;
Stream loRequestStream = default(Stream);
Stream loResponseStream = default(Stream);
XmlDocument loXmlDoc = default(XmlDocument);
loXmlDoc = new XmlDocument();
try
{
lsRootUri = p_strServer + "/Exchange/" +
p_strAlias + "/" + p_strInboxURL;
lsQuery = "<?xml version=\"1.0\"?>"
+ "<D:searchrequest xmlns:D = \"DAV:\"
xmlns:m=\"urn:schemas:httpmail:\">"
+ "<D:sql>SELECT \"urn:schemas:httpmail:hasattachment\",
\"DAV:displayname\", "
+ "\"urn:schemas:httpmail:from\",
\"urn:schemas:httpmail:subject\", "
+ "\"urn:schemas:httpmail:htmldescription\"
FROM \"" + lsRootUri
+ "\" WHERE \"DAV:ishidden\" = false
AND \"DAV:isfolder\" = false AND "
+ "\"urn:schemas:httpmail:hasattachment\" = true
AND \"urn:schemas:httpmail:read\" = false"
+ "</D:sql></D:searchrequest>";
loRequest = (HttpWebRequest)WebRequest.Create(lsRootUri);
loRequest.Credentials = new NetworkCredential
(p_strUserName, p_strPassword);
loRequest.Method = "SEARCH";
laBytes = System.Text.Encoding.UTF8.GetBytes(lsQuery);
loRequest.ContentLength = laBytes.Length;
loRequestStream = loRequest.GetRequestStream();
loRequestStream.Write(laBytes, 0, laBytes.Length);
loRequestStream.Close();
loRequest.ContentType = "text/xml";
loRequest.Headers.Add("Translate", "F");
loResponse = (HttpWebResponse)loRequest.GetResponse();
loResponseStream = loResponse.GetResponseStream();
loXmlDoc.Load(loResponseStream);
loResponseStream.Close();
}
catch (Exception ex)
{
throw;
}
return loXmlDoc;
}
public XmlDocument GetUnreadMailAll()
{
HttpWebRequest loRequest = default(HttpWebRequest);
HttpWebResponse loResponse = default(HttpWebResponse);
string lsRootUri = null;
string lsQuery = null;
byte[] laBytes = null;
Stream loRequestStream = default(Stream);
Stream loResponseStream = default(Stream);
XmlDocument loXmlDoc = default(XmlDocument);
loXmlDoc = new XmlDocument();
try
{
lsRootUri = p_strServer + "/Exchange/" +
p_strAlias + "/" + p_strInboxURL;
lsQuery = "<?xml version=\"1.0\"?>"
+ "<D:searchrequest xmlns:D = \"DAV:\"
xmlns:m=\"urn:schemas:httpmail:\">"
+ "<D:sql>SELECT \"urn:schemas:httpmail:hasattachment\",
\"DAV:displayname\", "
+ "\"urn:schemas:httpmail:from\",
\"urn:schemas:httpmail:subject\", "
+ "\"urn:schemas:httpmail:htmldescription\"
FROM \"" + lsRootUri
+ "\" WHERE \"DAV:ishidden\" = false "
+ "AND \"DAV:isfolder\" = false "
+ "AND \"urn:schemas:httpmail:read\" = false"
+ "</D:sql></D:searchrequest>";
loRequest = (HttpWebRequest)WebRequest.Create(lsRootUri);
loRequest.Credentials = new NetworkCredential
(p_strUserName, p_strPassword);
loRequest.Method = "SEARCH";
laBytes = System.Text.Encoding.UTF8.GetBytes(lsQuery);
loRequest.ContentLength = laBytes.Length;
loRequestStream = loRequest.GetRequestStream();
loRequestStream.Write(laBytes, 0, laBytes.Length);
loRequestStream.Close();
loRequest.ContentType = "text/xml";
loRequest.Headers.Add("Translate", "F");
loResponse = (HttpWebResponse)loRequest.GetResponse();
loResponseStream = loResponse.GetResponseStream();
loXmlDoc.Load(loResponseStream);
loResponseStream.Close();
}
catch (Exception ex)
{
throw;
}
return loXmlDoc;
}
public XmlDocument GetAllMailboxInfo()
{
XmlDocument loXmlDoc = new XmlDocument();
string lsRootUri = p_strServer + "/Exchange/" +
p_strAlias + "/" + p_strInboxURL;
byte[] buffer = GetFolderSizeRequest(lsRootUri);
var request = (HttpWebRequest)WebRequest.Create(lsRootUri);
request.Method = "SEARCH";
request.ContentType = "text/xml";
request.Credentials = new NetworkCredential(p_strUserName, p_strPassword);
request.Headers.Add("Translate", "f");
request.Headers.Add("Depth", "1");
using (Stream stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
HttpWebResponse loResponse = (HttpWebResponse)request.GetResponse();
Stream loResponseStream = loResponse.GetResponseStream();
loXmlDoc.Load(loResponseStream);
return loXmlDoc;
}
public long GetMailboxSize()
{
return GetMailboxSize(p_strServer + "/Exchange/" +
p_strAlias + "/" + p_strInboxURL);
}
private long GetMailboxSize(string lsRootUri)
{
XmlReader reader;
byte[] buffer = GetFolderSizeRequest(lsRootUri);
var request = (HttpWebRequest) WebRequest.Create(lsRootUri);
request.Method = "SEARCH";
request.ContentType = "text/xml";
request.Credentials = new NetworkCredential(p_strUserName, p_strPassword);
request.Headers.Add("Translate", "f");
request.Headers.Add("Depth", "1");
using (Stream stream = request.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
using (WebResponse response = request.GetResponse())
{
string content = new StreamReader
(response.GetResponseStream()).ReadToEnd();
reader = XmlReader.Create(new StringReader(content));
var nsmgr = new XmlNamespaceManager(reader.NameTable);
nsmgr.AddNamespace("dav", "DAV:");
nsmgr.AddNamespace("e", "http://schemas.microsoft.com/mapi/proptag/");
var doc = new XPathDocument(reader);
long result = 0;
foreach (XPathNavigator element in doc.CreateNavigator().Select
("//dav:response[dav:propstat/dav:status = 'HTTP/1.1 200 OK']", nsmgr))
{
var size = element.SelectSingleNode
("dav:propstat/dav:prop/e:x0e080014", nsmgr).ValueAsLong;
string folderUrl = element.SelectSingleNode("dav:href", nsmgr).Value;
result += size;
bool hasSubs = element.SelectSingleNode
("dav:propstat/dav:prop/dav:hassubs", nsmgr).ValueAsBoolean;
if (hasSubs)
{
result += GetMailboxSize(folderUrl);
}
}
return result;
}
}
private byte[] GetFolderSizeRequest(string sUrl)
{
var settings = new XmlWriterSettings {Encoding = Encoding.UTF8};
using (var stream = new MemoryStream())
using (XmlWriter writer = XmlWriter.Create(stream, settings))
{
writer.WriteStartElement("searchrequest", "DAV:");
var searchRequest = new StringBuilder();
searchRequest.AppendFormat
("SELECT \"http://schemas.microsoft.com/mapi/proptag/x0e080014\",
\"DAV:hassubs\" FROM SCOPE ('HIERARCHICAL TRAVERSAL OF \"{0}\"')", sUrl);
writer.WriteElementString("sql", searchRequest.ToString());
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
return stream.ToArray();
}
}
internal string MarkAsRead(string strMailUrl)
{
string strResult = "";
HttpWebRequest loRequest = default(HttpWebRequest);
HttpWebResponse loResponse = default(HttpWebResponse);
string lsQuery = null;
byte[] laBytes = null;
Stream loRequestStream = default(Stream);
XmlDocument loXmlDoc = default(XmlDocument);
loXmlDoc = new XmlDocument();
try
{
lsQuery = "<?xml version=\"1.0\"?>"
+ "<a:propertyupdate xmlns:a=\"DAV:\"
xmlns:d=\"urn:schemas-microsoft-com:exch-data:\" "
+ "xmlns:b=\"urn:schemas:httpmail:\" xmlns:c=\"xml:\">"
+ "<a:set><a:prop><b:read>" + 1
+ "</b:read></a:prop>"
+ "</a:set></a:propertyupdate>";
loRequest = (HttpWebRequest)HttpWebRequest.Create(strMailUrl);
loRequest.Credentials = new NetworkCredential
(p_strUserName, p_strPassword);
loRequest.Method = "PROPPATCH";
laBytes = Encoding.UTF8.GetBytes((string)lsQuery);
loRequest.ContentLength = laBytes.Length;
loRequestStream = loRequest.GetRequestStream();
loRequestStream.Write(laBytes, 0, laBytes.Length);
loRequestStream.Close();
loRequest.ContentType = "text/xml";
loResponse = (HttpWebResponse)loRequest.GetResponse();
strResult = loResponse.StatusCode.ToString();
loRequest = null;
loResponse = null;
}
catch (Exception ex)
{
throw;
}
finally
{
loXmlDoc = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
return strResult;
}
internal string SendMail(string strSendTo,
string strSendSubject, string strSendBody)
{
HttpWebRequest PUTRequest = default(HttpWebRequest);
WebResponse PUTResponse = default(WebResponse);
HttpWebRequest MOVERequest = default(HttpWebRequest);
WebResponse MOVEResponse = default(WebResponse);
string strMailboxURI = "";
string strSubURI = "";
string strTempURI = "";
string strTo = strSendTo;
string strSubject = strSendSubject;
string strText = strSendBody;
string strBody = "";
byte[] bytes = null;
Stream PUTRequestStream = null;
try
{
strMailboxURI = p_strServer + "/exchange/" + p_strAlias;
strSubURI = p_strServer + "/exchange/" + p_strAlias
+ "/##DavMailSubmissionURI##/";
strTempURI = p_strServer + "/exchange/" + p_strAlias
+ "/" + p_strDrafts + "/" + strSubject + ".eml";
strBody = "To: " + strTo + "\n" +
"Subject: " + strSubject + "\n" +
"Date: " + System.DateTime.Now +
"X-Mailer: test mailer" + "\n" +
"MIME-Version: 1.0" + "\n" +
"Content-Type: text/plain;" + "\n" +
"Charset = \"iso-8859-1\"" + "\n" +
"Content-Transfer-Encoding: 7bit" + "\n" +
"\n" + strText;
PUTRequest = (HttpWebRequest)HttpWebRequest.Create(strTempURI);
PUTRequest.Credentials = new NetworkCredential
(p_strUserName, p_strPassword);
PUTRequest.Method = "PUT";
bytes = Encoding.UTF8.GetBytes((string)strBody);
PUTRequest.ContentLength = bytes.Length;
PUTRequestStream = PUTRequest.GetRequestStream();
PUTRequestStream.Write(bytes, 0, bytes.Length);
PUTRequestStream.Close();
PUTRequest.ContentType = "message/rfc822";
PUTResponse = (HttpWebResponse)PUTRequest.GetResponse();
MOVERequest = (HttpWebRequest)HttpWebRequest.Create(strTempURI);
MOVERequest.Credentials = new NetworkCredential
(p_strUserName, p_strPassword);
MOVERequest.Method = "MOVE";
MOVERequest.Headers.Add("Destination", strSubURI);
MOVEResponse = (HttpWebResponse)MOVERequest.GetResponse();
Console.WriteLine("Message successfully sent.");
PUTResponse.Close();
MOVEResponse.Close();
}
catch (Exception ex)
{
throw;
}
return strBody;
}
internal string PrintContactsUsingExchangeWebDAV(string strZoekString)
{
string sResult = "";
NetworkCredential credentials = new NetworkCredential
(p_strUserName, p_strPassword);
string uri = p_strServer + "/exchange/" + p_strAlias;
string sRequest = string.Format(
@"<?xml version=""1.0""?>
<g:searchrequest xmlns:g=""DAV:"">
<g:sql>
SELECT
""urn:schemas:contacts:sn"",
""urn:schemas:contacts:givenName"",
""urn:schemas:contacts:email1"",
""urn:schemas:contacts:telephoneNumber"",
""urn:schemas:contacts:bday"",
""urn:schemas:contacts:nickname"",
""urn:schemas:contacts:o"",
"" urn:schemas:contacts:profession""
FROM
Scope('SHALLOW TRAVERSAL OF ""{0}/exchange/{1}/contacts""')
WHERE
""urn:schemas:contacts:givenName"" LIKE '{2}%'
OR
""urn:schemas:contacts:sn"" LIKE '{2}%'
</g:sql>
</g:searchrequest>",
p_strServer, p_strAlias, strZoekString);
byte[] contents = Encoding.UTF8.GetBytes(sRequest);
HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;
request.Credentials = credentials;
request.Method = "SEARCH";
request.ContentLength = contents.Length;
request.ContentType = "text/xml";
using (Stream requestStream = request.GetRequestStream())
requestStream.Write(contents, 0, contents.Length);
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using (Stream responseStream = response.GetResponseStream())
{
XmlDocument document = new XmlDocument();
document.Load(responseStream);
foreach (XmlElement element in document.GetElementsByTagName("a:prop"))
{
if (element.InnerText.Length > 0)
{
sResult = sResult + string.Format("Name: {0} {1}\nNickname:
{2}\nBirthday: {3}\nEmail: {4}\nPhone:
{5}\nProfession: {6}\nCompany: {7}",
(element["d:givenName"] != null ?
element["d:givenName"].InnerText : ""),
(element["d:sn"] != null ? element["d:sn"].InnerText : ""),
(element["d:nickname"] != null ?
element["d:nickname"].InnerText : ""),
(element["d:bday"] != null ?
element["d:bday"].InnerText : ""),
(element["d:email1"] != null ?
element["d:email1"].InnerText : ""),
(element["d:telephoneNumber"] != null ?
element["d:telephoneNumber"].InnerText : ""),
(element["d:profession"] != null ?
element["d:profession"].InnerText : ""),
(element["d:o"] != null ? element["d:o"].InnerText : "")
) + Environment.NewLine + Environment.NewLine;
}
}
}
return sResult;
}
}
History
- 18-09-2009 - First post of this article