pop3协议之二:tcp连接

 

         public  PopMailBox( string  username , string  password,  string  popserver,  int  port , int  delay)
        {
            
this .username  =  username;
            
this .password  =  password;
            
this .popserver  =  popserver;
            
this .port  =  port;
            
this .delay  =  delay;
        }
        
///   <summary>
        
///  
        
///   </summary>
        
///   <param name="username"> pop服务器登陆名 </param>
        
///   <param name="password"> pop服务器登陆密码 </param>
        
///   <param name="popserver"> pop服务器名 </param>
         public  PopMailBox( string  username,  string  password,  string  popserver)
        {
            
this .username  =  username;
            
this .password  =  password;
            
this .popserver  =  popserver;
        }
        
///   <summary>
        
///  
        
///   </summary>
        
///   <param name="username"> pop服务器登陆名 </param>
        
///   <param name="password"> pop服务器登陆密码 </param>
        
///   <param name="popserver"> pop服务器名 </param>
        
///   <param name="port"> pop服务器端口 </param>
         public  PopMailBox( string  username,  string  password,  string  popserver, int  port)
        {
            
this .username  =  username;
            
this .password  =  password;
            
this .popserver  =  popserver;
            
this .port  =  port;
        }

        
public   bool  Connect()
        {
            
try
            {
                client 
=   new  TcpClient( popserver, port );
                isConnected 
=   true ;
                
string  r  =  Receive();
                
if  (r.Length  >   0 )
                {
                    
if  (r.IndexOf( " +OK " ) == 0 )
                        
return   true ;
                    
else
                    {
                        Error
= " 响应协议不正确 " ;
                        
return   false ;
                    }
                }
                
else
                {
                    Error
= " 服务器无响应 " ;
                    isConnected 
=   false ;
                    client.Close();
                    
return   false ;
                    
                }
            }
            
catch (Exception ex)
            {
                Error 
=  ex.Message;
                
return   false ;
            }
        }

        
public   void  Close()
        {
            
if  (client  !=   null )
            {
                
if  ( this .isConnected)
                    Send(
" quit " );
                client.Close();
            }
        }

        
public   bool  Login()
        {
            Send(
" user  "   +  username);
            
if  (Receive().Substring( 0 , 3 ) == " +OK " )
            {
                Send(
" pass  "   +  password);
                
if  (Receive().Substring( 0 , 3 ) == " +OK " )
                    
return   true ;
                
else
                {
                    Send(
" quit " );
                    
this .Error  =   " 密码错误 " ;
                    
return   false ;
                }
            }
            
else
            {
                Send(
" quit " );
                
this .Error = " 用户名错误 " ;
                
return   false ;
            }

                
        }


         public   void  Send( string  data)
        {
            
byte [] buffer  =  Encoder.GetBytes(data  +   " \r\n " );
            
if  ( this .isConnected)
            {
                client.GetStream().Write(buffer,
0 ,buffer.Length);
                debug 
+=  data  +   " \n " ;
            }
        }

        
public   string  Receive()
        {
//             return Receive(false);
             byte [] buffer;
            
int  numberOfBytesRead;
            
if  ( this .IsConnected)
            {
                buffer 
=   new   byte [client.ReceiveBufferSize];
                
if  ((numberOfBytesRead = client.GetStream().Read(buffer, 0 ,buffer.Length))  !=   0 )
                {
                    
return  Encoder.GetString(buffer, 0 ,numberOfBytesRead);
                }
                
else
                {
                    
this .isConnected  =   false ;
                    
this .Error  =   " 连接被断开 " ;
                    
return   "" ;
                }
            }
            
this .Error  =   " 尚未连接 " ;
            
return   "" ;
        }

        
public   string  Receive( bool  multiLine)
        {
            
if  ( ! multiLine)
                
return  Receive();
            
else
            {
                
string  endCondition;
                
string  result = String.Empty;
                
byte [] buffer;
                
int  numberOfBytesRead  =   0 ;
                
bool  finish  =   false ;
                DateTime startTime 
=  DateTime.Now;
                endCondition 
=   " .\r\n " ;
                
if  ( this .IsConnected  &&  Receive().Substring( 0 , 3 ==   " +OK " )
                {
                    buffer 
=   new   byte [client.ReceiveBufferSize];
                    
while  (  ! finish  &&  (numberOfBytesRead = client.GetStream().Read(buffer, 0 ,buffer.Length))  !=   0 )
                    {
                        
string  temp  =  Encoder.GetString(buffer, 0 ,numberOfBytesRead);
                        
if  (temp.IndexOf(endCondition)  >=   0 )
                        {
                            finish 
=   true ;
                            temp 
=  temp.Replace(endCondition, "" );
                        }
                        result 
+=  temp;
                    }
                    
if  ( numberOfBytesRead  ==   0 )
                        
this .isConnected  =   false ;
                }
                
return  result;
            }
        }

        
private   string  Stat()
        {
            Send(
" stat " );
            
return  Receive();
        }

        
public   string  List()
        {
            Send(
" list " );
            
return  Receive( true );
        }

        
public   string  Uidl( int  index)
        {
            Send(
" uidl  "   +  index.ToString());
            
return  Receive();
        }

        
public   string  Uidl()
        {
            Send(
" uidl " );
            
return  Receive( true );
        }

        
public   string  Dele(  int  index )
        {
            Send(
" dele  "   +  index.ToString());
            
return  Receive();
        }

        
public   string  Retr( int  index)
        {
            Send(
" retr  "   +  index.ToString());
            
return  Receive( true );
        }

你可能感兴趣的:(tcp)