using System; 
 using System.IO; 
 using ; 
 using .Sockets; namespace 删除帐户中的全部电子邮件 
 { 
 /// <summary> 
 /// Class1 的摘要说明。 
 /// </summary> 
 class Class1 
 { 
  /// <summary> 
  /// 应用程序的主入口点。 
  /// </summary> 
  private static TcpClient tpServer ; 
  private static byte[] byData ; 
  //设定一个字节数组,用以存放向POP3服务器传送的命令 
  private static string CRLF = "\r\n" ; 
  //定义回车换行符 
  private static string sRec ; 
  //定义一个字符串,用以存放从POP3服务器反馈数据 
  private static string sData ; 
  //定义一个字符串,用以存放向POP3服务器传送的命令字符串 
  private static NetworkStream nsStream; 
  //客户机和POP3服务器进行数据交换的基础数据流 
  private static StreamReader rdStream; 
  //从基础数据流中读取数据 
   
  [STAThread] 
  static void Main(string[] args) 
  { 
   if (LoadPOP("", "majinhu", "123456")) 
   { 
   sData = "STAT" + CRLF; 
   //定义获取帐户中邮件数目和占用空间的命令 
   byData = System.Text.Encoding.Default.GetBytes(sData.ToCharArray()); 
   nsStream.Write(byData, 0, byData.Length); 
   //向POP3服务器传送获取帐户中邮件数目和占用空间的命令 
   sRec = rdStream.ReadLine(); 
   //接收服务器反馈的数据 
   String[] Num = sRec.Split(" ".ToCharArray()); 
   //下列代码将删除帐户中的所有电子邮件 
   if (Int32.Parse(Num[1]) != 0) 
   { 
    Console.WriteLine("帐户中邮件数目:" + Num[1] + ",所占空间:" + Num[2] + "字节"); 
    int i; 
    for (i = 1; i < Int32.Parse(Num[1]) + 1; i++) 
    { 
    sData = "DELE " + i.ToString() + CRLF;   
    //定义删除帐户中的电子邮件字符串 
    byData = System.Text.Encoding.Default.GetBytes(sData.ToCharArray()); 
    nsStream.Write(byData, 0, byData.Length); 
    //向POP3服务器传送删除帐户中电子邮件命令 
    sRec = rdStream.ReadLine(); 
    //接收服务器反馈的数据 
    Console.WriteLine("删除帐户第" + i.ToString() + "封邮件!"); 
    } 
   } 
   else 
    Console.WriteLine("此帐户中没有邮件!"); 
   //下列代码是结束客户端和服务器端的会话 
   sData = "QUIT"+ CRLF; 
   //定义退出对话字符串 
   byData = System.Text.Encoding.Default.GetBytes(sData.ToCharArray()); 
   nsStream.Write(byData, 0, byData.Length); 
   //向POP3服务器传送删除帐户中电子邮件命令 
   sRec = rdStream.ReadLine(); 
   } 
   // 
   // TODO: 在此处添加代码以启动应用程序 
   // 
  } 
  public static bool LoadPOP ( string POPServer , string POPUser , string POPPsw ) 
  { 
   string sPOPServer = POPServer ; 
   string sPOPUser = POPUser ; 
   string sPOPPass = POPPsw ; 
   try 
   { 
   tpServer = new TcpClient ( sPOPServer , 110 ) ; 
   //和POP3服务器的110端口号建立TCP连接 
   } 
   catch 
   { 
   Console.WriteLine ("无法和指定的POP3服务器建立连接!" ) ; 
   return false; 
   } 
   nsStream = tpServer.GetStream ( ) ; 
   //获取客户机和服务器会话的数据流 
   rdStream = new StreamReader ( tpServer.GetStream ( ) ) ; 
   sRec = rdStream.ReadLine ( ) ; 
   sData = "USER "+ sPOPUser + CRLF ;   
   //定义向POP3服务器传送帐户对应的命令行 
   byData = System.Text.Encoding.Default.GetBytes ( sData.ToCharArray ( ) ) ; 
   nsStream.Write ( byData , 0 , byData.Length ) ; 
   //向POP3服务器传送帐户 
   sRec = rdStream.ReadLine ( ) ; 
   //接收POP3服务器反馈数据 
   Console.WriteLine ( "向POP3服务器传送帐户后,服务器返回的信息:" + sRec ) ; 
   string [ ] sUserString = sRec.Split ( " ".ToCharArray ( ) ) ; 
   if ( sUserString[ 0 ] == "-ERR" ) 
   { 
   Console.WriteLine ( "该POP3服务器中没有此帐户!" ) ; 
   return false; 
   } 
   sData = "PASS " + sPOPPass + CRLF ; 
   //定义向POP3服务器传送帐户口令对应的命令行 
   byData = System.Text.Encoding.Default.GetBytes ( sData.ToCharArray ( ) ) ; 
   nsStream.Write ( byData , 0 , byData.Length ) ; 
   //向POP3服务器传送帐户对应口令 
   sRec = rdStream.ReadLine ( ) ; 
   //接收POP3服务器反馈数据 
   Console.WriteLine ( "向POP3服务器传送帐户口令后,服务器返回的信息:" + sRec ) ; 
   string[ ] sPassString = sRec.Split ( " ".ToCharArray ( ) ) ; 
   if ( sPassString[ 0 ] == "-ERR" ) 
   { 
   Console.WriteLine ( "帐户对应的口令有误!" ) ; 
   return false; 
   } 
   return true; 
  }  } 
 }