POP3 Client as a C# Class(转自CodeProject)
2024-07-21 02:16:53
供稿:网友
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.
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
}
}
//转自codeproject