Form1.cs
Upload User: chinafred
Upload Date: 2007-08-14
Package Size: 10127k
Code Size: 11k
Category:

ADO-ODBC

Development Platform:

C#

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Web.Mail;
  8. namespace ch7_4
  9. {
  10. /// <summary>
  11. /// Form1 的摘要说明。
  12. /// </summary>
  13. public class Form1 : System.Windows.Forms.Form
  14. {
  15. private System.Windows.Forms.Label label1;
  16. private System.Windows.Forms.TextBox textBox1;
  17. private System.Windows.Forms.Label label2;
  18. private System.Windows.Forms.TextBox textBox2;
  19. private System.Windows.Forms.Label label3;
  20. private System.Windows.Forms.Label label4;
  21. private System.Windows.Forms.TextBox textBox3;
  22. private System.Windows.Forms.TextBox textBox4;
  23. private System.Windows.Forms.Button button1;
  24. private System.Windows.Forms.RichTextBox richTextBox1;
  25. private System.Windows.Forms.Button button2;
  26. private System.Windows.Forms.RadioButton radioButton1;
  27. private System.Windows.Forms.RadioButton radioButton2;
  28. private System.Windows.Forms.Label label5;
  29. private System.Windows.Forms.TextBox textBox5;
  30. private System.Windows.Forms.ComboBox comboBox1;
  31. private System.Windows.Forms.Label label6;
  32. /// <summary>
  33. /// 必需的设计器变量。
  34. /// </summary>
  35. private System.ComponentModel.Container components = null;
  36. public Form1()
  37. {
  38. //
  39. // Windows 窗体设计器支持所必需的
  40. //
  41. InitializeComponent();
  42. //
  43. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  44. //
  45. }
  46. /// <summary>
  47. /// 清理所有正在使用的资源。
  48. /// </summary>
  49. protected override void Dispose( bool disposing )
  50. {
  51. if( disposing )
  52. {
  53. if (components != null) 
  54. {
  55. components.Dispose();
  56. }
  57. }
  58. base.Dispose( disposing );
  59. }
  60. #region Windows Form Designer generated code
  61. /// <summary>
  62. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  63. /// 此方法的内容。
  64. /// </summary>
  65. private void InitializeComponent()
  66. {
  67. this.label1 = new System.Windows.Forms.Label();
  68. this.textBox1 = new System.Windows.Forms.TextBox();
  69. this.label2 = new System.Windows.Forms.Label();
  70. this.textBox2 = new System.Windows.Forms.TextBox();
  71. this.label3 = new System.Windows.Forms.Label();
  72. this.label4 = new System.Windows.Forms.Label();
  73. this.textBox3 = new System.Windows.Forms.TextBox();
  74. this.textBox4 = new System.Windows.Forms.TextBox();
  75. this.button1 = new System.Windows.Forms.Button();
  76. this.richTextBox1 = new System.Windows.Forms.RichTextBox();
  77. this.button2 = new System.Windows.Forms.Button();
  78. this.radioButton1 = new System.Windows.Forms.RadioButton();
  79. this.radioButton2 = new System.Windows.Forms.RadioButton();
  80. this.label5 = new System.Windows.Forms.Label();
  81. this.textBox5 = new System.Windows.Forms.TextBox();
  82. this.comboBox1 = new System.Windows.Forms.ComboBox();
  83. this.label6 = new System.Windows.Forms.Label();
  84. this.SuspendLayout();
  85. // 
  86. // label1
  87. // 
  88. this.label1.Location = new System.Drawing.Point(24, 28);
  89. this.label1.Name = "label1";
  90. this.label1.Size = new System.Drawing.Size(56, 16);
  91. this.label1.TabIndex = 0;
  92. this.label1.Text = "收件人";
  93. // 
  94. // textBox1
  95. // 
  96. this.textBox1.Location = new System.Drawing.Point(112, 24);
  97. this.textBox1.Name = "textBox1";
  98. this.textBox1.Size = new System.Drawing.Size(256, 21);
  99. this.textBox1.TabIndex = 1;
  100. this.textBox1.Text = "";
  101. // 
  102. // label2
  103. // 
  104. this.label2.Location = new System.Drawing.Point(24, 60);
  105. this.label2.Name = "label2";
  106. this.label2.Size = new System.Drawing.Size(56, 16);
  107. this.label2.TabIndex = 2;
  108. this.label2.Text = "抄送";
  109. // 
  110. // textBox2
  111. // 
  112. this.textBox2.Location = new System.Drawing.Point(112, 56);
  113. this.textBox2.Name = "textBox2";
  114. this.textBox2.Size = new System.Drawing.Size(256, 21);
  115. this.textBox2.TabIndex = 3;
  116. this.textBox2.Text = "";
  117. // 
  118. // label3
  119. // 
  120. this.label3.Location = new System.Drawing.Point(24, 90);
  121. this.label3.Name = "label3";
  122. this.label3.Size = new System.Drawing.Size(72, 16);
  123. this.label3.TabIndex = 4;
  124. this.label3.Text = "主题";
  125. // 
  126. // label4
  127. // 
  128. this.label4.Location = new System.Drawing.Point(24, 124);
  129. this.label4.Name = "label4";
  130. this.label4.Size = new System.Drawing.Size(56, 16);
  131. this.label4.TabIndex = 5;
  132. this.label4.Text = "附件";
  133. // 
  134. // textBox3
  135. // 
  136. this.textBox3.Location = new System.Drawing.Point(112, 88);
  137. this.textBox3.Name = "textBox3";
  138. this.textBox3.Size = new System.Drawing.Size(256, 21);
  139. this.textBox3.TabIndex = 6;
  140. this.textBox3.Text = "";
  141. // 
  142. // textBox4
  143. // 
  144. this.textBox4.Location = new System.Drawing.Point(112, 120);
  145. this.textBox4.Name = "textBox4";
  146. this.textBox4.Size = new System.Drawing.Size(256, 21);
  147. this.textBox4.TabIndex = 7;
  148. this.textBox4.Text = "";
  149. // 
  150. // button1
  151. // 
  152. this.button1.Location = new System.Drawing.Point(384, 120);
  153. this.button1.Name = "button1";
  154. this.button1.Size = new System.Drawing.Size(72, 23);
  155. this.button1.TabIndex = 8;
  156. this.button1.Text = "添加附件";
  157. this.button1.Click += new System.EventHandler(this.button1_Click);
  158. // 
  159. // richTextBox1
  160. // 
  161. this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
  162. this.richTextBox1.Location = new System.Drawing.Point(0, 237);
  163. this.richTextBox1.Name = "richTextBox1";
  164. this.richTextBox1.Size = new System.Drawing.Size(472, 184);
  165. this.richTextBox1.TabIndex = 9;
  166. this.richTextBox1.Text = "";
  167. // 
  168. // button2
  169. // 
  170. this.button2.Location = new System.Drawing.Point(384, 196);
  171. this.button2.Name = "button2";
  172. this.button2.Size = new System.Drawing.Size(72, 23);
  173. this.button2.TabIndex = 10;
  174. this.button2.Text = "发送邮件";
  175. this.button2.Click += new System.EventHandler(this.button2_Click);
  176. // 
  177. // radioButton1
  178. // 
  179. this.radioButton1.Checked = true;
  180. this.radioButton1.Location = new System.Drawing.Point(208, 196);
  181. this.radioButton1.Name = "radioButton1";
  182. this.radioButton1.Size = new System.Drawing.Size(72, 24);
  183. this.radioButton1.TabIndex = 11;
  184. this.radioButton1.TabStop = true;
  185. this.radioButton1.Text = "文本方式";
  186. this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
  187. // 
  188. // radioButton2
  189. // 
  190. this.radioButton2.Location = new System.Drawing.Point(280, 196);
  191. this.radioButton2.Name = "radioButton2";
  192. this.radioButton2.Size = new System.Drawing.Size(72, 24);
  193. this.radioButton2.TabIndex = 12;
  194. this.radioButton2.Text = "html方式";
  195. this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
  196. // 
  197. // label5
  198. // 
  199. this.label5.Location = new System.Drawing.Point(24, 164);
  200. this.label5.Name = "label5";
  201. this.label5.Size = new System.Drawing.Size(56, 16);
  202. this.label5.TabIndex = 13;
  203. this.label5.Text = "发件人";
  204. // 
  205. // textBox5
  206. // 
  207. this.textBox5.Location = new System.Drawing.Point(112, 160);
  208. this.textBox5.Name = "textBox5";
  209. this.textBox5.Size = new System.Drawing.Size(256, 21);
  210. this.textBox5.TabIndex = 14;
  211. this.textBox5.Text = "";
  212. // 
  213. // comboBox1
  214. // 
  215. this.comboBox1.Items.AddRange(new object[] {
  216.    "正常",
  217.    "高",
  218.    "低"});
  219. this.comboBox1.Location = new System.Drawing.Point(112, 198);
  220. this.comboBox1.Name = "comboBox1";
  221. this.comboBox1.Size = new System.Drawing.Size(72, 20);
  222. this.comboBox1.TabIndex = 15;
  223. this.comboBox1.Text = "正常";
  224. // 
  225. // label6
  226. // 
  227. this.label6.Location = new System.Drawing.Point(24, 202);
  228. this.label6.Name = "label6";
  229. this.label6.Size = new System.Drawing.Size(48, 16);
  230. this.label6.TabIndex = 16;
  231. this.label6.Text = "优先级";
  232. // 
  233. // Form1
  234. // 
  235. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  236. this.ClientSize = new System.Drawing.Size(472, 421);
  237. this.Controls.AddRange(new System.Windows.Forms.Control[] {
  238.   this.label6,
  239.   this.comboBox1,
  240.   this.textBox5,
  241.   this.label5,
  242.   this.radioButton2,
  243.   this.radioButton1,
  244.   this.button2,
  245.   this.richTextBox1,
  246.   this.button1,
  247.   this.textBox4,
  248.   this.textBox3,
  249.   this.label4,
  250.   this.label3,
  251.   this.textBox2,
  252.   this.label2,
  253.   this.textBox1,
  254.   this.label1});
  255. this.Name = "Form1";
  256. this.Text = "发送邮件";
  257. this.ResumeLayout(false);
  258. }
  259. #endregion
  260. /// <summary>
  261. /// 应用程序的主入口点。
  262. /// </summary>
  263. [STAThread]
  264. static void Main() 
  265. {
  266. Application.Run(new Form1());
  267. }
  268. private void button2_Click(object sender, System.EventArgs e)
  269. {
  270. try
  271. {
  272. MailMessage message=new MailMessage();
  273. if (this.textBox1.Text!="")
  274. {
  275. message.To=this.textBox1.Text;
  276. //设置正文内容类型
  277. message.BodyFormat=MailFormat.Text;
  278. if (this.radioButton2.Checked==true)
  279. {
  280. message.BodyFormat=MailFormat.Html;
  281. }
  282. //设置邮件征文的编码类型
  283. message.BodyEncoding=System.Text.Encoding.UTF8;
  284. //设置邮件主题
  285. message.Subject=this.textBox3.Text;
  286. //设置邮件正文
  287. message.Body=this.richTextBox1.Text;
  288. //判断是否有附件
  289. if (this.textBox4.Text!="")
  290. {
  291. //构造一个附件对象
  292. MailAttachment attach=new MailAttachment(this.textBox4.Text);
  293. //添加附件
  294. message.Attachments.Add(attach);
  295. }
  296. //判断是否有抄送
  297. if (this.textBox3.Text!="")
  298. {
  299. message.Cc=this.textBox3.Text;
  300. }
  301. //设置发件人
  302. message.From=this.textBox5.Text;
  303. //设置优先级
  304. int i=this.comboBox1.SelectedIndex;
  305. if (i==0)
  306. {
  307. message.Priority=MailPriority.Normal;
  308. }
  309. if (i==1)
  310. {
  311. message.Priority=MailPriority.High;
  312. }
  313. if (i==2)
  314. {
  315. message.Priority=MailPriority.Low;
  316. }
  317. //发送邮件
  318. SmtpMail.Send(message);
  319. MessageBox.Show("邮件发送已经成功","恭喜"
  320. ,MessageBoxButtons.OK,MessageBoxIcon.Information);
  321. }
  322. else
  323. {
  324.     MessageBox.Show("未指定收件人地址","警告",
  325. MessageBoxButtons.OK,MessageBoxIcon.Warning);
  326. }
  327. }
  328. catch( Exception error)
  329. {
  330. MessageBox.Show("邮件发送失败信息:"+error.Message,"失败"
  331. ,MessageBoxButtons.OK,MessageBoxIcon.Information);
  332. }
  333. }
  334. private void button1_Click(object sender, System.EventArgs e)
  335. {
  336. OpenFileDialog oFileDialog=new OpenFileDialog();
  337. if (oFileDialog.ShowDialog()==DialogResult.OK)
  338. {
  339. this.textBox4.Text=oFileDialog.FileName;
  340. }
  341. }
  342. private void radioButton1_CheckedChanged(object sender, System.EventArgs e)
  343. {
  344. if (this.radioButton1.Checked==true)
  345. {
  346. //是两个单选按钮状态不同
  347. this.radioButton2.Checked=false;
  348. }
  349. }
  350. private void radioButton2_CheckedChanged(object sender, System.EventArgs e)
  351. {
  352. if (this.radioButton2.Checked==true)
  353. {
  354. //使两个单选按钮状态不同
  355. this.radioButton1.Checked=false;
  356. }
  357. }
  358. }
  359. }