Click here to Skip to main content
15,893,337 members
Articles / Programming Languages / C#

Hotmail using C# – A HTTPMail client under .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (64 votes)
25 Mar 20038 min read 552.5K   2.3K   171  
A C# client library for access Hotmail using the undocumented HTTPMail protocol.
// HttpMail - Provides a .NET API for connecting to Hotmail.
// Copyright (C) Kais Dukes, 2003. All rights reserved. For
// futher information, contact: kaisdukes@hotmail.com

// Get the namespaces.
using System;
using System.Xml;
using System.IO;
using System.Collections;
using MSXML2;

// Start the namespace.
namespace HttpMail
{
	/// <summary>
	/// This class provides an implementation of a HttpMail client.
	/// </summary>
	public class MailClient
	{
		/// <summary>
		/// Class constructor
		/// </summary>
		public MailClient()
		{
		}

		/// <summary>
		/// Connects to the HTTP mail server.
		/// </summary>
		public void Connect(string username, string password)
		{
			// Specify the server URL.
			string serverUrl = "http://services.msn.com/svcs/hotmail/httpmail.asp";

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

			// 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><h:adbar/>";
			folderQuery += "<hm:contacts/><hm:inbox/><hm:outbox/><hm:sendmsg/>";
			folderQuery += "<hm:sentitems/><hm:deleteditems/><hm:drafts/>";
			folderQuery += "<hm:msgfolderroot/><h:maxpoll/><h:sig/></D:prop></D:propfind>";

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

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

			// Get the response.
			string folderList = xmlHttp_.responseText;

			// Parse the folder list.
			ParseFolderList(folderList);
		}

		/// <summary>
		/// Sends an e-mail.
		/// </summary>
		public void SendMail(string from, string fromName, string to, string subject, string body)
		{
			// Check for errors.
			if(null == sendUrl_)
			{
				// Throw an exception.
				throw new Exception("Send mail URL not specified.");
			}

			// Quote character.
			string quote = "\u0022";

			// Generate the time stamp.
			DateTime now = DateTime.Now;
			string timeStamp = now.ToString("ddd, dd MMM yyyy hh:mm:ss");

			// Build the post body.
			string postBody = null;

			// Dump mail headers.
			postBody += "MAIL FROM:<" + from + ">\r\n";
			postBody += "RCPT TO:<" + to + ">\r\n";
			postBody += "\r\n";
			postBody += "From: " + quote + fromName + quote + " <" + from + ">\r\n";
			postBody += "To: <" + to + ">\r\n";
			postBody += "Subject: " + subject +"\r\n";
			postBody += "Date: " + timeStamp + " -0000\n";
			postBody += "\r\n";

			// Dump mail body.
			postBody += body;

			// Open the connection.
			xmlHttp_.open("POST", sendUrl_, false, null, null);

			// Send the request.
			xmlHttp_.setRequestHeader("Content-Type", "message/rfc821");
			xmlHttp_.send(postBody);
		}

		/// <summary>
		/// Loads the inbox.
		/// </summary>
		public ArrayList LoadInbox()
		{
			// Load the inbox.
			ArrayList mailItems = LoadFolder(inboxUrl_);

			// Return.
			return mailItems;
		}

		/// <summary>
		/// Loads the given mail item.
		/// </summary>
		public string LoadMail(MailItem mailItem)
		{
			// Get the Url.
			string mailUrl = mailItem.Url;

			// Open a connection to the hotmail server.
			xmlHttp_.open("GET", mailUrl, false, null, null);

			// Send the request.
			xmlHttp_.send(null);

			// Get the response.
			string mailData = xmlHttp_.responseText;

			// Return the mail data.
			return mailData;
		}

		/// <summary>
		/// Loads the given folder.
		/// </summary>
		private ArrayList LoadFolder(string folderUrl)
		{
			// Initiate.
			ArrayList mailItems = new ArrayList();

			// Check for errors.
			if(null == folderUrl)
			{
				// Throw an exception.
				throw new Exception("Folder URL not specified.");
			}

			// Build the query.
			string getMailQuery = null;
			getMailQuery += "<?xml version='1.0'?><D:propfind xmlns:D='DAV:' ";
			getMailQuery += "xmlns:hm='urn:schemas:httpmail:' ";
			getMailQuery += "xmlns:m='urn:schemas:mailheader:'><D:prop><D:isfolder/>";
			getMailQuery += "<hm:read/><m:hasattachment/><m:to/><m:from/><m:subject/>";
			getMailQuery += "<m:date/><D:getcontentlength/></D:prop></D:propfind>";

			// Get the mail info.
			xmlHttp_.open("PROPFIND", folderUrl, false, null, null);
			xmlHttp_.send(getMailQuery);
			string folderInfo = xmlHttp_.responseText;

			// Holders.
			MailItem mailItem = null;

			// Load the Xml.
			StringReader reader = new StringReader(folderInfo);
			XmlTextReader xml = new XmlTextReader(reader);

			// Read the Xml.
			while(xml.Read())
			{
				// Sections.
				string name = xml.Name;
				XmlNodeType nodeType = xml.NodeType;

				// E-mail?
				if(name == "D:response")
				{
					// Start?
					if(nodeType == XmlNodeType.Element)
					{
						// Create a new mail item.
						mailItem = new MailItem();
					}

					// End?
					if(nodeType == XmlNodeType.EndElement)
					{
						// Store the last mail.
						mailItems.Add(mailItem);

						// Reset.
						mailItem = null;
					}
				}

				// Got an element?
				if(nodeType == XmlNodeType.Element)
				{
					// Mail field.
					if(name == "D:href")
					{
						// Load.
						xml.Read();
						mailItem.Url = xml.Value;
					}

					// Mail field.
					if(name == "hm:read")
					{
						// Load.
						xml.Read();
						mailItem.IsRead = (xml.Value == "1");
					}

					// Mail field.
					if(name == "m:to")
					{
						// Load.
						xml.Read();
						mailItem.To = xml.Value;
					}

					// Mail field.
					if(name == "m:from")
					{
						// Load.
						xml.Read();
						mailItem.From = xml.Value;
					}

					// Mail field.
					if(name == "m:subject")
					{
						// Load.
						xml.Read();
						mailItem.Subject = xml.Value;
					}

					// Mail field.
					if(name == "m:date")
					{
						// Load.
						xml.Read();
						mailItem.Time = DateTime.Parse(xml.Value);
					}

					// Mail field.
					if(name == "D:getcontentlength")
					{
						// Load.
						xml.Read();
						mailItem.Size = int.Parse(xml.Value);
					}
				}
			}

			// Return the mail items.
			return mailItems;
		}

		/// <summary>
		/// Parses the given folder list.
		/// </summary>
		private void ParseFolderList(string folderList)
		{
			// Initiate.
			inboxUrl_ = null;
			sendUrl_ = null;

			// Load the Xml.
			StringReader reader = new StringReader(folderList);
			XmlTextReader xml = new XmlTextReader(reader);

			// Check for errors.
			if(null == folderList || "" == folderList)
			{
				// Throw an exception.
				throw new Exception("Failed to connect, check username and password?");
			}

			// Read the Xml.
			while(xml.Read())
			{
				// Got an element?
				if(xml.NodeType == XmlNodeType.Element)
				{
					// Get this node.
					string name = xml.Name;

					// Got the inbox?
					if(name == "hm:inbox")
					{
						// Set folder.
						xml.Read();
						inboxUrl_ = xml.Value;
					}

					// Got the send message page?
					if(name == "hm:sendmsg")
					{
						// Set folder.
						xml.Read();
						sendUrl_ = xml.Value;
					}
				}
			}

			// No inbox?
			if(null == inboxUrl_)
			{
				// Throw an exception.
				throw new Exception("Failed to determine inbox URL.");
			}

			// No send page?
			if(null == sendUrl_)
			{
				// Throw an exception.
				throw new Exception("Failed to determine send page URL.");
			}
		}

		/// <summary>
		/// XML/HTTP object used to transmit and recieve data.
		/// </summary>
		private XMLHTTP xmlHttp_ = null;

		/// <summary>
		/// The Url of the hotmail inbox.
		/// </summary>
		private string inboxUrl_ = null;

		/// <summary>
		/// The url of the hotmail send page.
		/// </summary>
		private string sendUrl_ = null;
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions