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

📄 winform.cs

📁 delphi 2005 C#Builder精彩编程实例集锦源代码第5章 网络开发编程实例实例143 如何向服务器上传文件
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using System.Net.Sockets;
using System.IO;


namespace Example
{
	/// <summary>
	/// Summary description for WinForm.
	/// </summary>
	public class WinForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.TextBox textBox5;

		public WinForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "邮件内容:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(168, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 2;
			this.label2.Text = "发送邮箱:";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(240, 16);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(136, 21);
			this.textBox1.TabIndex = 3;
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(240, 48);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(136, 21);
			this.textBox2.TabIndex = 5;
			this.textBox2.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(168, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "接收邮箱:";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(240, 80);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(136, 21);
			this.textBox3.TabIndex = 7;
			this.textBox3.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(168, 88);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(80, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "SMTP服务器:";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(240, 112);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(136, 21);
			this.textBox4.TabIndex = 9;
			this.textBox4.Text = "";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(168, 120);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(72, 16);
			this.label5.TabIndex = 8;
			this.label5.Text = "邮件主题:";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(176, 144);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(88, 23);
			this.button1.TabIndex = 10;
			this.button1.Text = "发送邮件";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(280, 144);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(88, 23);
			this.button2.TabIndex = 11;
			this.button2.Text = "关闭程序";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(16, 38);
			this.textBox5.Multiline = true;
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(144, 128);
			this.textBox5.TabIndex = 12;
			this.textBox5.Text = "";
			// 
			// WinForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(390, 179);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "WinForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "演示创建邮件发送程序";
			this.ResumeLayout(false);
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new WinForm());
		}
		//关闭程序
		private void button2_Click(object sender, System.EventArgs e)
		{
		   this.Close();
		}
		//发送邮件
		private void button1_Click(object sender, System.EventArgs e)
		{
		   if(this.textBox1.Text==string.Empty||
			   this.textBox2.Text==string.Empty||
				   this.textBox3.Text==string.Empty||
					   this.textBox4.Text==string.Empty||
						   this.textBox5.Text==string.Empty)
		   {
			  return;
		   }
		   	try
			{
				TcpClient MyServer=new TcpClient(this.textBox3.Text,25);
				string MySendString;
				byte[] MyBufferString;
				NetworkStream MyStream=MyServer.GetStream();
				StreamReader MyReader=new StreamReader(MyStream);

				MySendString="Server "+"\r\n";
				MyBufferString=System.Text.Encoding.ASCII.GetBytes(MySendString.ToCharArray());
				MyStream.Write(MyBufferString,0,MyBufferString.Length);

				MySendString="FROM: "+" < "+this.textBox1.Text+" > "+"\r\n";
				MyBufferString=System.Text.Encoding.ASCII.GetBytes(MySendString.ToCharArray());
				MyStream.Write(MyBufferString,0,MyBufferString.Length);

				MySendString="TO: "+" < "+this.textBox2.Text+" > "+"\r\n";
				MyBufferString=System.Text.Encoding.ASCII.GetBytes(MySendString.ToCharArray());
				MyStream.Write(MyBufferString,0,MyBufferString.Length);

				MySendString="SUBJECT: "+this.textBox4.Text+"\r\n";
				MyBufferString=System.Text.Encoding.Default.GetBytes(MySendString.ToCharArray());
				MyStream.Write(MyBufferString,0,MyBufferString.Length);

				MySendString="DATA "+this.textBox5.Text+"\r\n";
				MyBufferString=System.Text.Encoding.ASCII.GetBytes(MySendString.ToCharArray());
				MyStream.Write(MyBufferString,0,MyBufferString.Length);

				MySendString="QUIT"+"\r\n";
				MyBufferString=System.Text.Encoding.ASCII.GetBytes(MySendString.ToCharArray());
				MyStream.Write(MyBufferString,0,MyBufferString.Length);
				MyStream.Close();
				MyReader.Close();
				MessageBox.Show("发送邮件完成","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
			catch(Exception Ex)
			{
			   MessageBox.Show("发送邮件出现错误:"+Ex.Message,"信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
		}
	}
}

⌨️ 快捷键说明

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