ASP.NET读取POP3邮件的操作
点击次数:22 次 发布日期:2008-11-26 15:55:34 作者:源代码网
|
源代码网推荐 { 源代码网推荐 using System.IO ; 源代码网推荐 using System.Net; 源代码网推荐 using System.Net.Sockets ; 源代码网推荐 //Please note that all code is copyright 2002 by William J Dean 源代码网推荐 public class POP3client 源代码网推荐 { 源代码网推荐 public enum connect_state {disc,AUTHORIZATION,TRANSACTION,UPDATE}; 源代码网推荐 源代码网推荐 public string user; 源代码网推荐 public string pwd; 源代码网推荐 public string pop; 源代码网推荐 public bool error; 源代码网推荐 public connect_state state=connect_state.disc ; 源代码网推荐 源代码网推荐 //borrowed from Agus Kurniawan"s article:"Retrieve Mail From a POP3 Server Using C#" at http://www.codeproject.com/csharp/popapp.asp 源代码网推荐 private TcpClient Server; 源代码网推荐 private NetworkStream NetStrm; 源代码网推荐 private StreamReader RdStrm; 源代码网推荐 private string Data; 源代码网推荐 private byte[] szData; 源代码网推荐 private string CRLF = " "; 源代码网推荐 源代码网推荐 public POP3client() 源代码网推荐 { 源代码网推荐 //nothing to do..just create to object 源代码网推荐 } 源代码网推荐 源代码网推荐 public POP3client(string pop_server,string user_name,string password) 源代码网推荐 { 源代码网推荐 //put the specied server (pop_server), user (user_name) and password (password) 源代码网推荐 //into the appropriate properties. 源代码网推荐 pop=pop_server; 源代码网推荐 user=user_name; 源代码网推荐 pwd=password; 源代码网推荐 } 源代码网推荐 源代码网推荐 #region Utility Methods, some public, some private 源代码网推荐 public string connect (string pop_server) 源代码网推荐 { 源代码网推荐 pop=pop_server; //put the specified server into the pop property 源代码网推荐 return(connect()); //call the connect method 源代码网推荐 } 源代码网推荐 public string connect() 源代码网推荐 { 源代码网推荐 //Initialize to the pop server. This code snipped "borrowed" 源代码网推荐 //with some modifications... 源代码网推荐 //from the article "Retrieve Mail From a POP3 Server Using C#" at 源代码网推荐 //www.codeproject.com by Agus Kurniawan 源代码网推荐 //http://www.codeproject.com/csharp/popapp.asp 源代码网推荐 源代码网推荐 // create server with port 110 源代码网推荐 Server = new TcpClient(pop,110); 源代码网推荐 源代码网推荐 try 源代码网推荐 { 源代码网推荐 // initialization 源代码网推荐 NetStrm = Server.GetStream(); 源代码网推荐 RdStrm= new StreamReader(Server.GetStream()); 源代码网推荐 源代码网推荐 //The pop session is now in the AUTHORIZATION state 源代码网推荐 state=connect_state.AUTHORIZATION ; 源代码网推荐 return(RdStrm.ReadLine ()); 源代码网推荐 } 源代码网推荐 catch(InvalidOperationException err) 源代码网推荐 { 源代码网推荐 return("Error: "+err.ToString()); 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 private string disconnect () 源代码网推荐 { 源代码网推荐 string temp="disconnected successfully."; 源代码网推荐 if(state !=connect_state.disc) 源代码网推荐 { 源代码网推荐 源代码网推荐 //close connection 源代码网推荐 NetStrm.Close(); 源代码网推荐 RdStrm.Close(); 源代码网推荐 state=connect_state.disc ; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 temp="Not Connected."; 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 源代码网推荐 private void issue_command(string command) 源代码网推荐 { 源代码网推荐 //send the command to the pop server. This code snipped "borrowed" 源代码网推荐 //with some modifications... 源代码网推荐 //from the article "Retrieve Mail From a POP3 Server Using C#" at 源代码网推荐 //www.codeproject.com by Agus Kurniawan 源代码网推荐 //http://www.codeproject.com/csharp/popapp.asp 源代码网推荐 Data= command + CRLF; 源代码网推荐 szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); 源代码网推荐 NetStrm.Write(szData,0,szData.Length); 源代码网推荐 源代码网推荐 } 源代码网推荐 private string read_single_line_response() 源代码网推荐 { 源代码网推荐 //read the response of the pop server. This code snipped "borrowed" 源代码网推荐 //with some modifications... 源代码网推荐 //from the article "Retrieve Mail From a POP3 Server Using C#" at 源代码网推荐 //www.codeproject.com by Agus Kurniawan 源代码网推荐 //http://www.codeproject.com/csharp/popapp.asp 源代码网推荐 string temp; 源代码网推荐 try 源代码网推荐 { 源代码网推荐 temp = RdStrm.ReadLine(); 源代码网推荐 was_pop_error(temp); 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 catch(InvalidOperationException err) 源代码网推荐 { 源代码网推荐 return("Error in read_single_line_response(): " + err.ToString ()) ; 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 private string read_multi_line_response() 源代码网推荐 { 源代码网推荐 //read the response of the pop server. This code snipped "borrowed" 源代码网推荐 //with some modifications... 源代码网推荐 //from the article "Retrieve Mail From a POP3 Server Using C#" at 源代码网推荐 //www.codeproject.com by Agus Kurniawan 源代码网推荐 //http://www.codeproject.com/csharp/popapp.asp 源代码网推荐 string temp=""; 源代码网推荐 string szTemp; 源代码网推荐 源代码网推荐 try 源代码网推荐 { 源代码网推荐 szTemp = RdStrm.ReadLine(); 源代码网推荐 was_pop_error(szTemp); 源代码网推荐 if(!error) 源代码网推荐 { 源代码网推荐 源代码网推荐 while(szTemp!=".") 源代码网推荐 { 源代码网推荐 temp += szTemp+CRLF; 源代码网推荐 szTemp = RdStrm.ReadLine(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 temp=szTemp; 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 catch(InvalidOperationException err) 源代码网推荐 { 源代码网推荐 return("Error in read_multi_line_response(): " + err.ToString ()); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 private void was_pop_error(string response) 源代码网推荐 { 源代码网推荐 //detect if the pop server that issued the response believes that 源代码网推荐 //an error has occured. 源代码网推荐 源代码网推荐 if(response.StartsWith ("-")) 源代码网推荐 { 源代码网推荐 //if the first character of the response is "-" then the 源代码网推荐 //pop server has encountered an error executing the last 源代码网推荐 //command send by the client 源代码网推荐 error=true; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 //success 源代码网推荐 error=false; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 #region POP commands 源代码网推荐 public string DELE(int msg_number) 源代码网推荐 { 源代码网推荐 string temp; 源代码网推荐 源代码网推荐 if (state != connect_state.TRANSACTION ) 源代码网推荐 { 源代码网推荐 //DELE is only valid when the pop session is in the TRANSACTION STATE 源代码网推荐 temp="Connection state not = TRANSACTION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 issue_command("DELE " + msg_number.ToString ()); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 源代码网推荐 public string LIST() 源代码网推荐 { 源代码网推荐 string temp=""; 源代码网推荐 if (state != connect_state.TRANSACTION ) 源代码网推荐 { 源代码网推荐 //the pop command LIST is only valid in the TRANSACTION state 源代码网推荐 temp="Connection state not = TRANSACTION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 issue_command ("LIST"); 源代码网推荐 temp=read_multi_line_response(); 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 源代码网推荐 public string LIST(int msg_number) 源代码网推荐 { 源代码网推荐 string temp=""; 源代码网推荐 源代码网推荐 if (state != connect_state.TRANSACTION ) 源代码网推荐 { 源代码网推荐 //the pop command LIST is only valid in the TRANSACTION state 源代码网推荐 temp="Connection state not = TRANSACTION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 issue_command ("LIST " + msg_number.ToString ()); 源代码网推荐 temp=read_single_line_response(); //when the message number is supplied, expect a single line response 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 public string NOOP() 源代码网推荐 { 源代码网推荐 string temp; 源代码网推荐 if (state != connect_state.TRANSACTION ) 源代码网推荐 { 源代码网推荐 //the pop command NOOP is only valid in the TRANSACTION state 源代码网推荐 temp="Connection state not = TRANSACTION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 issue_command ("NOOP"); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 源代码网推荐 } 源代码网推荐 public string PASS() 源代码网推荐 { 源代码网推荐 string temp; 源代码网推荐 if (state != connect_state.AUTHORIZATION) 源代码网推荐 { 源代码网推荐 //the pop command PASS is only valid in the AUTHORIZATION state 源代码网推荐 temp="Connection state not = AUTHORIZATION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 if (pwd !=null) 源代码网推荐 { 源代码网推荐 issue_command ("PASS " + pwd); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 源代码网推荐 if (!error) 源代码网推荐 { 源代码网推荐 //transition to the Transaction state 源代码网推荐 state=connect_state.TRANSACTION; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 temp="No Password set."; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 public string PASS(string password) 源代码网推荐 { 源代码网推荐 pwd=password; //put the supplied password into the appropriate property 源代码网推荐 return(PASS()); //call PASS() with no arguement 源代码网推荐 } 源代码网推荐 源代码网推荐 public string QUIT() 源代码网推荐 { 源代码网推荐 //QUIT is valid in all pop states 源代码网推荐 源代码网推荐 string temp; 源代码网推荐 if (state !=connect_state.disc) 源代码网推荐 { 源代码网推荐 issue_command ("QUIT"); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 temp += CRLF + disconnect(); 源代码网推荐 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 temp="Not Connected."; 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 源代码网推荐 } 源代码网推荐 public string RETR (int msg) 源代码网推荐 { 源代码网推荐 string temp=""; 源代码网推荐 if (state != connect_state.TRANSACTION ) 源代码网推荐 { 源代码网推荐 //the pop command RETR is only valid in the TRANSACTION state 源代码网推荐 temp="Connection state not = TRANSACTION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 // retrieve mail with number mail parameter 源代码网推荐 issue_command ("RETR "+ msg.ToString ()); 源代码网推荐 temp=read_multi_line_response(); 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 public string RSET() 源代码网推荐 { 源代码网推荐 string temp; 源代码网推荐 if (state != connect_state.TRANSACTION ) 源代码网推荐 { 源代码网推荐 //the pop command STAT is only valid in the TRANSACTION state 源代码网推荐 temp="Connection state not = TRANSACTION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 issue_command("RSET"); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 public string STAT() 源代码网推荐 { 源代码网推荐 string temp; 源代码网推荐 if (state==connect_state.TRANSACTION) 源代码网推荐 { 源代码网推荐 issue_command("STAT"); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 源代码网推荐 { 源代码网推荐 //the pop command STAT is only valid in the TRANSACTION state 源代码网推荐 return ("Connection state not = TRANSACTION"); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网推荐 public string USER() 源代码网推荐 { 源代码网推荐 string temp; 源代码网推荐 if (state != connect_state.AUTHORIZATION) 源代码网推荐 { 源代码网推荐 //the pop command USER is only valid in the AUTHORIZATION state 源代码网推荐 temp="Connection state not = AUTHORIZATION"; 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { 源代码网推荐 if (user !=null) 源代码网推荐 { 源代码网推荐 issue_command("USER "+ user); 源代码网推荐 temp=read_single_line_response(); 源代码网推荐 } 源代码网推荐 else 源代码网推荐 { //no user has been specified 源代码网推荐 temp="No User specified."; 源代码网推荐 } 源代码网推荐 } 源代码网推荐 return(temp); 源代码网推荐 } 源代码网推荐 源代码网推荐 public string USER(string user_name) 源代码网推荐 { 源代码网推荐 user=user_name; //put the user name in the appropriate propertity 源代码网推荐 return(USER()); //call USER with no arguements 源代码网推荐 } 源代码网推荐 #endregion 源代码网推荐 } 源代码网推荐 源代码网推荐 } 源代码网推荐 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
