⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 email源程序 很简单 的 啊
💻 CS
字号:
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;

namespace MailReceiver
{
	/// <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.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox PopServer;
		private System.Windows.Forms.TextBox Username;
		private System.Windows.Forms.TextBox Password;
		private System.Windows.Forms.RichTextBox Message;
		private System.Windows.Forms.TextBox MailNum;
		private System.Windows.Forms.ListBox Status;
		private System.Windows.Forms.Button Connect;
		private System.Windows.Forms.Button Disconnect;
		private System.Windows.Forms.Button Retrieve;
		private System.Windows.Forms.CheckBox BackupChBox;

		public TcpClient Server;
		public NetworkStream NetStrm;
		public StreamReader  RdStrm;
		public string Data;
		public byte[] szData;
		public string CRLF = "\r\n";

		/// <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 Form Designer generated code
		/// <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.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.PopServer = new System.Windows.Forms.TextBox();
			this.Username = new System.Windows.Forms.TextBox();
			this.Password = new System.Windows.Forms.TextBox();
			this.MailNum = new System.Windows.Forms.TextBox();
			this.Connect = new System.Windows.Forms.Button();
			this.Disconnect = new System.Windows.Forms.Button();
			this.Retrieve = new System.Windows.Forms.Button();
			this.Message = new System.Windows.Forms.RichTextBox();
			this.Status = new System.Windows.Forms.ListBox();
			this.BackupChBox = new System.Windows.Forms.CheckBox();
			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 服务器地址:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 48);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(112, 16);
			this.label2.TabIndex = 1;
			this.label2.Text = "用户名:";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 80);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(112, 16);
			this.label3.TabIndex = 2;
			this.label3.Text = "密码:";
			this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 272);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 23);
			this.label4.TabIndex = 3;
			this.label4.Text = "邮件编号:";
			this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(8, 112);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(100, 16);
			this.label5.TabIndex = 4;
			this.label5.Text = "信息:";
			this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(8, 304);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(100, 16);
			this.label6.TabIndex = 5;
			this.label6.Text = "状态:";
			this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// PopServer
			// 
			this.PopServer.Location = new System.Drawing.Point(136, 16);
			this.PopServer.Name = "PopServer";
			this.PopServer.Size = new System.Drawing.Size(168, 21);
			this.PopServer.TabIndex = 0;
			this.PopServer.Text = "";
			// 
			// Username
			// 
			this.Username.Location = new System.Drawing.Point(136, 48);
			this.Username.Name = "Username";
			this.Username.Size = new System.Drawing.Size(168, 21);
			this.Username.TabIndex = 1;
			this.Username.Text = "";
			// 
			// Password
			// 
			this.Password.Location = new System.Drawing.Point(136, 80);
			this.Password.Name = "Password";
			this.Password.PasswordChar = '*';
			this.Password.Size = new System.Drawing.Size(168, 21);
			this.Password.TabIndex = 2;
			this.Password.Text = "";
			// 
			// MailNum
			// 
			this.MailNum.Location = new System.Drawing.Point(88, 272);
			this.MailNum.Name = "MailNum";
			this.MailNum.Size = new System.Drawing.Size(40, 21);
			this.MailNum.TabIndex = 4;
			this.MailNum.Text = "";
			// 
			// Connect
			// 
			this.Connect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Connect.Location = new System.Drawing.Point(320, 24);
			this.Connect.Name = "Connect";
			this.Connect.Size = new System.Drawing.Size(75, 24);
			this.Connect.TabIndex = 7;
			this.Connect.Text = "连接";
			this.Connect.Click += new System.EventHandler(this.Connect_Click);
			// 
			// Disconnect
			// 
			this.Disconnect.Enabled = false;
			this.Disconnect.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Disconnect.Location = new System.Drawing.Point(320, 64);
			this.Disconnect.Name = "Disconnect";
			this.Disconnect.Size = new System.Drawing.Size(75, 24);
			this.Disconnect.TabIndex = 8;
			this.Disconnect.Text = "断开连接";
			this.Disconnect.Click += new System.EventHandler(this.Disconnect_Click);
			// 
			// Retrieve
			// 
			this.Retrieve.Enabled = false;
			this.Retrieve.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.Retrieve.Location = new System.Drawing.Point(320, 272);
			this.Retrieve.Name = "Retrieve";
			this.Retrieve.Size = new System.Drawing.Size(72, 24);
			this.Retrieve.TabIndex = 9;
			this.Retrieve.Text = "收取邮件";
			this.Retrieve.Click += new System.EventHandler(this.Retrieve_Click);
			// 
			// Message
			// 
			this.Message.Location = new System.Drawing.Point(8, 136);
			this.Message.Name = "Message";
			this.Message.Size = new System.Drawing.Size(384, 128);
			this.Message.TabIndex = 3;
			this.Message.Text = "";
			// 
			// Status
			// 
			this.Status.ItemHeight = 12;
			this.Status.Location = new System.Drawing.Point(8, 328);
			this.Status.Name = "Status";
			this.Status.Size = new System.Drawing.Size(384, 52);
			this.Status.TabIndex = 6;
			// 
			// BackupChBox
			// 
			this.BackupChBox.Location = new System.Drawing.Point(144, 272);
			this.BackupChBox.Name = "BackupChBox";
			this.BackupChBox.Size = new System.Drawing.Size(160, 24);
			this.BackupChBox.TabIndex = 5;
			this.BackupChBox.Text = "在邮件服务器上保留备份";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(400, 389);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.BackupChBox,
																		  this.Status,
																		  this.Message,
																		  this.Retrieve,
																		  this.Disconnect,
																		  this.Connect,
																		  this.MailNum,
																		  this.Password,
																		  this.Username,
																		  this.PopServer,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.label1});
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "POP3邮件接收程序";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		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());
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -