using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Net; using System.Net.Sockets; using System.Text; using System.IO; |
private void button1_Click(object sender, System.EventArgs e) { Thread myThread=new Thread(new ThreadStart(TreadGet)); myThread.Start(); } private void TreadGet() { ConnectToServer(); LogonServer(); GetEmailList(); } //连接服务器 private void ConnectToServer() { this.myClient=new TcpClient(); try { this.myClient.Connect(this.serverNameTextBox.Text,110); this.messageListBox.Items.Add("连接服务器成功"); //初始化网络流,数据就是通过这个对象读出来的 ns=this.myClient.GetStream(); sr=new StreamReader(ns); sw=new StreamWriter(ns); } catch(Exception ex) { this.messageListBox.Items.Add(ex.Message); } } //验证用户名,密码 private void LogonServer() { result=sr.ReadLine(); this.messageListBox.Items.Add(result); sw.WriteLine("USER "+this.nameTextBox.Text); sw.Flush(); result=sr.ReadLine(); if(result.Substring(0,3)=="-ER") { this.messageListBox.Items.Add("没有这个用户名"); return; } sw.WriteLine("PASS "+this.passTextBox.Text); sw.Flush(); try { result=sr.ReadLine(); } catch(IOException ioex) { this.messageListBox.Items.Add(ioex.Message); return; } if(result.Substring(0,4)=="-ERR") { this.messageListBox.Items.Add("无法登录,可能使用户名密码错误!"); return; } this.messageListBox.Items.Add("登录成功"); } //获取邮件列表 private void GetEmailList() { string from=null; string subject=null; sw.WriteLine("stat"); sw.Flush(); result=sr.ReadLine(); // MessageBox.Show(result); string[] nummessage=result.Split(' '); int totalnum=Convert.ToInt32(nummessage[1]); if(totalnum>0) this.messageListBox.Items.Add("你有"+totalnum.ToString()+"邮件"); else this.messageListBox.Items.Add("邮箱里没有邮件"); for(int i=1;i<=totalnum;i++) { sw.WriteLine("top "+i.ToString()+" 0"); sw.Flush(); result=sr.ReadLine(); while(true) { result=sr.ReadLine(); if(result==".") break; if(result.Length>4) { if(result.Substring(0,5)=="From:") from=result ; if(result.Substring(0,8)=="Subject:") subject=result ; } } this.listBox1 .Items.Add(i.ToString()+" "+from+ " "+ subject); } } private void button2_Click(object sender, System.EventArgs e) { this.listBox1.Items.Clear(); } |