www.pudn.com > mailreveive.rar > Pop3.cs


using System; 
using System.Collections; 
using System.Text; 
using System.IO; 
using System.Threading; 
 
namespace Pop3Com 
{ 
	///  
	/// Pop3 的摘要说明。 
	///  
	public class Pop3 
	{ 
		///  
		/// 邮件接收完毕通知的代理 
		///  
		public delegate void MailReceivedDelegate(); 
 
		///  
		/// 邮件接收成功后的事件 
		///  
		public event MailReceivedDelegate OnMailReceived; 
 
		private Pop3Connection con; 
		private string host; 
		private int    port; 
		private string username; 
		private string password; 
		private int numofmails; 
		private double totalsize; 
		private string body; 
		private string status; 
		private string CRLF = "\r\n"; 
         
		#region Properties 
		///  
		/// 主机名 
		///  
		public string Host 
		{ 
			get {return host;} 
			set  
			{ 
				if(value == null || value.Trim().Length == 0) 
				{ 
					throw new ArgumentException("Invalid host name."); 
				} 
				host = value; 
			} 
		} 
 
		///  
		/// 端口号 
		///  
		public int Port 
		{ 
			get {return port;} 
			set  
			{ 
				if(value <= 0) 
				{ 
					throw new ArgumentException("Invalid port."); 
				} 
				port = value; 
			} 
		} 
	 
		///  
		/// 用户名 
		///  
		public string UserName 
		{ 
			get {return username;} 
			set  
			{ 
				if(value == null || value.Trim().Length == 0) 
				{ 
					throw new ArgumentException("Invalid user name."); 
				} 
				username = value; 
			} 
		} 
 
		///  
		/// 密码 
		///  
		public string PassWord 
		{ 
			get {return password;} 
			set  
			{ 
				if(value == null) 
				{ 
					throw new ArgumentException("Invalid password."); 
				} 
				password = value; 
			} 
		} 
 
		///  
		/// 邮件数量 
		///  
		public int NumOfMails 
		{ 
			get {return numofmails;} 
		} 
 
		///  
		/// 邮件总体积 
		///  
		public double TotalSize 
		{ 
			get {return totalsize;} 
		} 
 
		///  
		/// 邮件内容 
		///  
		public string Body 
		{ 
			get {return body;} 
		} 
 
		///  
		/// 状态信息 
		///  
		public string Status 
		{ 
			get {return status;} 
		} 
 
		#endregion 
 
		public Pop3() 
		{ 
			body = status = null; 
			numofmails = 0; 
		} 
 
		///  
		/// 接收信息 
		///  
		public void ReceiveMessage() 
		{ 
			// 避免线程冲突 
			lock(this) 
			{ 
				// 设置初始连接 
				con = new Pop3Connection(); 
				if(port <= 0) port = 110; 
				con.Open(host, port); 
 
				StringBuilder buf = new StringBuilder(); 
				string response; 
				int code; 
 
				// 获取欢迎信息 
				con.GetReply(out response, out code); 
				status += response; 
 
				//登录服务器过程 
				buf.Append("USER"); 
				buf.Append(username); 
				buf.Append(CRLF); 
				con.SendCommand(buf.ToString()); 
				con.GetReply(out response, out code); 
				status += response; 
 
				buf.Length = 0; 
				buf.Append("PASS"); 
				buf.Append(password); 
				buf.Append(CRLF); 
				con.SendCommand(buf.ToString()); 
				con.GetReply(out response, out code); 
				status += response; 
 
				//向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小 
				buf.Length = 0; 
				buf.Append("STAT"); 
				buf.Append(CRLF); 
				con.SendCommand(buf.ToString()); 
				con.GetReply(out response, out code); 
				status += response; 
 
				//将总邮件数和邮件大小分离 
				string[] TotalStat = response.Split(new char[] {' '});			 
				numofmails = Int32.Parse(TotalStat[1]); 
				totalsize = (double)Int32.Parse(TotalStat[2]); 
 
				for( int x = 0; x < numofmails; ++x) 
				{ 
					//根据邮件编号从服务器获得相应邮件 
					buf.Length = 0; 
					buf.Append("RETR"); 
					buf.Append(x.ToString()); 
					buf.Append(CRLF); 
					con.SendCommand(buf.ToString()); 
					con.GetReply(out response, out code); 
				     
					if(response[0]!='-')  
					{ 
						//不断地读取邮件内容,只到结束标志:英文句号 
						while(response!=".") 
						{ 
							body += response; 
							con.GetReply(out response, out code); 
						} 
					} 
					else 
						status += response; 
				} 
 
				//向服务器发送QUIT命令从而结束和POP3服务器的会话 
				buf.Length = 0; 
				buf.Append("QUIT"); 
				buf.Append(CRLF); 
				con.SendCommand(buf.ToString()); 
				con.GetReply(out response, out code); 
				status += response; 
 
				con.Close(); 
 
				// 邮件接收成功后触发的事件 
				if(OnMailReceived != null) 
				{ 
					OnMailReceived(); 
				} 
			} 
		} 
 
		///  
		/// 通过一个独立的线程接收邮件 
		///  
		public void ReceiveMessageAsync() 
		{ 
			new Thread(new ThreadStart(ReceiveMessage)).Start(); 
		} 
	} 
}