C # visit Hotmail

Source: Internet
Author: User
Tags header http request mail requires sendmsg xml reader xmlns advantage

The advantage of the POP mail protocol is that it is an open standard with perfect documentation, which makes it less difficult to write a POP mail client, and as long as you have the basics of pop and SMTP, you can write an agent to perform various tasks, such as filtering ads and spam, or provide e-mail automatic answering service.

Hotmail is the world's most influential web mail system, and unfortunately, when we are writing a separate client program for Hotmail (a client program that is not accessed through a browser), we immediately encounter the barrier that Hotmail does not provide a pop gateway.

Although Hotmail does not provide pop support, browsers are not the only way to access Hotmail. For example, you can use Outlook Express to directly connect to a standard Hotmail or MSN mailbox to extract, delete, move, or send mail. With the HTTP packet monitor, we can monitor the communication process between Outlook Express and Hotmail, and analyze how the client program connects to the Hotmail mailbox.

Outlook Express leverages a set of http/1.1 extensions to access Hotmail using an undisclosed protocol commonly known as HTTPMail. This article describes some of the features of HTTPMail and the process of accessing Hotmail using C # client programs. The sample program in this article uses COM interop to use XMLHTTP as a transport service. The XMLHTTP component provides a complete HTTP implementation that includes authentication and the ability to set a custom HTTP header before sending an HTTP request to the server.

First, connect HTTPMail Gateway

Hotmail mailbox Default HTTPMail gateway in http://services.msn.com/svcs/hotmail/httpmail.asp. The HTTPMail protocol is actually a standard WebDAV service, but it has not yet been made public. When writing a C # program, we can easily invoke it. NET Framework for each TCP and HTTP class provided in the System.Net namespace. In addition, because we want to operate WebDAV, it is easiest to use XMLHTTP to connect to Hotmail in the C # environment, just refer to the MSXML2 component for direct access. Note In the code snippet for this article, the variable with the trailing line suffix is the member domain declared in the sample code:

Get namespaces
Using MSXML2;
...
Creating objects
XMLHTTP_ = new XmlHttp ();

In order to connect to a secure server, the WebDAV protocol requires http/1.1 authentication to be performed. The first request issued by the HTTPMail client uses the WebDAV PropFind method to find a set of properties, including the Hotmail banner URL and the location of the mailbox folder:

<?xml version= "1.0"?>
<d:propfind xmlns:d= "DAV:" xmlns:h= "http://schemas.microsoft.com/hotmail/"
Xmlns:hm= "Urn:schemas:httpmail:" >
<D:prop>
</D:prop>
</D:propfind>

When you send the first request through XMLHTTP, you first specify the WebDAV server URL and then generate the contents of the XML request:

Specify the URL of the server
String ServerURL = "http://services.msn.com/svcs/hotmail/httpmail.asp";
Constructing queries
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 = "Folderquery = "Folderquery = "

The XMLHTTP component provides an open () method to establish a connection to the HTTP server:

void Open (String method, string URL, bool async, string user, string password);

The first parameter of the open () method specifies the HTTP method used to open the connection, such as GET, POST, put, or propfind, through which we can extract folder information, collect messages, or send new messages. To connect to the Hotmail gateway, we specify the PropFind method to query the mailbox. Note the open () method allows asynchronous calls (which are enabled by default), which is the ideal invocation for mail clients with graphical user interfaces. Since the sample program in this article is a console application, we set this argument to False.

In order to perform authentication, we specified the user name and password in the open () method. When using the XMLHTTP component, if the open () method does not provide a user name and password parameter, but the Web site requires authentication, XMLHTTP will display a login window. To open the connection to the Hotmail gateway, we set the header of the PROPFIND request to the contents of the XML query, the body of the message remains blank, and then sends the message:

Open a connection to a Hotmail server
Xmlhttp_.open ("PROPFIND", ServerURL, false, username, password);
Send Request
Xmlhttp_.setrequestheader ("PROPFIND", folderquery);
Xmlhttp_.send (NULL);

Second, analysis of the folder List of mailboxes

Requests sent to services.msn.com typically undergo several redirects, with server-side load balancing, and the final request is delivered to an idle Hotmail server and authentication is performed. On the client side, this redirection, authentication-performing interaction process is handled by the XMLHTTP component. When a connection is successfully established, the server also requires that some cookies be set up to verify the legality of the current session, which is also handled automatically by the XMLHTTP component. After the initial connection request is issued, the server returns an XML-formatted reply:

Get answers to the content
string folderlist = Xmlhttp_.responsetext;

The answer returned by the server contains a number of useful information, including the URL location of the folder in the mailbox, and here is an example:

<?xml version= "1.0" encoding= "Windows-1252"?>
<D:response>
...
<D:propstat>
<D:prop>
...
</D:prop>
</D:response>
</D:multistatus>

In the console sample program of this article, the two folders that we are interested in are the folders for your Inbox and Outbox, which are used to receive and send messages, respectively.

There are many ways to parse XML in the C # environment, so we can take advantage of the System.XML.XmlTextReader speed because we are sure all the XML documents involved in the code are always legitimate. XmlTextReader is a "forward only" reader, which converts the XML character data into a character stream and initializes the XML reader:

Class
Inboxurl_ = null;
Sendurl_ = null;
Loading XML
StringReader reader = new StringReader (folderlist);
XmlTextReader xml = new XmlTextReader (reader);

Traverse each node and select the Hm:inbox and HM:SENDMSG nodes, which represent the Inbox and Outbox respectively:

Reading XML data
while (XML. Read ())
{
is an XML element?
if (XML. NodeType = = XmlNodeType.Element)
{
Get the Node
String name = XML. Name;
This node represents your Inbox?
if (name = = "Hm:inbox")
{
Save Inbox URL
Xml. Read ();
Inboxurl_ = XML. Value;
}
This node represents the Outbox?
if (name = = "Hm:sendmsg")
{
Save the Outbox URL
Xml. Read ();
Sendurl_ = XML. Value;
}
}
}

You can send and receive messages only if you first get the legal Inbox and Outbox URLs for the current session.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.