Form1.cs
上传用户:youdujia
上传日期:2017-09-03
资源大小:100k
文件大小:15k
源码类别:

WEB邮件程序

开发平台:

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