Form1.cs
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:12k
源码类别:

Email客户端

开发平台:

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