06.13.03


By Kais Dukes

The great thing about the POP mail protocol is that it is a well-documented open standard, making writing a mail client to collect mail from a POP box a relatively painless process. Armed with basic knowledge of POP, or SMTP it is possible to write proxies which do a variety of useful things, such filter out spam or junk mail, or provide an e-mail answering machine service. Unfortunately, in trying to write a standalone client for Hotmail, the world's most popular web-based mailing system, the fact that no POP gateway exists rapidly becomes a problem.

Despite the lack of POP-support, connecting to Hotmail without using a web-browser is possible. Outlook Express allows users to retrieve, delete, move and send messages, connecting directly to a standard Hotmail or MSN mailbox. By using a HTTP packet sniffer, it is possible to monitor communication between Outlook Express and Hotmail, making it possible to determine how the direct mailbox connection is made.

Outlook Express uses an undocumented protocol commonly referred to as HTTPMail, allowing a client to access Hotmail using a set of HTTP/1.1 extensions. This article explains some of the features of HTTPMail, and how best to connect to Hotmail using a C# client. The sample source code accompanying this article uses COM interop to leverage XMLHTTP as the transport service. The XMLHTTP component provides a complete HTTP implementation, including authentication together with ability to set custom headers before sending HTTP requests server-side.

Programming Resource Downloads


Connecting to the HTTPMail Hotmail Gateway

The default HTTPMail gateway for Hotmail boxes is located at http://services.msn.com/svcs/hotmail/httpmail.asp . Although undocumented, the HTTPMail protocol is actually a standard WebDAV service. As we are using C#, we could use the TCP and HTTP classes provided by the .NET framework within the System.Net namespace. Since we are working with WebDAV, it is simpler to use XMLHTTP to connect to Hotmail under C#. Referencing the MSXML2 component, provides an interop assembly which may be accessed directly. Note that in the code snippets contained within this article, variables suffixed with an underscore refer to member fields declared elsewhere within the sample code:

// Get the namespace.
using MSXML2;

...

// Create the object.
xmlHttp_ = new XMLHTTP();


In order to connect to a secure server, the WebDAV protocol requires HTTP/1.1 authentication. The initial request sent by a HTTPMail client uses the WebDAV PROPFIND method to query for a set of properties. These include the URL of the Hotmail advertisement bar together with the location of mailbox folders:

<?xml version="1.0"?>
<D:propfind xmlns:D="DAV:"
xmlns:h="http://schemas.microsoft.com/hotmail/"
xmlns:hm="urn:schemas:httpmail:">
  <D:prop>
   <h:adbar/>
   <hm:contacts/>
   <hm:inbox/>
   <hm:outbox/>
   <hm:sendmsg/>
   <hm:sentitems/>
   <hm:deleteditems/>
   <hm:drafts/>
   <hm:msgfolderroot/>
   <h:maxpoll/>
   <h:sig/>
  </D:prop>
</D:propfind>


Sending the initial request via XMLHTTP begins by specifying the WebDAV server URL, and generating the initial XML request:

// Specify the server URL.
string serverUrl = "http://services.msn.com/svcs/hotmail
                               /httpmail.asp";

// Build the query.
string folderQuery = null;
folderQuery += "<?xml version='1.0'?><D:propfind xmlns:D='DAV:' ";
folderQuery += "xmlns:h='http://schemas.microsoft.com/hotmail/' ";
folderQuery += "xmlns:hm='urn:schemas:httpmail:'><D:prop>";
folderQuery += "<h:adbar/><hm:contacts/><hm:inbox/>"
folderQuery += "<hm:outbox/><hm:sendmsg/><hm:sentitems/>";
folderQuery += "<hm:deleteditems/><hm:drafts/><hm:msgfolderroot/>";
folderQuery += "<h:maxpoll/><h:sig/></D:prop></D:propfind>";


The HTTPXML component provides an open() method used to establish a connection to a HTTP server:

void open(string method, string url, bool async, string user,
                   string password);


The first argument specifies the HTTP method used to open the connection, such as GET, POST, PUT, or PROPFIND. To connect to the Hotmail gateway, we specify the PROPFIND method to query the mailbox. This and other HTTP methods are used to retrieve folder information, collect mail items and send new mail. Note that the open() method allows for the possibility of asynchronous calls (enabled by default) which is preferred for a graphical mail client. Since the sample code is a console application, we set this parameter to false. For authentication, we specify a username and password. Note that under XMLHTTP, the component will display a login window if these parameters are missing and the site requires authentication. To connect to the Hotmail gateway we open the connection, set the PROPFIND request header to our XML-based query, and then send the request with a null body:

// Open a connection to the Hotmail server.
xmlHttp_.open("PROPFIND", serverUrl, false, username, password);

// Send the request.
xmlHttp_.setRequestHeader("PROPFIND", folderQuery);
xmlHttp_.send(null);


Click Here to Read the Full Article


About the Author:
Kais Dukes is currently working with a leading derivatives pricing specialist, where he is helping to build one of the world's largest financial systems that uses .NET technology. He is finishing a PhD in Artificial Intelligence, and is also completing a book on Templates and Generative Programming in C++. A selection of his more popular articles can be found at http://www.kaisdukes.com. Kais can be reached at kd@kaisdukes.com


Read this Newsletter at: http://www.cprogrammingtrends.com/2003/0613.html

 

 

 



 

 

 

-- CProgrammingTrends is an iEntry, Inc. publication --
2003 iEntry, Inc. All Rights Reserved Privacy Policy  Legal