Click here to Skip to main content
Click here to Skip to main content
Go to top

Using Exchange 2003 with Webdav (Send, Retrieve, Attachments, Contacts, Mailboxsize, Mark as Read)

, 18 Sep 2009
Rate this:
Please Sign up or sign in to vote.
Using Exchange 2003 with Webdav

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;
        
        /// <summary>
        /// Gets an XMLDocument containing a list of attachments, found in an email
        /// </summary>
        /// <param name="strMailUrl"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// Extracts an attachment from an email
        /// </summary>
        /// <param name="sAttachmentUrl"></param>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// Gets all unread email messages, containing at least one attachment, 
        /// from an email account on an exchange server
        /// </summary>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// Gets all unread email messages from an email account on an exchange server
        /// </summary>
        /// <returns></returns>
        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: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;
        }
        /// <summary>
        /// Returns information about all mailboxes
        /// </summary>
        /// <returns></returns>
        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;
        }
        /// <summary>
        /// Helper class
        /// </summary>
        /// <returns></returns>
        public long GetMailboxSize()
        {
            return GetMailboxSize(p_strServer + "/Exchange/" + 
				p_strAlias + "/" + p_strInboxURL);
        }
        /// <summary>
        /// Returns the total size of all mailboxes 
        /// within the root node of a mailbox URL
        /// </summary>
        /// <param name="lsRootUri"></param>
        /// <returns></returns>
        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;  
            }  
        }
        /// <summary>
        /// Returns the size of one mail folder
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        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(); 
            }  
        }
        /// <summary>
        /// Marks an email an read
        /// </summary>
        /// <param name="strMailUrl"></param>
        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;
        }
        /// <summary>
        /// Sending an email
        /// </summary>
        /// <param name="strSendTo"></param>
        /// <param name="strSendSubject"></param>
        /// <param name="strSendBody"></param>
        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.");
                // Clean up.
                PUTResponse.Close();
                MOVEResponse.Close();
            }
            catch (Exception ex)
            {
                throw;
            }
            return strBody;
        }
        /// <summary>
        /// Returns all contacts where firstname or lastname starts 
        /// with a certain character or string
        /// </summary>
        /// <returns></returns>
        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);
            // For more contact information look up urn:schemas:contacts on MSDN
            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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dennis Betten
Software Developer (Senior) Sogeti B.V. Netherlands
Netherlands Netherlands
No Biography provided

Comments and Discussions

 
Questiongood article Pinmembersimonwonn16-Oct-13 21:54 
GeneralHow to solve potential MSXML2 reference problem PinmemberDennis Betten9-Sep-13 23:39 
QuestionThis is beautiful PinmemberMember 826881931-Jul-13 11:44 
AnswerRe: This is beautiful PinmemberDennis Betten9-Sep-13 21:49 
Questionwebdav protocal will support Exchange 2013 Pinmembersubhash2823-Apr-13 23:55 
GeneralMy vote of 5 PinmemberWaldemar Sauer11-Feb-13 11:20 
QuestionDo we need to poll or look for asynchronous response? PinmemberJethro638-Jan-13 2:55 
Question400 bad request Pinmemberlxai3326-Aug-12 19:44 
QuestionSMS from Exchange 2003 PinmemberWilsonLast20-May-12 20:55 
QuestionWhen passing CredentialCash.DefaultCredentials() to the service getting unauthorized service error PinmemberAmit Mudgal18-May-12 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 18 Sep 2009
Article Copyright 2009 by Dennis Betten
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid