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

📄 form2.cs

📁 C#写的Email邮件客户端程序skyemail.
💻 CS
📖 第 1 页 / 共 2 页
字号:
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; 
using System.Text;  
using System.Runtime.InteropServices;  
using System.Security.Cryptography;
namespace SkyMail
{
	/// <summary>
	/// Form2 的摘要说明。
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ProgressBar progressBar1;
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		///
		private System.ComponentModel.Container components = null;
		public Form2()
		{
			//
			// 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.progressBar1 = new System.Windows.Forms.ProgressBar();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// progressBar1
			// 
			this.progressBar1.Location = new System.Drawing.Point(24, 24);
			this.progressBar1.Name = "progressBar1";
			this.progressBar1.Size = new System.Drawing.Size(220, 15);
			this.progressBar1.TabIndex = 0;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(144, 15);
			this.label1.TabIndex = 1;
			// 
			// Form2
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(280, 45);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label1,
																		  this.progressBar1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form2";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "收邮件";
			this.TransparencyKey = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(0)), ((System.Byte)(0)));
			this.Load += new System.EventHandler(this.Form2_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void Form2_Load(object sender, System.EventArgs e)
		{
			//POP3client dd =new POP3client(); 
			//dd.EmailCheck();

		}

	}
	
	public class POP3client
	{
		public enum connect_state {disc,AUTHORIZATION,TRANSACTION,UPDATE};
		public string Dispname;//名字
		public string Dispemail;//邮件地址
		public string smtpname;//SMTP服务器地址
		public string smtpport;
		public string pop3port;
		public string pop3name;//POP3服务器地址
		public string username;//邮件账号
		public string userpwd;//账号密码
		public bool emailbak;//该服务器是否保留备份
		public int timeout;//该服务器超时设置
		public string[] OtherPOP3;//其它的POP3服务器配置
		public bool error;
		public connect_state state=connect_state.disc ;
		private TcpClient Server;
		public NetworkStream NetStrm;
		public StreamReader  RdStrm;
		private string Data;
		private byte[] szData;
		private string CRLF = "\r\n";    
        #region Utility Methods, some public, some private
		public static Byte[] ConvertStringToByteArray(String s)
		{
			return (new UnicodeEncoding()).GetBytes(s);
		}
		
		public void EmailCheck() ///检查邮件函数
		{
			FileInfo ad=new FileInfo("skymail.wg");
			if (ad.Exists)
			{
				FileStream fin = new FileStream("skymail.wg", FileMode.Open, FileAccess.Read);
				string password="wanwengu";
				UnicodeEncoding UE = new UnicodeEncoding();
				SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();
				byte[] key = UE.GetBytes(password);
				CryptoStream cryptostreamDecr = new CryptoStream(fin,rijn.CreateDecryptor(key,key),CryptoStreamMode.Read);
				string con=(new StreamReader(cryptostreamDecr, new UnicodeEncoding())).ReadToEnd();
				cryptostreamDecr.Close(); 
				fin.Close();
				string[] conn=con.Split(new char[]{'\n'});
				for(int j=0;j<conn.Length;j++ )
				{
					string szTemp=conn[j];	
					int hint=szTemp.IndexOf(":"); ///将单行字符串以冒号为分隔分成两个字符串
					if(hint!=-1)
					{
						string headcaption;
						string headvalue;
						headcaption=szTemp.Substring(0,hint);
						headvalue=szTemp.Substring(hint+1,szTemp.Length-hint-1).Trim() ;
						switch (headcaption) 
						{
							case "Dispname":
								Dispname=headvalue;  
								break;
							case "Dispemail":
								Dispemail =headvalue;  
								break;
							case "smtpname":
								smtpname =headvalue;  
								break;
							case "smtpport":
								smtpport=headvalue;  
								break;
							case "pop3name":
								pop3name=headvalue;  
								break;
							case "pop3port":
								pop3port=headvalue; 
								break;
							case "username":
								username=headvalue; 
								break;
							case "userpwd":
								userpwd=headvalue; 
								break;
							case "emailbak":
								if(headvalue.IndexOf("True")!=-1)
								{
									emailbak=true; 
								}
								else
								{
									emailbak=false; 
								};  
								break;
							case "timeout":
								timeout=1;
							switch(headvalue)
							{
								case "2":
									timeout=2;
									break;
								case "3":
									timeout=3;
									break;
								case "4":
									timeout=4;
									break;
								case "5":
									timeout=5;
									break;
							}
								break;
						}
					}
				}

			}

			connect();
			USER();
			PASS(); 
			string returnMsg=this.STAT(); 
			//将总邮件数和邮件大小分离 
			string[] TotalStat= returnMsg.Split(new char[] {' '}); 
			int count =Int32.Parse(TotalStat[1]); 
			int totalSize=Int32.Parse(TotalStat[2]); 
			//调用精灵,告知用户新邮件数 
			///Form2 agent= new Form2(count);	
			//循环
			FileInfo dd=new FileInfo("main.wg");
			for(int j=1;j<count+1;j++)
			{
				String MessageText =this.RETR(j);
				Encoding tmpEncoding = Encoding.ASCII;
				
				bool BExit=false;
				if (dd.Exists)
				{
				
					string Msgid;
					int id1=MessageText.IndexOf("Message-Id");
					int id2=MessageText.IndexOf("Message-ID");
					if(id1==-1) id1=id2;
					
					id2=MessageText.IndexOf("\n",id1);
					id1=MessageText.IndexOf(":",id1);
					Msgid=MessageText.Substring(id1+1,id2-id1-1).Trim(); 
					StreamReader sr=new StreamReader(@"main.wg"); 
					int i=0;
					bool error=false;
					while(!error) 
					{	
						string szTemp="";
						while(szTemp!=".")
						{
							szTemp = sr.ReadLine();
							if( szTemp==null) 
							{szTemp="";error=true;break; }
							else
							{
								if (szTemp.IndexOf(Msgid)!=-1)
								{
									BExit=true;
									break;
								}
							}
						}
						i=i+1;
					}
					sr.Close(); 
					if(BExit!=true)//判断如果不存在重复Messageid的邮件则进行保存
					{
						StreamWriter sw =new StreamWriter(@"main.wg",true,Encoding.GetEncoding("gb2312")) ;
						sw.Write(MessageText); 
						sw.Close(); 
					}
				}
				else
				{
					byte[] bytes=Encoding.Default.GetBytes(MessageText);///字符转换成字节
					FileStream  emailstr =File.Create("main.wg");//打开文件
					for(int i=0;i<bytes.Length;i++)
					{
						emailstr.WriteByte(bytes[i]);
					}
					emailstr.Close();
				}
				//判断帐号配置中是否在服务器上保留邮件
				StreamReader mb=new StreamReader(@"skymail.wg"); 
				bool mb1=false;
				while(!mb1) 
				{	
					string szTemp="";
					while(szTemp!=".")
					{
						szTemp = mb.ReadLine();
						if( szTemp==null) 
						{szTemp="";mb1=true;break; }
						else
						{
							if (szTemp.IndexOf("mailbak")!=-1)
							{
								int f=szTemp.IndexOf(":")+1;
								if(szTemp.Substring(f,szTemp.Length-f).IndexOf("True")==-1)
								{																															   
									this.DELE(j); 
								}
								break;
							}
						}
					}
				}
				mb.Close(); 
					
			}
			this.QUIT();
		}

		public string connect (string pop_server)
		{
			pop3name=pop_server;    //put the specified server into the pop property
			return(connect()); //call the connect method
		}
		public string connect()
		{
			//create server with port 110
			Server = new TcpClient(pop3name,110);                                
			try

⌨️ 快捷键说明

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