Form1.cs
上传用户:youdujia
上传日期:2017-09-03
资源大小:100k
文件大小:11k
- 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.IO;
- using System.Text;
- namespace POP3Server
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.CheckBox checkBox1;
- private System.Windows.Forms.GroupBox groupBox2;
- //添加公有数据成员
- public TcpClient Server;
- public NetworkStream NetStrm;
- public StreamReader RdStrm;
- public string Data;
- public byte[] szData;
-
- private System.Windows.Forms.TextBox txtPopServer;
- private System.Windows.Forms.TextBox txtUsername;
- private System.Windows.Forms.TextBox txtPassword;
- private System.Windows.Forms.Button btnConnect;
- private System.Windows.Forms.Button btnDisConnect;
- private System.Windows.Forms.TextBox txtMailNum;
- private System.Windows.Forms.Button btnRetrieve;
- private System.Windows.Forms.RichTextBox rtbMessage;
- private System.Windows.Forms.ListBox Status;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if (components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.label1 = new System.Windows.Forms.Label();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.txtPopServer = new System.Windows.Forms.TextBox();
- this.txtUsername = new System.Windows.Forms.TextBox();
- this.txtPassword = new System.Windows.Forms.TextBox();
- this.btnConnect = new System.Windows.Forms.Button();
- this.btnDisConnect = new System.Windows.Forms.Button();
- this.groupBox1 = new System.Windows.Forms.GroupBox();
- this.rtbMessage = new System.Windows.Forms.RichTextBox();
- this.label4 = new System.Windows.Forms.Label();
- this.txtMailNum = new System.Windows.Forms.TextBox();
- this.checkBox1 = new System.Windows.Forms.CheckBox();
- this.btnRetrieve = new System.Windows.Forms.Button();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.Status = new System.Windows.Forms.ListBox();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.SuspendLayout();
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(16, 16);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(112, 23);
- this.label1.TabIndex = 0;
- this.label1.Text = "POP3服务器地址:";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(16, 48);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(112, 23);
- this.label2.TabIndex = 1;
- this.label2.Text = "用户名:";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(16, 80);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(112, 23);
- this.label3.TabIndex = 2;
- this.label3.Text = "密 码:";
- //
- // txtPopServer
- //
- this.txtPopServer.Location = new System.Drawing.Point(120, 16);
- this.txtPopServer.Name = "txtPopServer";
- this.txtPopServer.Size = new System.Drawing.Size(192, 21);
- this.txtPopServer.TabIndex = 3;
- this.txtPopServer.Text = "pop.126.com";
- //
- // txtUsername
- //
- this.txtUsername.Location = new System.Drawing.Point(120, 48);
- this.txtUsername.Name = "txtUsername";
- this.txtUsername.Size = new System.Drawing.Size(192, 21);
- this.txtUsername.TabIndex = 4;
- this.txtUsername.Text = "hrk11111";
- //
- // txtPassword
- //
- this.txtPassword.Location = new System.Drawing.Point(120, 80);
- this.txtPassword.Name = "txtPassword";
- this.txtPassword.PasswordChar = '*';
- this.txtPassword.Size = new System.Drawing.Size(192, 21);
- this.txtPassword.TabIndex = 5;
- this.txtPassword.Text = "19831022";
- //
- // btnConnect
- //
- this.btnConnect.Location = new System.Drawing.Point(120, 120);
- this.btnConnect.Name = "btnConnect";
- this.btnConnect.TabIndex = 6;
- this.btnConnect.Text = "连接";
- this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
- //
- // btnDisConnect
- //
- this.btnDisConnect.Location = new System.Drawing.Point(240, 120);
- this.btnDisConnect.Name = "btnDisConnect";
- this.btnDisConnect.TabIndex = 7;
- this.btnDisConnect.Text = "断开连接";
- this.btnDisConnect.Click += new System.EventHandler(this.btnDisConnect_Click);
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.rtbMessage);
- this.groupBox1.Location = new System.Drawing.Point(16, 320);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(400, 136);
- this.groupBox1.TabIndex = 8;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "信息:";
- //
- // rtbMessage
- //
- this.rtbMessage.Location = new System.Drawing.Point(0, 16);
- this.rtbMessage.Name = "rtbMessage";
- this.rtbMessage.Size = new System.Drawing.Size(400, 120);
- this.rtbMessage.TabIndex = 0;
- this.rtbMessage.Text = "";
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(16, 288);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(72, 23);
- this.label4.TabIndex = 9;
- this.label4.Text = "邮件编号";
- //
- // txtMailNum
- //
- this.txtMailNum.Location = new System.Drawing.Point(104, 288);
- this.txtMailNum.Name = "txtMailNum";
- this.txtMailNum.Size = new System.Drawing.Size(32, 21);
- this.txtMailNum.TabIndex = 10;
- this.txtMailNum.Text = "1";
- //
- // checkBox1
- //
- this.checkBox1.Checked = true;
- this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
- this.checkBox1.Location = new System.Drawing.Point(160, 288);
- this.checkBox1.Name = "checkBox1";
- this.checkBox1.Size = new System.Drawing.Size(136, 24);
- this.checkBox1.TabIndex = 11;
- this.checkBox1.Text = "在服务器上保留备份";
- //
- // btnRetrieve
- //
- this.btnRetrieve.Location = new System.Drawing.Point(336, 288);
- this.btnRetrieve.Name = "btnRetrieve";
- this.btnRetrieve.TabIndex = 12;
- this.btnRetrieve.Text = "收取邮件";
- this.btnRetrieve.Click += new System.EventHandler(this.btnRetrieve_Click);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.Status);
- this.groupBox2.Location = new System.Drawing.Point(16, 152);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(400, 128);
- this.groupBox2.TabIndex = 13;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "状态:";
- //
- // Status
- //
- this.Status.ItemHeight = 12;
- this.Status.Location = new System.Drawing.Point(0, 16);
- this.Status.Name = "Status";
- this.Status.Size = new System.Drawing.Size(400, 100);
- this.Status.TabIndex = 0;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.AutoScroll = true;
- this.ClientSize = new System.Drawing.Size(424, 462);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.btnRetrieve);
- this.Controls.Add(this.checkBox1);
- this.Controls.Add(this.txtMailNum);
- this.Controls.Add(this.label4);
- this.Controls.Add(this.groupBox1);
- this.Controls.Add(this.btnDisConnect);
- this.Controls.Add(this.btnConnect);
- this.Controls.Add(this.txtPassword);
- this.Controls.Add(this.txtUsername);
- this.Controls.Add(this.txtPopServer);
- this.Controls.Add(this.label3);
- this.Controls.Add(this.label2);
- this.Controls.Add(this.label1);
- this.HelpButton = true;
- this.MaximizeBox = false;
- this.Name = "Form1";
- this.Text = "POP3服务器";
- this.TopMost = true;
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void btnConnect_Click(object sender, System.EventArgs e)
- {
- //用110端口新建POP3服务器连接
- TcpClient Server = new TcpClient(this.txtPopServer.Text,110);
- Status.Items.Clear();
- try
- {
- //初始化
- NetStrm = Server.GetStream();
- RdStrm = new StreamReader(Server.GetStream());
- Status.Items.Add(RdStrm.ReadLine());
- //登录服务器过程
- Data = "USER:"+this.txtUsername.Text+"rn";
- szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
- NetStrm.Write(szData,0,szData.Length);
- Status.Items.Add(RdStrm.ReadLine());
- Data ="PASS:"+this.txtPassword.Text+"rn";
- szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
- NetStrm.Write(szData,0,szData.Length);
- Status.Items.Add(RdStrm.ReadLine());
- }
- catch(InvalidOperationException err)
- {
- Status.Items.Add("Error:"+err.ToString());
- }
- }
- private void btnDisConnect_Click(object sender, System.EventArgs e)
- {
- //发送QUIT命令
- Data = "QUIT"+"rn";
- szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
- NetStrm.Write(szData,0,szData.Length);
- Status.Items.Add(RdStrm.ReadLine());
- //断开连接
- NetStrm.Close();
- RdStrm.Close();
- }
- private void btnRetrieve_Click(object sender, System.EventArgs e)
- {
- string szTemp;
- this.rtbMessage.Clear();
- try
- {
- //根据邮件编号从服务器获取相应邮件
- Data = "RETR:"+this.txtMailNum.Text+"rn";
- szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
- NetStrm.Write(szData,0,szData.Length);
- szTemp = RdStrm.ReadLine();
- if(szTemp[0] != '-')
- {
- while(szTemp.IndexOf("<End>") <= 0)
- {
- this.rtbMessage.Text += szTemp;
- szTemp = RdStrm.ReadLine();
- }
- }
- if(this.checkBox1.Checked == false)
- {
- Data = "DELE:"+this.txtMailNum.Text+"rn";
- szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
- NetStrm.Write(szData,0,szData.Length);
- this.Status.Items.Add(RdStrm.ReadLine());
- }
- else
- {
- Status.Items.Add(szTemp);
- }
- }
- catch(InvalidOperationException err)
- {
- Status.Items.Add("Error:"+err.ToString());
- }
- }
- }
- }