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.IO;
  9. using System.Net.Sockets ;
  10. using System.Text ;
  11. namespace MailPOP3
  12. {
  13. /// <summary>
  14. /// Form1 的摘要说明。
  15. /// </summary>
  16. public class Form1 : System.Windows.Forms.Form
  17. {
  18. private System.Windows.Forms.GroupBox groupBox1;
  19. private System.Windows.Forms.Label label1;
  20. private System.Windows.Forms.Label label3;
  21. private System.Windows.Forms.Label label4;
  22. private System.Windows.Forms.GroupBox groupBox2;
  23. private System.Windows.Forms.GroupBox groupBox3;
  24. private System.Windows.Forms.TextBox servername;
  25. private System.Windows.Forms.TextBox password;
  26. private System.Windows.Forms.TextBox username;
  27. private System.Windows.Forms.Button receive;
  28. private System.Windows.Forms.RichTextBox richTextBox1;
  29. private System.Windows.Forms.RichTextBox richTextBox2;
  30. /// <summary>
  31. /// 必需的设计器变量。
  32. /// </summary>
  33. private System.ComponentModel.Container components = null;
  34. public Form1()
  35. {
  36. //
  37. // Windows 窗体设计器支持所必需的
  38. //
  39. InitializeComponent();
  40. //
  41. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  42. //
  43. }
  44. /// <summary>
  45. /// 清理所有正在使用的资源。
  46. /// </summary>
  47. protected override void Dispose( bool disposing )
  48. {
  49. if( disposing )
  50. {
  51. if (components != null) 
  52. {
  53. components.Dispose();
  54. }
  55. }
  56. base.Dispose( disposing );
  57. }
  58. #region Windows 窗体设计器生成的代码
  59. /// <summary>
  60. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  61. /// 此方法的内容。
  62. /// </summary>
  63. private void InitializeComponent()
  64. {
  65. this.groupBox1 = new System.Windows.Forms.GroupBox();
  66. this.label1 = new System.Windows.Forms.Label();
  67. this.servername = new System.Windows.Forms.TextBox();
  68. this.password = new System.Windows.Forms.TextBox();
  69. this.username = new System.Windows.Forms.TextBox();
  70. this.label3 = new System.Windows.Forms.Label();
  71. this.label4 = new System.Windows.Forms.Label();
  72. this.receive = new System.Windows.Forms.Button();
  73. this.groupBox2 = new System.Windows.Forms.GroupBox();
  74. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  75. this.groupBox3 = new System.Windows.Forms.GroupBox();
  76. this.richTextBox2 = new System.Windows.Forms.RichTextBox();
  77. this.groupBox1.SuspendLayout();
  78. this.groupBox2.SuspendLayout();
  79. this.groupBox3.SuspendLayout();
  80. this.SuspendLayout();
  81. // 
  82. // groupBox1
  83. // 
  84. this.groupBox1.Controls.Add(this.label1);
  85. this.groupBox1.Controls.Add(this.servername);
  86. this.groupBox1.Controls.Add(this.password);
  87. this.groupBox1.Controls.Add(this.username);
  88. this.groupBox1.Controls.Add(this.label3);
  89. this.groupBox1.Controls.Add(this.label4);
  90. this.groupBox1.Controls.Add(this.receive);
  91. this.groupBox1.Location = new System.Drawing.Point(4, 4);
  92. this.groupBox1.Name = "groupBox1";
  93. this.groupBox1.Size = new System.Drawing.Size(338, 126);
  94. this.groupBox1.TabIndex = 0;
  95. this.groupBox1.TabStop = false;
  96. this.groupBox1.Text = "接收邮件设置:";
  97. // 
  98. // label1
  99. // 
  100. this.label1.Location = new System.Drawing.Point(22, 26);
  101. this.label1.Name = "label1";
  102. this.label1.Size = new System.Drawing.Size(64, 16);
  103. this.label1.TabIndex = 1;
  104. this.label1.Text = "服务器名:";
  105. // 
  106. // servername
  107. // 
  108. this.servername.Location = new System.Drawing.Point(86, 22);
  109. this.servername.Name = "servername";
  110. this.servername.Size = new System.Drawing.Size(172, 21);
  111. this.servername.TabIndex = 0;
  112. this.servername.Text = "pop.126.com";
  113. // 
  114. // password
  115. // 
  116. this.password.Location = new System.Drawing.Point(86, 88);
  117. this.password.Name = "password";
  118. this.password.PasswordChar = '*';
  119. this.password.Size = new System.Drawing.Size(172, 21);
  120. this.password.TabIndex = 0;
  121. this.password.Text = "19831022";
  122. // 
  123. // username
  124. // 
  125. this.username.Location = new System.Drawing.Point(86, 54);
  126. this.username.Name = "username";
  127. this.username.Size = new System.Drawing.Size(172, 21);
  128. this.username.TabIndex = 0;
  129. this.username.Text = "hrk11111";
  130. // 
  131. // label3
  132. // 
  133. this.label3.Location = new System.Drawing.Point(22, 58);
  134. this.label3.Name = "label3";
  135. this.label3.Size = new System.Drawing.Size(48, 14);
  136. this.label3.TabIndex = 1;
  137. this.label3.Text = "用户名:";
  138. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  139. // 
  140. // label4
  141. // 
  142. this.label4.Location = new System.Drawing.Point(22, 92);
  143. this.label4.Name = "label4";
  144. this.label4.Size = new System.Drawing.Size(40, 16);
  145. this.label4.TabIndex = 1;
  146. this.label4.Text = "密码:";
  147. this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  148. // 
  149. // receive
  150. // 
  151. this.receive.Location = new System.Drawing.Point(264, 86);
  152. this.receive.Name = "receive";
  153. this.receive.Size = new System.Drawing.Size(64, 23);
  154. this.receive.TabIndex = 4;
  155. this.receive.Text = "接收邮件";
  156. this.receive.Click += new System.EventHandler(this.receive_Click);
  157. // 
  158. // groupBox2
  159. // 
  160. this.groupBox2.Controls.Add(this.richTextBox1);
  161. this.groupBox2.Location = new System.Drawing.Point(4, 134);
  162. this.groupBox2.Name = "groupBox2";
  163. this.groupBox2.Size = new System.Drawing.Size(342, 128);
  164. this.groupBox2.TabIndex = 2;
  165. this.groupBox2.TabStop = false;
  166. this.groupBox2.Text = "邮件名称:";
  167. // 
  168. // richTextBox1
  169. // 
  170. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
  171. this.richTextBox1.Location = new System.Drawing.Point(3, 17);
  172. this.richTextBox1.Name = "richTextBox1";
  173. this.richTextBox1.Size = new System.Drawing.Size(336, 108);
  174. this.richTextBox1.TabIndex = 1;
  175. this.richTextBox1.Text = "";
  176. // 
  177. // groupBox3
  178. // 
  179. this.groupBox3.Controls.Add(this.richTextBox2);
  180. this.groupBox3.Location = new System.Drawing.Point(2, 266);
  181. this.groupBox3.Name = "groupBox3";
  182. this.groupBox3.Size = new System.Drawing.Size(342, 216);
  183. this.groupBox3.TabIndex = 3;
  184. this.groupBox3.TabStop = false;
  185. this.groupBox3.Text = "邮件内容:";
  186. // 
  187. // richTextBox2
  188. // 
  189. this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
  190. this.richTextBox2.Location = new System.Drawing.Point(3, 17);
  191. this.richTextBox2.Name = "richTextBox2";
  192. this.richTextBox2.Size = new System.Drawing.Size(336, 196);
  193. this.richTextBox2.TabIndex = 0;
  194. this.richTextBox2.Text = "";
  195. // 
  196. // Form1
  197. // 
  198. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  199. this.BackColor = System.Drawing.SystemColors.Control;
  200. this.ClientSize = new System.Drawing.Size(352, 486);
  201. this.Controls.Add(this.groupBox3);
  202. this.Controls.Add(this.groupBox2);
  203. this.Controls.Add(this.groupBox1);
  204. this.MaximizeBox = false;
  205. this.Name = "Form1";
  206. this.Text = "邮件接收端";
  207. this.groupBox1.ResumeLayout(false);
  208. this.groupBox2.ResumeLayout(false);
  209. this.groupBox3.ResumeLayout(false);
  210. this.ResumeLayout(false);
  211. }
  212. #endregion
  213. /// <summary>
  214. /// 应用程序的主入口点。
  215. /// </summary>
  216. [STAThread]
  217. static void Main() 
  218. {
  219. Application.Run(new Form1());
  220. }
  221. private void receive_Click(object sender, System.EventArgs e)
  222. {
  223. int mailnumber;
  224. TcpClient tcpClient = new TcpClient();
  225. try
  226. {
  227. //连接POP3服务器
  228. tcpClient.Connect(servername.Text,110);
  229. }
  230. catch
  231. {
  232. //显示错误信息
  233. MessageBox.Show ("无法连接到POP3服务器"+servername.Text+"的端口!");
  234. }
  235. NetworkStream netStream = tcpClient.GetStream ();
  236. if (netStream == null)
  237. {
  238. MessageBox.Show("无法得到答复!");
  239. }
  240. string returnMessage = ReadFromNetStream(ref netStream);
  241. checkForError(returnMessage);
  242. richTextBox1.AppendText("连接应答:"+returnMessage+"rn");
  243. //发送用户名信息
  244. richTextBox1.AppendText("C:USER "+this.username.Text+"rn");
  245. WriteToNetStream(ref netStream,"USER "+this.username.Text);
  246. returnMessage = ReadFromNetStream(ref netStream);
  247. checkForError(returnMessage);
  248. richTextBox1.AppendText("POP3SERVER:"+returnMessage+"rn");
  249. //发送用户密码信息
  250. richTextBox1.AppendText("C:PASSWORD:"+this.password.Text+"rn");
  251. WriteToNetStream(ref netStream,"PASS "+this.password .Text);
  252. returnMessage = ReadFromNetStream(ref netStream);
  253. checkForError(returnMessage);
  254. richTextBox1.AppendText("POP3SERVER:"+returnMessage+"rn");
  255. //检查邮件
  256. mailnumber=EmailCheck(ref netStream);
  257. richTextBox1.AppendText("邮件数:"+mailnumber+"rn");
  258. //接收邮件
  259. int i = 1;
  260. richTextBox1.AppendText("C:RETR "+i.ToString()+"rn");
  261. WriteToNetStream(ref netStream,"RETR "+i.ToString());
  262. returnMessage = ReadFromNetStream(ref netStream);
  263. checkForError(returnMessage);
  264. richTextBox1.AppendText("POP3SERVER:"+returnMessage+"rn");
  265. ReadMail(ref netStream,i);
  266. //断开与POP3服务器的连接
  267. richTextBox1.AppendText("C:QUIT rn");
  268. WriteToNetStream(ref netStream,"QUIT ");
  269. returnMessage = ReadFromNetStream(ref netStream);
  270. checkForError(returnMessage);
  271. richTextBox1.AppendText("POP3SERVER:"+returnMessage+"rn");
  272. }
  273. //邮件检查
  274. private int EmailCheck(ref NetworkStream NetStream)
  275. {
  276. richTextBox1.AppendText("C:STAT rn");
  277. WriteToNetStream(ref NetStream,"STAT");
  278. string returnMessage = ReadFromNetStream(ref NetStream);
  279. checkForError(returnMessage);
  280. richTextBox1.AppendText("POP3SERVER:"+returnMessage+"rn");
  281. string[] TotalStat = returnMessage.Split (new char[] {' '});
  282. int mailNumber = Int32.Parse (TotalStat[1]);
  283. return mailNumber;
  284. }
  285. //向POP3服务器写入命令
  286. private void WriteToNetStream(ref NetworkStream NetStream,string Command)
  287. {
  288. string stringToSend = Command + "rn";
  289. Byte[] arrayToSend = Encoding.ASCII.GetBytes(stringToSend.ToCharArray());
  290. NetStream.Write(arrayToSend,0,arrayToSend.Length );
  291. }
  292. //从POP3读取数据
  293. private string ReadFromNetStream(ref NetworkStream NetStream)
  294. {
  295. StringBuilder strReceived=new StringBuilder();
  296. StreamReader sr = new StreamReader (NetStream);
  297. String strLine = sr.ReadLine ();
  298. while(strLine == null || strLine.Length == 0)
  299. {
  300. strLine = sr.ReadLine ();
  301. }
  302. strReceived.Append(strLine);
  303. if (sr.Peek () != -1)
  304. {
  305. while((strLine=sr.ReadLine ()) != null)
  306. {
  307. strReceived.Append(strLine);
  308. }
  309. }
  310. return strReceived.ToString();
  311. }
  312. //检查流中是否有错误
  313. private void checkForError(String strMessage)
  314. {
  315. if (strMessage.IndexOf ("+OK") == -1)
  316. throw new Exception ("收到来自POP3服务器的错误信息:"+strMessage);
  317. }
  318.        //读邮件
  319. private void ReadMail(ref NetworkStream NetStream,int number)
  320. {
  321. int k = 0;
  322. bool check = false;
  323. byte[] bt = new byte [500000];
  324. while(!check)
  325. {
  326. k = NetStream.Read (bt,0,bt.Length );
  327. string read = System.Text .Encoding .UTF8.GetString (bt,0,k);
  328. int x = read.IndexOf ("rn.rn");
  329. if (x == -1)
  330. {
  331. check=true;
  332. }
  333. richTextBox2.AppendText (read);
  334. richTextBox1.AppendText("C:DELE "+number.ToString ()+"rn");
  335. WriteToNetStream(ref NetStream,"DELE "+number.ToString ());
  336. string returnMessage = ReadFromNetStream(ref NetStream);
  337. checkForError(returnMessage);
  338. richTextBox1.AppendText("POP3SERVER:"+returnMessage+"rn");
  339. }
  340. }
  341. }
  342. }