Form1.cs
上传用户:youdujia
上传日期:2017-09-03
资源大小:100k
文件大小:11k
源码类别:

WEB邮件程序

开发平台:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Net;
  8. using System.Net.Sockets;
  9. using System.IO;
  10. using System.Text;
  11. namespace POP3Server
  12. {
  13. /// <summary>
  14. /// Form1 的摘要说明。
  15. /// </summary>
  16. public class Form1 : System.Windows.Forms.Form
  17. {
  18. private System.Windows.Forms.Label label1;
  19. private System.Windows.Forms.Label label2;
  20. private System.Windows.Forms.Label label3;
  21. private System.Windows.Forms.GroupBox groupBox1;
  22. private System.Windows.Forms.Label label4;
  23. private System.Windows.Forms.CheckBox checkBox1;
  24. private System.Windows.Forms.GroupBox groupBox2;
  25.         //添加公有数据成员
  26. public TcpClient Server;
  27.         public NetworkStream NetStrm;
  28. public StreamReader RdStrm;
  29. public string Data;
  30. public byte[] szData;
  31. private System.Windows.Forms.TextBox txtPopServer;
  32. private System.Windows.Forms.TextBox txtUsername;
  33. private System.Windows.Forms.TextBox txtPassword;
  34. private System.Windows.Forms.Button btnConnect;
  35. private System.Windows.Forms.Button btnDisConnect;
  36. private System.Windows.Forms.TextBox txtMailNum;
  37. private System.Windows.Forms.Button btnRetrieve;
  38. private System.Windows.Forms.RichTextBox rtbMessage;
  39. private System.Windows.Forms.ListBox Status;
  40. /// <summary>
  41. /// 必需的设计器变量。
  42. /// </summary>
  43. private System.ComponentModel.Container components = null;
  44. public Form1()
  45. {
  46. //
  47. // Windows 窗体设计器支持所必需的
  48. //
  49. InitializeComponent();
  50. //
  51. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  52. //
  53. }
  54. /// <summary>
  55. /// 清理所有正在使用的资源。
  56. /// </summary>
  57. protected override void Dispose( bool disposing )
  58. {
  59. if( disposing )
  60. {
  61. if (components != null) 
  62. {
  63. components.Dispose();
  64. }
  65. }
  66. base.Dispose( disposing );
  67. }
  68. #region Windows 窗体设计器生成的代码
  69. /// <summary>
  70. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  71. /// 此方法的内容。
  72. /// </summary>
  73. private void InitializeComponent()
  74. {
  75. this.label1 = new System.Windows.Forms.Label();
  76. this.label2 = new System.Windows.Forms.Label();
  77. this.label3 = new System.Windows.Forms.Label();
  78. this.txtPopServer = new System.Windows.Forms.TextBox();
  79. this.txtUsername = new System.Windows.Forms.TextBox();
  80. this.txtPassword = new System.Windows.Forms.TextBox();
  81. this.btnConnect = new System.Windows.Forms.Button();
  82. this.btnDisConnect = new System.Windows.Forms.Button();
  83. this.groupBox1 = new System.Windows.Forms.GroupBox();
  84. this.rtbMessage = new System.Windows.Forms.RichTextBox();
  85. this.label4 = new System.Windows.Forms.Label();
  86. this.txtMailNum = new System.Windows.Forms.TextBox();
  87. this.checkBox1 = new System.Windows.Forms.CheckBox();
  88. this.btnRetrieve = new System.Windows.Forms.Button();
  89. this.groupBox2 = new System.Windows.Forms.GroupBox();
  90. this.Status = new System.Windows.Forms.ListBox();
  91. this.groupBox1.SuspendLayout();
  92. this.groupBox2.SuspendLayout();
  93. this.SuspendLayout();
  94. // 
  95. // label1
  96. // 
  97. this.label1.Location = new System.Drawing.Point(16, 16);
  98. this.label1.Name = "label1";
  99. this.label1.Size = new System.Drawing.Size(112, 23);
  100. this.label1.TabIndex = 0;
  101. this.label1.Text = "POP3服务器地址:";
  102. // 
  103. // label2
  104. // 
  105. this.label2.Location = new System.Drawing.Point(16, 48);
  106. this.label2.Name = "label2";
  107. this.label2.Size = new System.Drawing.Size(112, 23);
  108. this.label2.TabIndex = 1;
  109. this.label2.Text = "用户名:";
  110. // 
  111. // label3
  112. // 
  113. this.label3.Location = new System.Drawing.Point(16, 80);
  114. this.label3.Name = "label3";
  115. this.label3.Size = new System.Drawing.Size(112, 23);
  116. this.label3.TabIndex = 2;
  117. this.label3.Text = "密 码:";
  118. // 
  119. // txtPopServer
  120. // 
  121. this.txtPopServer.Location = new System.Drawing.Point(120, 16);
  122. this.txtPopServer.Name = "txtPopServer";
  123. this.txtPopServer.Size = new System.Drawing.Size(192, 21);
  124. this.txtPopServer.TabIndex = 3;
  125. this.txtPopServer.Text = "pop.126.com";
  126. // 
  127. // txtUsername
  128. // 
  129. this.txtUsername.Location = new System.Drawing.Point(120, 48);
  130. this.txtUsername.Name = "txtUsername";
  131. this.txtUsername.Size = new System.Drawing.Size(192, 21);
  132. this.txtUsername.TabIndex = 4;
  133. this.txtUsername.Text = "hrk11111";
  134. // 
  135. // txtPassword
  136. // 
  137. this.txtPassword.Location = new System.Drawing.Point(120, 80);
  138. this.txtPassword.Name = "txtPassword";
  139. this.txtPassword.PasswordChar = '*';
  140. this.txtPassword.Size = new System.Drawing.Size(192, 21);
  141. this.txtPassword.TabIndex = 5;
  142. this.txtPassword.Text = "19831022";
  143. // 
  144. // btnConnect
  145. // 
  146. this.btnConnect.Location = new System.Drawing.Point(120, 120);
  147. this.btnConnect.Name = "btnConnect";
  148. this.btnConnect.TabIndex = 6;
  149. this.btnConnect.Text = "连接";
  150. this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
  151. // 
  152. // btnDisConnect
  153. // 
  154. this.btnDisConnect.Location = new System.Drawing.Point(240, 120);
  155. this.btnDisConnect.Name = "btnDisConnect";
  156. this.btnDisConnect.TabIndex = 7;
  157. this.btnDisConnect.Text = "断开连接";
  158. this.btnDisConnect.Click += new System.EventHandler(this.btnDisConnect_Click);
  159. // 
  160. // groupBox1
  161. // 
  162. this.groupBox1.Controls.Add(this.rtbMessage);
  163. this.groupBox1.Location = new System.Drawing.Point(16, 320);
  164. this.groupBox1.Name = "groupBox1";
  165. this.groupBox1.Size = new System.Drawing.Size(400, 136);
  166. this.groupBox1.TabIndex = 8;
  167. this.groupBox1.TabStop = false;
  168. this.groupBox1.Text = "信息:";
  169. // 
  170. // rtbMessage
  171. // 
  172. this.rtbMessage.Location = new System.Drawing.Point(0, 16);
  173. this.rtbMessage.Name = "rtbMessage";
  174. this.rtbMessage.Size = new System.Drawing.Size(400, 120);
  175. this.rtbMessage.TabIndex = 0;
  176. this.rtbMessage.Text = "";
  177. // 
  178. // label4
  179. // 
  180. this.label4.Location = new System.Drawing.Point(16, 288);
  181. this.label4.Name = "label4";
  182. this.label4.Size = new System.Drawing.Size(72, 23);
  183. this.label4.TabIndex = 9;
  184. this.label4.Text = "邮件编号";
  185. // 
  186. // txtMailNum
  187. // 
  188. this.txtMailNum.Location = new System.Drawing.Point(104, 288);
  189. this.txtMailNum.Name = "txtMailNum";
  190. this.txtMailNum.Size = new System.Drawing.Size(32, 21);
  191. this.txtMailNum.TabIndex = 10;
  192. this.txtMailNum.Text = "1";
  193. // 
  194. // checkBox1
  195. // 
  196. this.checkBox1.Checked = true;
  197. this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
  198. this.checkBox1.Location = new System.Drawing.Point(160, 288);
  199. this.checkBox1.Name = "checkBox1";
  200. this.checkBox1.Size = new System.Drawing.Size(136, 24);
  201. this.checkBox1.TabIndex = 11;
  202. this.checkBox1.Text = "在服务器上保留备份";
  203. // 
  204. // btnRetrieve
  205. // 
  206. this.btnRetrieve.Location = new System.Drawing.Point(336, 288);
  207. this.btnRetrieve.Name = "btnRetrieve";
  208. this.btnRetrieve.TabIndex = 12;
  209. this.btnRetrieve.Text = "收取邮件";
  210. this.btnRetrieve.Click += new System.EventHandler(this.btnRetrieve_Click);
  211. // 
  212. // groupBox2
  213. // 
  214. this.groupBox2.Controls.Add(this.Status);
  215. this.groupBox2.Location = new System.Drawing.Point(16, 152);
  216. this.groupBox2.Name = "groupBox2";
  217. this.groupBox2.Size = new System.Drawing.Size(400, 128);
  218. this.groupBox2.TabIndex = 13;
  219. this.groupBox2.TabStop = false;
  220. this.groupBox2.Text = "状态:";
  221. // 
  222. // Status
  223. // 
  224. this.Status.ItemHeight = 12;
  225. this.Status.Location = new System.Drawing.Point(0, 16);
  226. this.Status.Name = "Status";
  227. this.Status.Size = new System.Drawing.Size(400, 100);
  228. this.Status.TabIndex = 0;
  229. // 
  230. // Form1
  231. // 
  232. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  233. this.AutoScroll = true;
  234. this.ClientSize = new System.Drawing.Size(424, 462);
  235. this.Controls.Add(this.groupBox2);
  236. this.Controls.Add(this.btnRetrieve);
  237. this.Controls.Add(this.checkBox1);
  238. this.Controls.Add(this.txtMailNum);
  239. this.Controls.Add(this.label4);
  240. this.Controls.Add(this.groupBox1);
  241. this.Controls.Add(this.btnDisConnect);
  242. this.Controls.Add(this.btnConnect);
  243. this.Controls.Add(this.txtPassword);
  244. this.Controls.Add(this.txtUsername);
  245. this.Controls.Add(this.txtPopServer);
  246. this.Controls.Add(this.label3);
  247. this.Controls.Add(this.label2);
  248. this.Controls.Add(this.label1);
  249. this.HelpButton = true;
  250. this.MaximizeBox = false;
  251. this.Name = "Form1";
  252. this.Text = "POP3服务器";
  253. this.TopMost = true;
  254. this.groupBox1.ResumeLayout(false);
  255. this.groupBox2.ResumeLayout(false);
  256. this.ResumeLayout(false);
  257. }
  258. #endregion
  259. /// <summary>
  260. /// 应用程序的主入口点。
  261. /// </summary>
  262. [STAThread]
  263. static void Main() 
  264. {
  265. Application.Run(new Form1());
  266. }
  267. private void btnConnect_Click(object sender, System.EventArgs e)
  268. {
  269. //用110端口新建POP3服务器连接
  270. TcpClient Server = new TcpClient(this.txtPopServer.Text,110);
  271. Status.Items.Clear();
  272. try
  273. {
  274. //初始化
  275. NetStrm = Server.GetStream();
  276. RdStrm = new StreamReader(Server.GetStream());
  277. Status.Items.Add(RdStrm.ReadLine());
  278. //登录服务器过程
  279. Data = "USER:"+this.txtUsername.Text+"rn";
  280. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  281. NetStrm.Write(szData,0,szData.Length);
  282. Status.Items.Add(RdStrm.ReadLine());
  283. Data ="PASS:"+this.txtPassword.Text+"rn";
  284. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  285. NetStrm.Write(szData,0,szData.Length);
  286. Status.Items.Add(RdStrm.ReadLine());
  287. }
  288. catch(InvalidOperationException err)
  289. {
  290. Status.Items.Add("Error:"+err.ToString());
  291. }
  292. }
  293. private void btnDisConnect_Click(object sender, System.EventArgs e)
  294. {
  295. //发送QUIT命令
  296. Data = "QUIT"+"rn";
  297. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  298. NetStrm.Write(szData,0,szData.Length);
  299. Status.Items.Add(RdStrm.ReadLine());
  300. //断开连接
  301. NetStrm.Close();
  302. RdStrm.Close();
  303. }
  304. private void btnRetrieve_Click(object sender, System.EventArgs e)
  305. {
  306. string szTemp;
  307. this.rtbMessage.Clear();
  308. try
  309. {
  310. //根据邮件编号从服务器获取相应邮件
  311. Data = "RETR:"+this.txtMailNum.Text+"rn";
  312. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  313. NetStrm.Write(szData,0,szData.Length);
  314. szTemp = RdStrm.ReadLine();
  315. if(szTemp[0] != '-')
  316. {
  317. while(szTemp.IndexOf("<End>") <= 0)
  318. {
  319. this.rtbMessage.Text += szTemp;
  320. szTemp = RdStrm.ReadLine();
  321. }
  322. }
  323. if(this.checkBox1.Checked == false)
  324. {
  325. Data = "DELE:"+this.txtMailNum.Text+"rn";
  326. szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
  327. NetStrm.Write(szData,0,szData.Length);
  328. this.Status.Items.Add(RdStrm.ReadLine());
  329. }
  330. else
  331. {
  332. Status.Items.Add(szTemp);
  333. }
  334. }
  335. catch(InvalidOperationException err)
  336. {
  337. Status.Items.Add("Error:"+err.ToString());
  338. }
  339. }
  340. }
  341. }