分享
 
 
 

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] 下一页

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有