C#访问hotmail邮件

王朝c#·作者佚名  2006-11-24
窄屏简体版  字體: |||超大  

如何使用程序来访问HOTMAIL的邮件

using System;

using System.Diagnostics;

using System.IO;

using System.Net;

using System.Text;

using System.Xml;

using System.Web;

namespace Hotmail

{

/// <summary>

/// HotmailProxy is able to speak HttpMail to various

/// Hotmail servers across the world.

/// </summary>

public class HotmailProxy

{

/// <summary>

/// Storage area for cookies.

/// </summary>

private CookieContainer ccContainer;

/// <summary>

/// Creates a new instance of HotmailHttp.

/// </summary>

public HotmailProxy()

{

Trace.WriteLine("Creating new instance.");

ccContainer = new CookieContainer();

}

/// <summary>

/// Sends the given request to the given destination

/// and returns the answer from the server.

/// </summary>

/// <param name="request">The request to send.</param>

/// <param name="destination">The destination to send the request to.</param>

/// <returns>The answer from the remote host.</returns>

public string SendRequest(string request, Uri destination)

{

// pass along

return SendRequest(request,destination,null);

}

/// <summary>

/// Sends the given request to the given destination

/// and returns the answer from the server.

/// </summary>

/// <param name="request">The request to send.</param>

/// <param name="destination">The destination to send the request to.</param>

/// <param name="credential">The network credentials to use with the request.</param>

/// <returns>The answer from the remote host.</returns>

public string SendRequest(string request, Uri destination, NetworkCredential credential)

{

// Verify input

if(request == null || request.Trim().Length == 0)

throw new ArgumentNullException("request");

else if (destination == null)

throw new ArgumentNullException("destination");

else

{

// Get byte[] and send the request using private method.

byte[] xmlBytes = Encoding.ASCII.GetBytes(request);

return SendRequestTo(xmlBytes,destination, credential);

}

}

/// <summary>

/// Sends the given byte[] to the given remote host using

/// authentication with the supplied credentials.

/// </summary>

/// <param name="requestBytes"></param>

/// <param name="destination"></param>

/// <param name="credential"></param>

/// <returns></returns>

private string SendRequestTo(byte[] requestBytes, Uri destination, NetworkCredential credential)

{

Trace.WriteLine("Sending request to url:" + destination.AbsoluteUri);

// Build the request.

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(destination);

webRequest.Method = "PROPFIND";

webRequest.Accept = "*/*";

webRequest.AllowAutoRedirect = false;

webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR v1.1.4322)";

webRequest.CookieContainer = new CookieContainer();

webRequest.ContentLength = requestBytes.Length;

webRequest.ContentType = "text/xml";

// Set the credentials

webRequest.Credentials = credential;

// Add cookies for this request

webRequest.CookieContainer.Add(ccContainer.GetCookies(destination));

try

{

// Write the request

Stream reqStream = webRequest.GetRequestStream();

reqStream.Write(requestBytes,0,requestBytes.Length);

reqStream.Close();

// Get a response

HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();

if (webRequest.HaveResponse)

{

// Handle returned cookies

foreach(Cookie retCookie in webResponse.Cookies)

{

bool cookieFound = false;

foreach(Cookie oldCookie in ccContainer.GetCookies(destination))

{

if (retCookie.Name.Equals(oldCookie.Name))

{

oldCookie.Value = retCookie.Value;

cookieFound = true;

}

}

if (!cookieFound)

ccContainer.Add(retCookie);

}

// Handle redirection headers

if ((webResponse.StatusCode == HttpStatusCode.Found) ||

(webResponse.StatusCode == HttpStatusCode.Redirect) ||

(webResponse.StatusCode == HttpStatusCode.Moved) ||

(webResponse.StatusCode == HttpStatusCode.MovedPermanently))

{

WebHeaderCollection headers = webResponse.Headers;

return SendRequestTo(requestBytes,new Uri(headers["location"]),credential);

}

// Read response

StreamReader stream = new StreamReader(webResponse.GetResponseStream());

string responseString = stream.ReadToEnd();

stream.Close();

return responseString;

}

// No response

throw new ApplicationException("No response received from host.");

}

catch(WebException e)

{

// Error occured

Trace.WriteLine("Exception occured " + e.Message);

throw new ApplicationException("Exception occured while sending request."+e.Message,e);

}

}

}

}

using System;

using System.Diagnostics;

using System.Net;

using System.Xml;

namespace Hotmail

{

public class HotmailClient

{

#region Class Instances and Properties

/// <summary>

/// The Http proxy used for Hotmail

/// </summary>

private HotmailProxy hHttp = null;

/// <summary>

/// Indicator if connected to remote host.

/// </summary>

private bool connected = false;

#endregion

#region Public Interface( Constructor, Connect, Disconnect, RetrieveFilledMailBox)

/// <summary>

/// Create a new instance of HotmailClient.

/// </summary>

public HotmailClient()

{

}

//

[1] [2] [3] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航