Form1.cs
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:15k
源码类别:

Email客户端

开发平台:

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