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

📄 form4.cs

📁 C#写的Email邮件客户端程序skyemail.
💻 CS
📖 第 1 页 / 共 3 页
字号:
					byte[] fbinaryData;
					fbinaryData=Encoding.Default.GetBytes(fnstr);
                    ftemp=Convert.ToBase64String(fbinaryData, 0,	fbinaryData.Length);
					fnstr="=?GB2312?B?"+ ftemp+"?=";
					mData=mData + "------=_NextPart_000_000A_01BF9F1A"+ CRLF;
					mData=mData + "Content-Type: Application/octet-stream name="+ (char)34+fnstr + (char)34+ CRLF;
					mData=mData + "Content-Transfer-Encoding: base64"+ CRLF;
					mData=mData + "Content-Disposition: attachment; fileName="+ (char)34+fnstr + (char)34 + CRLF+CRLF;
					mData=mData + attstr+"==" + CRLF;
				}
				mData=mData + "------=_NextPart_000_000A_01BF9F1A--"+ CRLF;
			}
			mData=mData + "."+ CRLF;

			return(mData);
		}
		private string base64Encode(string fnstr)
		{
			System.IO.FileStream inFile;    
			byte[] binaryData;
//			try 
//			{
				inFile = new System.IO.FileStream(fnstr,System.IO.FileMode.Open,System.IO.FileAccess.Read);
				binaryData = new Byte[inFile.Length];
				long bytesRead = inFile.Read(binaryData, 0,(int)inFile.Length);
				inFile.Close();
//			}
//			catch(System.Exception exp) 
//			{
//				// Error creating stream or reading from it.
//				MessageBox.Show ("附件读取出错!");	
//				return("err");
//			}

			// Convert the binary input into Base64 UUEncoded output.
			string base64String;
			try 
			{
				base64String = System.Convert.ToBase64String(binaryData, 0,	binaryData.Length);
				return base64String;
			}
			catch (System.ArgumentNullException) 
			{
				MessageBox.Show ("附件大小好像为空!");	
				return("err");
			}
		
		}
		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			switch (toolBar1.Buttons.IndexOf(e.Button))
			{
				case 0 :
					///调用邮件发送过程
					int s=textBox1.Text.IndexOf("@");
					int s1=textBox1.Text.IndexOf(".");
					if(s==-1||s1==-1)
					{
						MessageBox.Show("您输入的不是一个标准的邮件地址");
						break;
					}
					if (textBox1.Text!="")
					{
						
						Sendmail sw=new Sendmail();
						sw.connect(smtpserver); 
						//sw.HELO("wanwenguo" );
						if(smtplogin.IndexOf("True")!=-1)//需要Smtp验证
						{
							sw.EHLO(smtpserver);
							sw.AUTH();
							sw.USER(user);
							sw.PASSWORD(pwd); 
						}
						else
						{
							sw.HELO(smtpserver);
						}
						sw.FROM(emailaddress); 
						//需要根据真实发件人调整调整收件人地址
						string Rto="";
						
						if(textBox1.Text!="")
						{
								Rto=textBox1.Text;
								string[] d=Rto.Split(new char[] {','});
							if(d.Length==1)
							{
								sw.RCPT(Rto); 
							}
							else
							{
								for(int i=0;i<d.Length;i++ )
								{
									sw.RCPT(d[i]); 
								}
							}

						}
						if(textBox2.Text!="")
						{
							Rto=textBox2.Text;
							string[] d=Rto.Split(new char[] {','});
							if(d.Length==0)
							{
								sw.RCPT(Rto); 
							}
							else
							{
								for(int i=0;i<d.Length;i++ )
								{
									sw.RCPT(d[i]); 
								}
							}
						}
						if(textBox5.Text!="")
						{
							Rto=textBox5.Text;
							string[] d=Rto.Split(new char[] {','});
							if(d.Length==0)
							{
								sw.RCPT(Rto); 
							}
							else
							{
								for(int i=0;i<d.Length;i++ )
								{
									sw.RCPT(d[i]); 
								}
							}
						}
						
						sw.DATA(contect(),smtplogin);
						sw.QUIT(); 
						this.Close ();
						
						this.Dispose(); 

					}
					else
					{
						MessageBox.Show ("您的邮件没有收件人哦?");
					}

					break;
				case 1 :
					MessageBox.Show("直接发送到目标地址SMTP服务器");
					break;
				case 2 :
					//MessageBox.Show("保存邮件");
					if(textBox1.Text!="" )
					{
						FileInfo dd=new FileInfo("sent.wg");
						Encoding tmpEncoding = Encoding.ASCII;
						string econ=contect();
						
						if (dd.Exists)
						{
							StreamWriter sw =new StreamWriter(@"out.wg",true,Encoding.GetEncoding("gb2312")) ;
							sw.Write(econ); 
							sw.Close(); 
						}
						else
						{
							byte[] bytes=Encoding.Default.GetBytes(econ);///字符转换成字符流
							FileStream  emailstr =File.Create("sent.wg");//打开文件
							for(int i=0;i<bytes.Length;i++)
							{
								emailstr.WriteByte(bytes[i]);
							}
							emailstr.Close();
						}
						this.Close();
						this.Dispose(); 
					}
					else
					{
						MessageBox.Show("收件人为空!");
					}
					break;
				case 3 :
					MessageBox.Show("放入草稿箱");
					break;
				case 4 :
					//MessageBox.Show("增加附件");
					//Stream myStream;
					OpenFileDialog openFileDialog1 = new OpenFileDialog();
					//openFileDialog1.InitialDirectory = "c:\\" ;
					openFileDialog1.Filter = "All files (*.*)|*.*" ;
					openFileDialog1.FilterIndex = 2 ;
					openFileDialog1.Multiselect =true;
					openFileDialog1.RestoreDirectory = true ;

					if(openFileDialog1.ShowDialog() == DialogResult.OK)
					{
						//if((myStream = openFileDialog1.OpenFile())!= null)
						//{
							// Insert code to read the stream here.
							//myStream.Close();
						if(listView1.Items.Count<1)
						{
							textBox4.Height= textBox4.Height-70;
							listView1.Height= 60;
							listView1.Width =textBox4.Width ;
							listView1.Top=textBox4.Top + textBox4.Height + 10 ; 
							listView1.Left=textBox4.Left;  
						}
							listView1.Visible=true; 
							int i=0;
							for(i=0;i<openFileDialog1.FileNames.Length;i++)
							{
								listView1.Items.Add(openFileDialog1.FileNames[i].ToString()); 
							}
							  
						//}
					}
					
					break;
				case 5 :
					//MessageBox.Show("地址簿");
					Form5 Frm5=new Form5();
					Frm5.Show();
					break;
				
			}
		}

		private void label1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			
			label1.Cursor=System.Windows.Forms.Cursors.Hand; 
		}

		private void label1_Click(object sender, System.EventArgs e)
		{
		label1.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
		Form8 d=new Form8();
		this.AddOwnedForm(d);
		d.ShowDialog();
		label1.BorderStyle=System.Windows.Forms.BorderStyle.None; 
		
		}

		private void label2_Click(object sender, System.EventArgs e)
		{
			label2.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
			Form8 d=new Form8();
			this.AddOwnedForm(d);
			d.ShowDialog();
			label2.BorderStyle=System.Windows.Forms.BorderStyle.None; 
		}

		private void label4_Click(object sender, System.EventArgs e)
		{
			label4.BorderStyle=System.Windows.Forms.BorderStyle.Fixed3D;
			Form8 d=new Form8();
			this.AddOwnedForm(d);
			d.ShowDialog();
			label4.BorderStyle=System.Windows.Forms.BorderStyle.None; 
		}

		private void menuItem1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openFileDialog1 = new OpenFileDialog();
			//openFileDialog1.InitialDirectory = "c:\\" ;
			openFileDialog1.Filter = "All files (*.*)|*.*" ;
			openFileDialog1.FilterIndex = 2 ;
			openFileDialog1.Multiselect =true;
			openFileDialog1.RestoreDirectory = true ;

			if(openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				//if((myStream = openFileDialog1.OpenFile())!= null)
				//{
				// Insert code to read the stream here.
				//myStream.Close();
				if(listView1.Items.Count<1)
				{
					textBox4.Height= textBox4.Height-70;
					listView1.Height= 60;
					listView1.Width =textBox4.Width ;
					listView1.Top=textBox4.Top + textBox4.Height + 10 ; 
					listView1.Left=textBox4.Left;  
				}
				listView1.Visible=true; 
				int i=0;
				for(i=0;i<openFileDialog1.FileNames.Length;i++)
				{
					listView1.Items.Add(openFileDialog1.FileNames[i].ToString()); 
				}
							  
				//}
			}
		}

		/// <summary>
		/// 附件的删除
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void menuItem2_Click(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems.Count>0 )
			{
				
				for(int i=0;i<listView1.SelectedItems.Count;i++)
				{
					listView1.Items.RemoveAt(listView1.SelectedItems[i].Index);
				}
			}
			else
			{
				MessageBox.Show ("请先选中想删除的附件!");
			}
	}
		//对需要调用的dll进行导入
		[DllImport("shell32.dll")]
			//声明函数结构
			//原型的数据类型要进行更改
		private static extern int ShellExecute(int d, string ff, string fg, string gf, string hh, int y);


		private void menuItem4_Click(object sender, System.EventArgs e)
		{
			string filepath=listView1.SelectedItems[0].Text; 
			int f=ShellExecute(0,"open",filepath,"","",1);
		}

		private void listView1_DoubleClick(object sender, System.EventArgs e)
		{
			if(listView1.SelectedItems.Count>0)
			{
				string filepath=listView1.SelectedItems[0].Text; 
				int f=ShellExecute(0,"open",filepath,"","",1);
			}
		}

		private void Form4_Load(object sender, System.EventArgs e)
		{
			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 "smtpserver":
								smtpserver=headvalue;  
								break;
							case "user":
								user=headvalue;  
								break;
							case "password":
								pwd=headvalue;  
								break;
							case "smtplogin":
								smtplogin=headvalue; 
								break;
							case "emailaddress":
								emailaddress=headvalue; 

								break;
						}
}

				}
			}
		}
	}

⌨️ 快捷键说明

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