namespace POP3Client {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 = "/ r / n"; 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. p op = 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 () {// 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 ()); ". disconnected successfully"}} private string disconnect () {string temp =; if (! state = connect_state.disc) {/ / close connection netstrm.close (); rdstrm.close (); state = connect_state.disc;} else {temp = "NOT Connected.";} return (temp);} private void issu_Command 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.codeproj Ect.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 ());} } P rivate 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 "-" THE // POP Server Has Encountered An Error Executing The Last // Command Send by The Client Error = True;} else {// Success Error = false;}} #endregion #REGON 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! = Connection_state.transaction) {// The pop commnd list is only valid in the transactions 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 transactions;} 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;} ". No password set"} else {temp =;}} return (temp);} public string PASS (string password ) {PWD = password; // put the support password INTO the appropriate property return (Pass ()); // call pass () with no arguement} public string quit () {quit is valid in all pop statings temind teemp ; If (state! = Connection_state.disc) {Issue_Command ("quit"); temp = read_single_line_response (); temp = crlf disconnect ();} else {temp = "n } returncted;} public string retrin {string Temp = ""; if (state! = connection_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! =