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

📄 form3.cs

📁 C#写的Email邮件客户端程序skyemail.
💻 CS
📖 第 1 页 / 共 4 页
字号:
			this.menuItem18.Index = 5;
			this.menuItem18.Shortcut = System.Windows.Forms.Shortcut.F4;
			this.menuItem18.Text = "退出";
			this.menuItem18.Click += new System.EventHandler(this.menuItem18_Click);
			// 
			// notifyIcon1
			// 
			this.notifyIcon1.ContextMenu = this.contextMenu2;
			this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
			this.notifyIcon1.Text = "邮件检查";
			this.notifyIcon1.Visible = true;
			// 
			// listView2
			// 
			this.listView2.Alignment = System.Windows.Forms.ListViewAlignment.Left;
			this.listView2.ContextMenu = this.contextMenu3;
			this.listView2.HoverSelection = true;
			this.listView2.LabelEdit = true;
			this.listView2.Location = new System.Drawing.Point(688, 216);
			this.listView2.Name = "listView2";
			this.listView2.Scrollable = false;
			this.listView2.Size = new System.Drawing.Size(56, 256);
			this.listView2.TabIndex = 7;
			this.listView2.DoubleClick += new System.EventHandler(this.listView2_DoubleClick);
			// 
			// contextMenu3
			// 
			this.contextMenu3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.打开附件,
																						 this.menuItem19,
																						 this.menuItem20});
			// 
			// 打开附件
			// 
			this.打开附件.Index = 0;
			this.打开附件.Text = "打开附件";
			this.打开附件.Click += new System.EventHandler(this.打开附件_Click);
			// 
			// menuItem19
			// 
			this.menuItem19.Index = 1;
			this.menuItem19.Text = "另存附件";
			this.menuItem19.Click += new System.EventHandler(this.menuItem19_Click);
			// 
			// menuItem20
			// 
			this.menuItem20.Index = 2;
			this.menuItem20.Text = "附件属性";
			// 
			// saveFileDialog1
			// 
			this.saveFileDialog1.FileName = "doc1";
			// 
			// timer1
			// 
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form3
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(768, 493);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.listView2,
																		  this.toolBar1,
																		  this.listView1,
																		  this.splitter1,
																		  this.textBox1,
																		  this.treeView1});
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "Form3";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "SkyMail";
			this.Load += new System.EventHandler(this.Form3_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void Form3_Load(object sender, System.EventArgs e)
		{
			///<summary>
			///首先判断是否当前是否存在合法用户
			///如果没有则提示新建用户
			///有存在用户则提示使用哪个用户登陆或者新建用户
			///暂时不实现此功能
			///<summary>
				Getdir();
				treeView1.ExpandAll();//将所有的收件箱展开
				listView1.FullRowSelect=true;//允许在邮件列表选择时选中整行
				textBox1.Width=(int)(listView1.Width*0.9); //定义正文宽度
				resize();
		}
		private void Getdir()
		{
			treeView1.Nodes.Clear();
			treeView1.Nodes.Add("本地文件夹");  
			TreeNode d1=new TreeNode("收件箱",1,1); 
			treeView1.Nodes[0].Nodes.Add(d1);  
			TreeNode d2=new TreeNode("发件箱",2,2); 
			treeView1.Nodes[0].Nodes.Add(d2);  
			TreeNode d3=new TreeNode("已发送邮件",3,3); 
			treeView1.Nodes[0].Nodes.Add(d3);  
			TreeNode d4=new TreeNode("垃圾箱",4,4); 
			treeView1.Nodes[0].Nodes.Add(d4);  

			FileInfo s=new FileInfo("user.dr");
			if(s.Exists)
			{
				StreamReader sr=new StreamReader(@"user.dr",Encoding.GetEncoding("GB2312"));
				string con=sr.ReadToEnd();
				sr.Close();
				if (con.Trim().Length >0)
				{
					string[] fdir=con.Trim().Split(new char[]{'\n'}); 
					for(int h=0;h<fdir.Length;h++ )
					{
						string[] fcon=fdir[h].Split((char)92);
						string parstr=fcon[0].Trim();
						string childstr=fcon[1].Trim();
						//			//循环判断选中哪个节点
						for(int i=0;i<treeView1.Nodes.Count ;i++)
						{
							for(int j=0;j<treeView1.Nodes[i].Nodes.Count;j++)
							{
								if(treeView1.Nodes[i].Nodes[j].Text.IndexOf(parstr)!=-1)
								{
									int dtimage=treeView1.Nodes[i].Nodes[j].SelectedImageIndex; 
									TreeNode dt=new TreeNode(childstr,dtimage,dtimage);
									treeView1.Nodes[i].Nodes[j].Nodes.Add(dt); 
									break;
								}

							}
							if(treeView1.Nodes[i].Text.IndexOf(parstr)!=-1)
							{
								TreeNode dt=new TreeNode(childstr);
								treeView1.Nodes[i].Nodes.Add(dt); 
								break;
							}
						}


					}
				}
			}
			treeView1.ExpandAll();
		}
		private void resize()
		{	
			listView1.Width =(int)(this.Width*0.75);
			listView1.Columns[0].Width=(int)(listView1.Width*0.05); 
			listView1.Columns[1].Width=(int)(listView1.Width*0.2); 
			listView1.Columns[2].Width=(int)(listView1.Width*0.43); 
			listView1.Columns[3].Width=(int)(listView1.Width*0.25); 
			listView1.Columns[4].Width=listView1.Width-listView1.Columns[0].Width-listView1.Columns[1].Width-listView1.Columns[2].Width-listView1.Columns[3].Width;
			
		}
	
		public void DispAll(string folder)
		{
			filename=folder;
			listView1.Items.Clear(); 
			FileInfo dd=new FileInfo(filename);
			string temp="";
			
			if (dd.Exists && dd.Length>0)
			{
				StreamReader sw =new StreamReader(@filename,Encoding.GetEncoding("gb2312")) ;
				bool error =false;
				
				int i=0;
				while(!error) 
				{	
					string szTemp="";
					temp="";
					while(szTemp!=".")
					{
						szTemp = sw.ReadLine();
						if( szTemp==null) 
						{szTemp="";error=true;break; }
						else
						{temp += szTemp+CRLF;}
					}
					if( temp!="") {addlist(temp,i); }//此处可以不需要取出所有的邮件内容,如果已经换行的话就可以判断伪邮件头的内容
					i=i+1;
				}
				sw.Close(); 
			}
//			else
//			{
//			//	MessageBox.Show("您帐号的邮件数据可能丢失!");
//			}
			
		}

		
		private string addlist(string con,int id)
		{
			
			string msgcon=con;
			int msgid=id;
			string msgconhead;
			string msgconbody;
			int slength=msgcon.IndexOf(CRLF+CRLF);///两个换行分隔邮件头和内容
			Msubject = "";  //邮件主题
			Mdate = "";    //日期
			Mfrom = "";    //发件人
			Mto= "";       //收件人
			Msize = "";    //邮件大小
			Mmsgid = "";   //邮件ID
			Mtype = "";    //编码类型
			Mxmailer = ""; //邮件服务器类型
			Mstatus = "";  //邮件状态
			Mcon= "";      //邮件正文
			Matt = "";
			msgconhead=msgcon.Substring(0,slength);
			msgconbody=msgcon.Substring(slength,msgcon.Length-slength);
			
			string[] heads=msgconhead.Split(new char[] {'\n'});
			int j=0;
			for(j=0;j<heads.Length;j++)
			{
				if (heads[j]!="")
				{
					int hint=heads[j].IndexOf(":"); ///将单行字符串以冒号为分隔分成两个字符串
					if(hint!=-1)
					{
						string headcaption;
						string headvalue;
						headcaption=heads[j].Substring(0,hint);
						headvalue=heads[j].Substring(hint+1,heads[j].Length-hint-2);
							
						switch (headcaption) 
						{
							case "From":
								//判断地址是否署名,署名则显示名字,否则显示邮件地址
										
								int Ftmp=headvalue.IndexOf((char)34,0);
								int end=headvalue.IndexOf((char)34,Ftmp+1);
								if(Ftmp!=-1)
								{
									Mfrom=headvalue.Substring(Ftmp+1,end-2);  
								}
								else{Mfrom=headvalue;}
								break;
							case "To":
								Mto=headvalue;  
								break;
							case "Date":
								Mdate=headvalue;  
								break;
							case "Subject":
								Msubject=headvalue; 
								//主题可能需要解码
								int tempsize=headvalue.Length +msgconbody.Length;
								Msize=tempsize.ToString(); 
								break;
							case "Message-Id":///不同邮件系统存在大小写区别
								Mmsgid=headvalue;  
								break;
							case "Message-ID":
								Mmsgid=headvalue;  
								break;
							case "Content-Type":///判断附件的有无
										
								break;
							case "Status":
								if(headvalue.IndexOf("R")!=-1){Mstatus="旧"; }
								else{Mstatus="新";}
 
								break;
							case "X-Mailer":
								Mxmailer=headvalue;  
								break;
						}
					}	

				}
			}
			if (Mstatus=="")
				Mstatus="旧";
			int  aa=Mdate.LastIndexOf(":");
			string myStr=Mdate.Substring(0,aa+3);
			aa=Mdate.IndexOf(",");
			if(aa!=-1)
				myStr=myStr.Substring(aa+1,myStr.Length-aa-1).Trim();
			System.DateTime  myDate=System.Convert.ToDateTime(myStr);
			myStr=myDate.ToString(); 
			int d=Msubject.IndexOf("?");
			if(d!=-1)//主题经过编码
			{
				string[] m1=Msubject.Split(new char[] {'?'});
				Msubject=m1[3];
				string type=m1[2];
				if(type.IndexOf("B")!=-1)
					Msubject=this.Base64Decode(Msubject); 
				if(type.IndexOf("Q")!=-1)
					Msubject=this.QDecode(Msubject); 
			}
			


 
			string[] items = new string[5];
			items[0] = Mstatus ;
			items[1] = Mfrom;
			items[2] = Msubject ;
			items[3] = myStr ;
			items[4] = Msize ;
			listView1.Items.Add(new ListViewItem(items,msgid));
			if (Mstatus.IndexOf("旧")==-1 )
			{
				listView1.Items[msgid].Font=new Font(listView1.Items[msgid].Font,listView1.Items[msgid].Font.Style | FontStyle.Bold);
			}
			listView1.Items[msgid].Tag=(msgid); 
			return("ok");
		}

		public void Bodydisp(int strpara)///根据传递的参数显示该邮件的正文和附件
		{
			FileInfo ftemp=new FileInfo(filename);
			int Iid=strpara;
			string temp="";
			int i=0;
			listView2.Items.Clear();//将附件框的内容清除 
			if (ftemp.Exists)
			{
				StreamReader sw =new StreamReader(@filename,Encoding.GetEncoding("gb2312"));
				bool error =false;
				while(!error) 
				{	
					string szTemp="";
					szTemp = sw.ReadLine();
					while(szTemp!=".")
					{
						if( szTemp==null) 
						{szTemp="";error=true;break; }
						else
						{
							if(i==Iid)
							{
								temp += szTemp+"\r\n";
							}
						
						}
						szTemp = sw.ReadLine();
					}
					i=i+1;
				}
				/////////////////////
				sw.Close(); //关闭文件资源
				string msgconhead;
				string msgconbody;
				int slength=temp.IndexOf(CRLF+CRLF);///两个换行分隔邮件头和内容
				Mtype = "";    //编码类型
				Matt = "";
				msgconhead=temp.Substring(0,slength).Trim();
				msgconbody=temp.Substring(slength,temp.Length-slength).Trim();
				string[] heads=msgconhead.Split(new char[] {'\n'});
				int j=0;
				bool plain=false;
				bool mixed=false;
				bool alter=false;
				string list="";
				bool att=true;

				for(j=0;j<heads.Length;j++)
				{
					if (heads[j]!="")
					{
						int hint=heads[j].IndexOf(":"); ///将单行字符串以冒号为分隔分成两个字符串
						if(hint!=-1)
						{
							string headcaption;
							string headvalue;
							headcaption=heads[j].Substring(0,hint);
							headvalue=heads[j].Substring(hint+1,heads[j].Length-hint-1);
							
							if(headcaption.IndexOf("Content-Type")!=-1 ||headcaption.IndexOf("Content-type")!=-1)
							{
								//MIME格式邮件体
								headvalue=headvalue.Substring(0,headvalue.IndexOf(";")).Trim(); 
								att=false;
								if(headvalue.IndexOf("multipart/mixed")!=-1) 
								{
									mixed=true;
									att=false;
								}
								if(headvalue.IndexOf("text/plain")!=-1) 
								{
									plain=true;
									att=false;
								}
								if(headvalue.IndexOf("multipart/alternative")!=-1)
								{
									alter=true;
									att=false;
								}
								
							}
							if(headcaption.IndexOf("Content-Transfer-Encoding")!=-1)
							{
								Mtype=headvalue;
							}
							//取出多内容邮件的分隔符
							
							
						}
							//不是使用冒号分隔的行包含boundary是邮件体的分隔行
							
						else
						{
							if(heads[j].IndexOf("boundary")!=-1)
							{
								list=heads[j];
								string[]  l1=list.Split(new char[] {(char)34});  
								list=l1[1];
							}
						}
					}
				}
				
				if(att)//不是Mime格式邮件,直接显示正文内容
				{
					listView2.Visible=false;
					textBox1.Width=this.listView1.Width;   
					textBox1.Text=msgconbody; 
				}
				if(plain)
				{
					listView2.Visible=false;
					textBox1.Width=this.listView1.Width;   
					switch(Mtype)//没有附件但是正文编码处理
					{
						case "base64":
							textBox1.Text=Base64Decode(msgconbody);
							break;
						case "quoted-printable":
							textBox1.Text=QDecode(msgconbody);
							break;
						case "8bit":
							break;
					}

				}
				if(alter)//多内容邮件一般包含两种格式:text/plain,text/html目前只显示text/plain
				{
					//list为邮件体的分隔符
					listView2.Visible=false;
					textBox1.Width=this.listView1.Width; 
					int q1=msgconbody.IndexOf(list);
					int q2=msgconbody.IndexOf(list,q1+list.Length+1);  
					string msgconbody1=msgconbody.Substring(q1+list.Length,q2-q1-list.Length-2).Trim();    
					int q3=msgconbody1.IndexOf(CRLF+CRLF);
					string msgconbody2=msgconbody1.Substring(q3,msgconbody1.Length-q3).Trim();
					msgconbody=msgconbody1.Substring(0,q3).Trim();
					string[] head=msgconbody.Split(new char[] {'\n'});
					if(head.Length==3)
					{
						q1=head[0].IndexOf(":");
						string sty=head[0].Substring(q1+1,head[0].Length-q1-2).Trim();  
						q1=head[1].IndexOf("=");
						string charsty=head[1].Substring(q1+2,head[1].Length-q1-3).Trim();  
						q1=head[2].IndexOf(":");
						Mtype=head[2].Substring(q1+1,head[2].Length-q1-1).Trim();  
					}
					else
					{
						q1=head[0].IndexOf(":");
						q2=head[0].IndexOf(";");
						string sty=head[0].Substring(q1+1,q2-q1-1).Trim();  //媒体类型
						q1=head[0].IndexOf("=");
						string charsty=head[0].Substring(q1+1,head[0].Length-q1-1).Trim(); //获取的是字符集 
						q1=head[1].IndexOf(":");
						Mtype=head[1].Substring(q1+1,head[1].Length-q1-1).Trim();  //获取编码的方式
					}
					switch(Mtype)
					{
						case "base64":
							textBox1.Text=Base64Decode(msgconbody2);
							break;
						case "quoted-printable":
							textBox1.Text=QDecode(msgconbody);
							break;
						case "8bit":
							break;
					}

				}


				///////////////////////////////
				if(mixed)//多内容邮件并且包含附件格式
				{
					//list为邮件体的分隔符
					/*-----------------------------------------------*/
					textBox1.Width=(int)(listView1.Width*0.9); 
					textBox1.Text="" ;
					listView2.Visible=true;
					//显示附件框
					/*-----------------------------------------------*/
					int q1;//查找起始位置
					int q2;//截取的截至位置
					bool end=true;//循环结束的标识
//					int count=0;
					while(end)//通过循环将邮件各部分内容分离出来,通过查找媒体类型确定是否是正文或者是附件,目前都不支持Html邮件
					{
						q1=msgconbody.IndexOf(list);

⌨️ 快捷键说明

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