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

📄 form1.cs

📁 用.netc#语言写的邮件收发小样例源代码,代码短小精悍,具备了核心扩展部分.再经过扩展就可以使用了
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Web.Mail;
namespace ch7_4
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.GroupBox groupBox2;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.Label label12;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.GroupBox 其它;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.RadioButton radioButton2;
		private System.Windows.Forms.RadioButton radioButton1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.GroupBox groupBox4;
		private System.Windows.Forms.RichTextBox richTextBox1;
		/// <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 Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.button2 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label8 = new System.Windows.Forms.Label();
			this.groupBox2 = new System.Windows.Forms.GroupBox();
			this.label9 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.其它 = new System.Windows.Forms.GroupBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.label6 = new System.Windows.Forms.Label();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.radioButton2 = new System.Windows.Forms.RadioButton();
			this.radioButton1 = new System.Windows.Forms.RadioButton();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.groupBox4 = new System.Windows.Forms.GroupBox();
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.groupBox1.SuspendLayout();
			this.groupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.groupBox4.SuspendLayout();
			this.SuspendLayout();
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(340, 174);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 23);
			this.button2.TabIndex = 10;
			this.button2.Text = "发送邮件";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label8);
			this.groupBox1.Controls.Add(this.textBox6);
			this.groupBox1.Controls.Add(this.label7);
			this.groupBox1.Location = new System.Drawing.Point(250, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(266, 72);
			this.groupBox1.TabIndex = 17;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "SMTP服务器";
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(114, 34);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(142, 21);
			this.textBox6.TabIndex = 3;
			this.textBox6.Text = "";
			// 
			// label7
			// 
			this.label7.Location = new System.Drawing.Point(-72, 54);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(56, 16);
			this.label7.TabIndex = 2;
			this.label7.Text = "收件人";
			// 
			// label8
			// 
			this.label8.Location = new System.Drawing.Point(12, 36);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(98, 16);
			this.label8.TabIndex = 4;
			this.label8.Text = "邮件发送服务器:";
			// 
			// groupBox2
			// 
			this.groupBox2.Controls.Add(this.textBox5);
			this.groupBox2.Controls.Add(this.textBox3);
			this.groupBox2.Controls.Add(this.textBox2);
			this.groupBox2.Controls.Add(this.textBox1);
			this.groupBox2.Controls.Add(this.label9);
			this.groupBox2.Controls.Add(this.label10);
			this.groupBox2.Controls.Add(this.label11);
			this.groupBox2.Controls.Add(this.label12);
			this.groupBox2.Location = new System.Drawing.Point(10, 6);
			this.groupBox2.Name = "groupBox2";
			this.groupBox2.Size = new System.Drawing.Size(230, 188);
			this.groupBox2.TabIndex = 18;
			this.groupBox2.TabStop = false;
			this.groupBox2.Text = "邮件头";
			// 
			// label9
			// 
			this.label9.Location = new System.Drawing.Point(12, 146);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(56, 16);
			this.label9.TabIndex = 17;
			this.label9.Text = "发件人";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(12, 106);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(72, 16);
			this.label10.TabIndex = 16;
			this.label10.Text = "主题";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(12, 66);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(56, 16);
			this.label11.TabIndex = 15;
			this.label11.Text = "抄送";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(12, 26);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(56, 16);
			this.label12.TabIndex = 14;
			this.label12.Text = "收件人";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(60, 141);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(158, 21);
			this.textBox5.TabIndex = 21;
			this.textBox5.Text = "";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(60, 102);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(158, 21);
			this.textBox3.TabIndex = 20;
			this.textBox3.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(60, 63);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(158, 21);
			this.textBox2.TabIndex = 19;
			this.textBox2.Text = "";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(60, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(158, 21);
			this.textBox1.TabIndex = 18;
			this.textBox1.Text = "";
			// 
			// 其它
			// 
			this.其它.Location = new System.Drawing.Point(254, 170);
			this.其它.Name = "其它";
			this.其它.TabIndex = 19;
			this.其它.TabStop = false;
			this.其它.Text = "groupBox3";
			// 
			// groupBox3
			// 
			this.groupBox3.Controls.Add(this.label6);
			this.groupBox3.Controls.Add(this.comboBox1);
			this.groupBox3.Controls.Add(this.radioButton2);
			this.groupBox3.Controls.Add(this.radioButton1);
			this.groupBox3.Controls.Add(this.button1);
			this.groupBox3.Controls.Add(this.textBox4);
			this.groupBox3.Controls.Add(this.label4);
			this.groupBox3.Location = new System.Drawing.Point(252, 88);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(266, 78);
			this.groupBox3.TabIndex = 19;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "其它";
			// 
			// label6
			// 
			this.label6.Location = new System.Drawing.Point(10, 52);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(48, 16);
			this.label6.TabIndex = 23;
			this.label6.Text = "优先级";
			// 
			// comboBox1
			// 
			this.comboBox1.Items.AddRange(new object[] {
														   "正常",
														   "高",
														   "低"});
			this.comboBox1.Location = new System.Drawing.Point(58, 50);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(54, 20);
			this.comboBox1.TabIndex = 22;
			this.comboBox1.Text = "正常";
			// 
			// radioButton2
			// 
			this.radioButton2.Location = new System.Drawing.Point(174, 50);
			this.radioButton2.Name = "radioButton2";
			this.radioButton2.Size = new System.Drawing.Size(72, 24);
			this.radioButton2.TabIndex = 21;
			this.radioButton2.Text = "html";
			// 
			// radioButton1
			// 
			this.radioButton1.Checked = true;
			this.radioButton1.Location = new System.Drawing.Point(122, 50);
			this.radioButton1.Name = "radioButton1";
			this.radioButton1.Size = new System.Drawing.Size(72, 24);
			this.radioButton1.TabIndex = 20;
			this.radioButton1.TabStop = true;
			this.radioButton1.Text = "文本";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(200, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 23);
			this.button1.TabIndex = 19;
			this.button1.Text = "添加";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(46, 14);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(148, 21);
			this.textBox4.TabIndex = 18;
			this.textBox4.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(10, 22);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(56, 16);
			this.label4.TabIndex = 17;
			this.label4.Text = "附件";
			// 
			// groupBox4
			// 
			this.groupBox4.Controls.Add(this.richTextBox1);
			this.groupBox4.Location = new System.Drawing.Point(8, 206);
			this.groupBox4.Name = "groupBox4";
			this.groupBox4.Size = new System.Drawing.Size(510, 208);
			this.groupBox4.TabIndex = 20;
			this.groupBox4.TabStop = false;
			this.groupBox4.Text = "信件内容";
			// 
			// 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(504, 188);
			this.richTextBox1.TabIndex = 10;
			this.richTextBox1.Text = "";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(532, 421);
			this.Controls.Add(this.groupBox4);
			this.Controls.Add(this.groupBox3);
			this.Controls.Add(this.groupBox2);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.button2);
			this.Name = "Form1";
			this.Text = "发送邮件";
			this.groupBox1.ResumeLayout(false);
			this.groupBox2.ResumeLayout(false);
			this.groupBox3.ResumeLayout(false);
			this.groupBox4.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			try
			{
				MailMessage message=new MailMessage();
				if (this.textBox1.Text!="")
				{
					message.To=this.textBox1.Text;
					//设置正文内容类型
					message.BodyFormat=MailFormat.Text;
					if (this.radioButton2.Checked==true)
					{
						message.BodyFormat=MailFormat.Html;
					}
					//设置邮件征文的编码类型
					message.BodyEncoding=System.Text.Encoding.UTF8;
					//设置邮件主题
					message.Subject=this.textBox3.Text;
					//设置邮件正文
					message.Body=this.richTextBox1.Text;

					//判断是否有附件
					if (this.textBox4.Text!="")
					{
						//构造一个附件对象
						MailAttachment attach=new MailAttachment(this.textBox4.Text);
						//添加附件
						message.Attachments.Add(attach);
					}
					//判断是否有抄送
					if (this.textBox3.Text!="")
					{
						message.Cc=this.textBox3.Text;
					}
					//设置发件人
					message.From=this.textBox5.Text;
					//设置优先级
					int i=this.comboBox1.SelectedIndex;
					if (i==0)
					{
						message.Priority=MailPriority.Normal;
					}
					if (i==1)
					{
						message.Priority=MailPriority.High;
					}
					if (i==2)
					{
						message.Priority=MailPriority.Low;
					}

					//设置邮件发送服务器
					SmtpMail.SmtpServer=this.textBox6.Text;
					//发送邮件
					SmtpMail.Send(message);
					MessageBox.Show("邮件发送已经成功","恭喜"
						,MessageBoxButtons.OK,MessageBoxIcon.Information);
				}
				else
				{
				    MessageBox.Show("未指定收件人地址","警告",
					MessageBoxButtons.OK,MessageBoxIcon.Warning);
				}
			}
			catch( Exception error)
			{
				MessageBox.Show("邮件发送失败信息:"+error.Message,"失败"
					,MessageBoxButtons.OK,MessageBoxIcon.Information);
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog oFileDialog=new OpenFileDialog();
			if (oFileDialog.ShowDialog()==DialogResult.OK)
			{
			this.textBox4.Text=oFileDialog.FileName;		
			}
		}

		private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.radioButton1.Checked==true)
			{
			//是两个单选按钮状态不同
			this.radioButton2.Checked=false;
			}
		}

		private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
		{
			if (this.radioButton2.Checked==true)
			{
			//使两个单选按钮状态不同
			this.radioButton1.Checked=false;
			}
		}
	}
}

⌨️ 快捷键说明

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