Form1.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:12k
源码类别:
Email客户端
开发平台:
Visual C++
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Data;
- using System.Net ;
- using System.IO;
- using System.Net.Sockets ;
- using System.Text ;
- //Download by http://www.codefans.net
- namespace MailPOP3
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private System.Windows.Forms.GroupBox groupBox1;
- 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.GroupBox groupBox2;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.TextBox servername;
- private System.Windows.Forms.TextBox mailport;
- private System.Windows.Forms.TextBox password;
- private System.Windows.Forms.TextBox username;
- private System.Windows.Forms.Button receive;
- private System.Windows.Forms.RichTextBox richTextBox1;
- private System.Windows.Forms.RichTextBox richTextBox2;
- /// <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.groupBox1 = new System.Windows.Forms.GroupBox();
- this.label1 = new System.Windows.Forms.Label();
- this.servername = new System.Windows.Forms.TextBox();
- this.mailport = new System.Windows.Forms.TextBox();
- this.password = new System.Windows.Forms.TextBox();
- this.username = new System.Windows.Forms.TextBox();
- this.label2 = new System.Windows.Forms.Label();
- this.label3 = new System.Windows.Forms.Label();
- this.label4 = new System.Windows.Forms.Label();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.groupBox3 = new System.Windows.Forms.GroupBox();
- this.receive = new System.Windows.Forms.Button();
- this.richTextBox1 = new System.Windows.Forms.RichTextBox();
- this.richTextBox2 = new System.Windows.Forms.RichTextBox();
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Controls.Add(this.servername);
- this.groupBox1.Controls.Add(this.mailport);
- this.groupBox1.Controls.Add(this.password);
- this.groupBox1.Controls.Add(this.username);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.label3);
- this.groupBox1.Controls.Add(this.label4);
- this.groupBox1.Location = new System.Drawing.Point(4, 4);
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(490, 82);
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "接收邮件服务器设置";
- //
- // label1
- //
- this.label1.Location = new System.Drawing.Point(10, 26);
- this.label1.Name = "label1";
- this.label1.Size = new System.Drawing.Size(64, 16);
- this.label1.TabIndex = 1;
- this.label1.Text = "服务器名:";
- //
- // servername
- //
- this.servername.Location = new System.Drawing.Point(76, 22);
- this.servername.Name = "servername";
- this.servername.Size = new System.Drawing.Size(174, 21);
- this.servername.TabIndex = 0;
- this.servername.Text = "";
- //
- // mailport
- //
- this.mailport.Location = new System.Drawing.Point(76, 52);
- this.mailport.Name = "mailport";
- this.mailport.Size = new System.Drawing.Size(174, 21);
- this.mailport.TabIndex = 0;
- this.mailport.Text = "";
- //
- // password
- //
- this.password.Location = new System.Drawing.Point(306, 52);
- this.password.Name = "password";
- this.password.Size = new System.Drawing.Size(174, 21);
- this.password.TabIndex = 0;
- this.password.Text = "";
- //
- // username
- //
- this.username.Location = new System.Drawing.Point(306, 22);
- this.username.Name = "username";
- this.username.Size = new System.Drawing.Size(174, 21);
- this.username.TabIndex = 0;
- this.username.Text = "";
- //
- // label2
- //
- this.label2.Location = new System.Drawing.Point(38, 56);
- this.label2.Name = "label2";
- this.label2.Size = new System.Drawing.Size(36, 14);
- this.label2.TabIndex = 1;
- this.label2.Text = "端口:";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(260, 26);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(48, 14);
- this.label3.TabIndex = 1;
- this.label3.Text = "用户名:";
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(272, 56);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(36, 16);
- this.label4.TabIndex = 1;
- this.label4.Text = "密码:";
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.richTextBox1);
- this.groupBox2.Location = new System.Drawing.Point(4, 90);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(562, 100);
- this.groupBox2.TabIndex = 2;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "邮件名称";
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.richTextBox2);
- this.groupBox3.Location = new System.Drawing.Point(2, 194);
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.Size = new System.Drawing.Size(564, 224);
- this.groupBox3.TabIndex = 3;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "邮件内容";
- //
- // receive
- //
- this.receive.Location = new System.Drawing.Point(498, 30);
- this.receive.Name = "receive";
- this.receive.Size = new System.Drawing.Size(64, 23);
- this.receive.TabIndex = 4;
- this.receive.Text = "接收邮件";
- this.receive.Click += new System.EventHandler(this.receive_Click);
- //
- // richTextBox1
- //
- this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.richTextBox1.Location = new System.Drawing.Point(3, 17);
- this.richTextBox1.Name = "richTextBox1";
- this.richTextBox1.Size = new System.Drawing.Size(556, 80);
- this.richTextBox1.TabIndex = 1;
- this.richTextBox1.Text = "";
- //
- // richTextBox2
- //
- this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
- this.richTextBox2.Location = new System.Drawing.Point(3, 17);
- this.richTextBox2.Name = "richTextBox2";
- this.richTextBox2.Size = new System.Drawing.Size(558, 204);
- this.richTextBox2.TabIndex = 0;
- this.richTextBox2.Text = "";
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(568, 424);
- this.Controls.Add(this.receive);
- this.Controls.Add(this.groupBox3);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.groupBox1);
- this.MaximizeBox = false;
- this.Name = "Form1";
- this.Text = "邮件接收";
- this.groupBox1.ResumeLayout(false);
- this.groupBox2.ResumeLayout(false);
- this.groupBox3.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- private void receive_Click(object sender, System.EventArgs e)
- {
- int mailnumber;
- //连接服务器
- TcpClient tcpClient=new TcpClient();
- try
- {
- //连接POP3服务器
- tcpClient.Connect(servername.Text,Int32.Parse (mailport.Text));
- }
- catch
- {
- //显示错误信息
- MessageBox.Show ("无法连接到POP3服务器"+servername.Text+"的"+mailport.Text+"端口!");
- }
- //从POP3邮件服务器取得回复
- NetworkStream netStream=tcpClient.GetStream ();
- if (netStream==null)
- {
- throw new Exception ("无法取得回复");
- }
- string returnMsg=ReadFromNetStream(ref netStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("连接应答:"+returnMsg+"rn");
- //发送用户名信息
- richTextBox1.AppendText("C:USER "+this.username.Text+"rn");
- WriteToNetStream(ref netStream,"USER "+this.username.Text);
- returnMsg=ReadFromNetStream(ref netStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("POP3SERVER:"+returnMsg+"rn");
- //发送用户密码信息
- richTextBox1.AppendText("C:PASSWORD "+this.password.Text+"rn");
- WriteToNetStream(ref netStream,"PASS "+this.password .Text);
- returnMsg=ReadFromNetStream(ref netStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("POP3SERVER:"+returnMsg+"rn");
- //检查邮件
- mailnumber=EmailCheck(ref netStream);
- richTextBox1.AppendText("邮件数:"+mailnumber+" rn");
- //接收邮件
- for (int i=1;i<=mailnumber;i++)
- {
- richTextBox1.AppendText("C:RETR "+i.ToString() +" rn");
- WriteToNetStream(ref netStream,"RETR "+i.ToString());
- returnMsg=ReadFromNetStream(ref netStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("POP3SERVER:"+returnMsg+"rn");
- ReadMail(ref netStream,i);
- }
- //断开与POP3服务器的连接
- richTextBox1.AppendText("C:QUIT rn");
- WriteToNetStream(ref netStream,"QUIT ");
- returnMsg=ReadFromNetStream(ref netStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("POP3SERVER:"+returnMsg+"rn");
- }
- //邮件检查
- private int EmailCheck(ref NetworkStream NetStream)
- {
- richTextBox1.AppendText("C:STAT rn");
- WriteToNetStream(ref NetStream,"STAT");
- string returnMsg=ReadFromNetStream(ref NetStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("POP3SERVER:"+returnMsg+"rn");
- string[] TotalStat=returnMsg.Split (new char[] {' '});
- int mailNumber=Int32.Parse (TotalStat[1]);
- return mailNumber;
- }
- //向POP3服务器写入命令
- private void WriteToNetStream(ref NetworkStream NetStream,string Command)
- {
- string stringToSend=Command+"rn";
- Byte[] arrayToSend=Encoding.ASCII.GetBytes(stringToSend.ToCharArray());
- NetStream.Write(arrayToSend,0,arrayToSend.Length );
- }
- //从POP3读取数据
- private string ReadFromNetStream(ref NetworkStream NetStream)
- {
- StringBuilder strReceived=new StringBuilder();
- StreamReader sr=new StreamReader (NetStream);
- String strLine=sr.ReadLine ();
- while(strLine==null || strLine.Length ==0)
- {
- strLine=sr.ReadLine ();
- }
- strReceived.Append(strLine);
- if (sr.Peek ()!=-1)
- {
- while((strLine=sr.ReadLine ())!=null)
- {
- strReceived.Append(strLine);
- }
- }
- return strReceived.ToString();
- }
- //检查流中是否含有错误
- private void checkForError(String strMessage)
- {
- if (strMessage.IndexOf ("+OK")==-1)
- throw new Exception ("收到来自POP3服务器的错误信息:"+strMessage);
- }
- //读邮件
- private void ReadMail(ref NetworkStream NetStream,int number)
- {
- int k=0;
- bool check=false;
- byte[] bb=new byte [6400];
- while(!check)
- {
- k=NetStream.Read (bb,0,bb.Length );
- string read=System.Text .Encoding .UTF8.GetString (bb,0,k);
- int x=read.IndexOf ("rn.rn");
- if (x!=-1)
- {
- check=true;
- }
- richTextBox2.AppendText (read);
- richTextBox1.AppendText("C:DELE "+number.ToString ()+"rn");
- WriteToNetStream(ref NetStream,"DELE "+number.ToString ());
- string returnMsg=ReadFromNetStream(ref NetStream);
- checkForError(returnMsg);
- richTextBox1.AppendText("POP3SERVER:"+returnMsg+"rn");
- }
- }
- }
- }