扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:王凯明 来源:yesky 2007年11月14日
关键字:
using System.Net; using System.Net.Sockets; using System.IO; |
public TcpClient Server; public NetworkStream NetStrm; public StreamReader RdStrm; public string Data; public byte[] szData; public string CRLF = "\r\n"; |
private void Connect_Click(object sender, System.EventArgs e) { //将光标置为等待状态 Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; //用110端口新建POP3服务器连接 Server = new TcpClient(PopServer.Text,110); Status.Items.Clear(); try { //初始化 NetStrm = Server.GetStream(); RdStrm= new StreamReader(Server.GetStream()); Status.Items.Add(RdStrm.ReadLine()); //登录服务器过程 Data = "USER "+ Username.Text+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); Data = "PASS "+ Password.Text+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); //向服务器发送STAT命令,从而取得邮箱的相关信息:邮件数量和大小 Data = "STAT"+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); //改变按钮的状态 Connect.Enabled = false; Disconnect.Enabled = true; Retrieve.Enabled = true; //将光标置回原来的状态 Cursor.Current = cr; } catch(InvalidOperationException err) { Status.Items.Add("Error: "+err.ToString()); } } |
private void Disconnect_Click(object sender, System.EventArgs e) { //将光标置为等待状态 Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; //向服务器发送QUIT命令从而结束和POP3服务器的会话 Data = "QUIT"+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); //断开连接 NetStrm.Close(); RdStrm.Close(); //改变按钮的状态 Connect.Enabled = true; Disconnect.Enabled = false; Retrieve.Enabled = false; //将光标置回原来的状态 Cursor.Current = cr; } |
private void Retrieve_Click(object sender, System.EventArgs e) { //将光标置为等待状态 Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; string szTemp; Message.Clear(); try { //根据邮件编号从服务器获得相应邮件 Data = "RETR "+ MailNum.Text+CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); szTemp = RdStrm.ReadLine(); if(szTemp[0]!='-') { //不断地读取邮件内容,只到结束标志:英文句号 while(szTemp!=".") { Message.Text += szTemp; szTemp = RdStrm.ReadLine(); } //若BackupChBox未选中,则收取邮件后,删除保留在服务器上的邮件 if(BackupChBox.Checked == false) { Data = "DELE" + MailNum.Text + CRLF; szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray()); NetStrm.Write(szData,0,szData.Length); Status.Items.Add(RdStrm.ReadLine()); } } else { Status.Items.Add(szTemp); } //将光标置回原来的状态 Cursor.Current = cr; } catch(InvalidOperationException err) { Status.Items.Add("Error: "+err.ToString()); } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者