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

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.Net ;
  7. using System.Net .Sockets ;
  8. using MailSend;
  9. using System.Data ;
  10. using System.Runtime .InteropServices ;
  11. namespace MailManageSystem
  12. {
  13. /// <summary>
  14. /// Send 的摘要说明。
  15. /// </summary>
  16. public class SendMailForm : System.Windows.Forms.Form
  17. {
  18. private System.Windows.Forms.MainMenu mainMenu1;
  19. private System.Windows.Forms.MenuItem menuItem1;
  20. private System.Windows.Forms.MenuItem menuItem2;
  21. private System.Windows.Forms.MenuItem menuItem3;
  22. private System.Windows.Forms.MenuItem menuItem4;
  23. private System.Windows.Forms.MenuItem menuItem5;
  24. private System.Windows.Forms.MenuItem menuItem6;
  25. private System.Windows.Forms.MenuItem menuItem7;
  26. private System.Windows.Forms.MenuItem menuItem8;
  27. private System.ComponentModel.IContainer components;
  28. private System.Windows.Forms.OpenFileDialog openFileDialog1;//语言编码 
  29. private System.Windows.Forms.ToolBar toolBar1;
  30. private System.Windows.Forms.ToolBarButton tbSend;
  31. private System.Windows.Forms.ToolBarButton tbSave;
  32. private System.Windows.Forms.ToolBarButton toolBarButton1;
  33. private System.Windows.Forms.ToolBarButton tbAttachment;
  34. private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  35. private System.Windows.Forms.ImageList imlSendMail;
  36. private System.Windows.Forms.ImageList imlAttachment;
  37. private System.Windows.Forms.ContextMenu conAttachment;
  38. private System.Windows.Forms.MenuItem menuItem9;
  39. private System.Windows.Forms.MenuItem menuItem10;
  40. private System.Windows.Forms.MenuItem menuItem11;
  41. private System.Windows.Forms.MenuItem menuItem12;
  42. private System.Windows.Forms.MenuItem menuItem13;
  43. private System.Windows.Forms.MenuItem menuItem14;
  44. private System.Windows.Forms.MenuItem menuItem15;
  45. private System.Windows.Forms.MenuItem menuItem16;
  46. private System.Windows.Forms.MenuItem menuItem17;
  47. private System.Windows.Forms.GroupBox groupBox2;
  48. private System.Windows.Forms.TextBox txtTo;
  49. private System.Windows.Forms.Label label3;
  50. private System.Windows.Forms.Label label4;
  51. private System.Windows.Forms.TextBox txtSubject;
  52. private System.Windows.Forms.Splitter splitter1;
  53. private System.Windows.Forms.Panel panel1;
  54. private System.Windows.Forms.RichTextBox txtMessage;
  55. private ArrayList array=new ArrayList ();
  56. private System.Windows.Forms.ListView ltAttachment;
  57. private System.Windows.Forms.ComboBox cbHtml;
  58. int i = 0;
  59. //EntryPoint 给出Dll入口点的名称,如果没有给出,则用方法本身的名称。
  60. [DllImport("shell32.dll",EntryPoint="ShellExecute")]
  61. private static extern int ShellExecute(IntPtr hwnd, string lpOperation,string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
  62. //函数SHGetFileInfo()的返回值也随uFlags的取值变化而有所不同。
  63. //通过调用SHGetFileInfo()可以由psfi参数得到文件的图标句柄,但要注意在uFlags参数中不使用SHGFI_PIDL时,SHGetFileInfo()不能获得"我的电脑"等虚似文件夹的信息。
  64. [DllImport("Shell32.dll")]
  65. private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, SHGFI uFlags);
  66. [StructLayout(LayoutKind.Sequential)]
  67. private struct SHFILEINFO
  68. {
  69. //重载一个构造函数
  70. public SHFILEINFO(bool b)
  71. {
  72. hIcon = IntPtr.Zero ; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
  73. }
  74. public IntPtr hIcon;
  75. public int iIcon;
  76. public uint dwAttributes;
  77. [MarshalAs(UnmanagedType.LPStr, SizeConst =256)]
  78. public string szDisplayName;
  79. [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
  80. public string szTypeName;
  81. };
  82. //图标类型的结构体
  83. private enum SHGFI
  84. {
  85. SHGFI_ICON            =0x000000100,     // get icon
  86. SHGFI_DISPLAYNAME     =0x000000200,     // get display name
  87. SHGFI_TYPENAME        =0x000000400,     // get type name
  88. SHGFI_ATTRIBUTES      =0x000000800,     // get attributes
  89. SHGFI_ICONLOCATION    =0x000001000,     // get icon location
  90. SHGFI_EXETYPE         =0x000002000,     // return exe type
  91. SHGFI_SYSICONINDEX   =0x000004000,     // get system icon index
  92. SHGFI_LINKOVERLAY     =0x000008000,     // put a link overlay on icon
  93. SHGFI_SELECTED        =0x000010000,     // show icon in selected state
  94. SHGFI_ATTR_SPECIFIED  =0x000020000,     // get only specified attributes
  95. SHGFI_LARGEICON       =0x000000000,     // get large icon
  96. SHGFI_SMALLICON       =0x000000001,     // get small icon
  97. SHGFI_OPENICON        =0x000000002,     // get open icon
  98. SHGFI_SHELLICONSIZE   =0x000000004,     // get shell size icon
  99. SHGFI_PIDL   =0x000000008,     // pszPath is a pidl
  100. SHGFI_USEFILEATTRIBUTES     =0x000000010,     // use passed dwFileAttribute
  101. SHGFI_ADDOVERLAYS           =0x000000020,     // apply the appropriate overlays
  102. SHGFI_OVERLAYINDEX          =0x000000040     // Get the index of the overlay
  103. }
  104. /// <summary>
  105. /// 调用获的程序图标句柄的函数
  106. /// </summary>
  107. /// <param name="strPath">选定程序的绝对路径</param>
  108. /// <returns></returns>
  109. private  static Icon GetIcon(string strPath)
  110. {
  111. try
  112. {
  113. SHFILEINFO info = new SHFILEINFO();
  114. int cbFileInfo = Marshal.SizeOf(info);
  115. SHGFI flags;
  116. flags = SHGFI.SHGFI_ICON|SHGFI.SHGFI_LARGEICON|SHGFI.SHGFI_ATTRIBUTES|SHGFI.SHGFI_EXETYPE;
  117. SHGetFileInfo(strPath, 32|0, ref  info,(uint)cbFileInfo,flags);
  118. return Icon.FromHandle(info.hIcon);
  119. }
  120. catch(Exception ex)
  121. {
  122. MessageBox.Show (ex.ToString ());
  123. return null;
  124. }
  125. }
  126.    
  127. public SendMailForm()
  128. {
  129. //
  130. // Windows 窗体设计器支持所必需的
  131. //
  132. InitializeComponent();
  133. //
  134. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  135. //
  136. }
  137. /// <summary>
  138. /// 清理所有正在使用的资源。
  139. /// </summary>
  140. protected override void Dispose( bool disposing )
  141. {
  142. if( disposing )
  143. {
  144. if(components != null)
  145. {
  146. components.Dispose();
  147. }
  148. }
  149. base.Dispose( disposing );
  150. }
  151. #region Windows 窗体设计器生成的代码
  152. /// <summary>
  153. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  154. /// 此方法的内容。
  155. /// </summary>
  156. private void InitializeComponent()
  157. {
  158. this.components = new System.ComponentModel.Container();
  159. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SendMailForm));
  160. this.mainMenu1 = new System.Windows.Forms.MainMenu();
  161. this.menuItem1 = new System.Windows.Forms.MenuItem();
  162. this.menuItem2 = new System.Windows.Forms.MenuItem();
  163. this.menuItem3 = new System.Windows.Forms.MenuItem();
  164. this.menuItem8 = new System.Windows.Forms.MenuItem();
  165. this.menuItem4 = new System.Windows.Forms.MenuItem();
  166. this.menuItem5 = new System.Windows.Forms.MenuItem();
  167. this.menuItem6 = new System.Windows.Forms.MenuItem();
  168. this.menuItem7 = new System.Windows.Forms.MenuItem();
  169. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
  170. this.toolBar1 = new System.Windows.Forms.ToolBar();
  171. this.tbSend = new System.Windows.Forms.ToolBarButton();
  172. this.tbSave = new System.Windows.Forms.ToolBarButton();
  173. this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
  174. this.tbAttachment = new System.Windows.Forms.ToolBarButton();
  175. this.imlSendMail = new System.Windows.Forms.ImageList(this.components);
  176. this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
  177. this.imlAttachment = new System.Windows.Forms.ImageList(this.components);
  178. this.conAttachment = new System.Windows.Forms.ContextMenu();
  179. this.menuItem9 = new System.Windows.Forms.MenuItem();
  180. this.menuItem10 = new System.Windows.Forms.MenuItem();
  181. this.menuItem11 = new System.Windows.Forms.MenuItem();
  182. this.menuItem12 = new System.Windows.Forms.MenuItem();
  183. this.menuItem13 = new System.Windows.Forms.MenuItem();
  184. this.menuItem14 = new System.Windows.Forms.MenuItem();
  185. this.menuItem15 = new System.Windows.Forms.MenuItem();
  186. this.menuItem17 = new System.Windows.Forms.MenuItem();
  187. this.menuItem16 = new System.Windows.Forms.MenuItem();
  188. this.groupBox2 = new System.Windows.Forms.GroupBox();
  189. this.cbHtml = new System.Windows.Forms.ComboBox();
  190. this.txtTo = new System.Windows.Forms.TextBox();
  191. this.label3 = new System.Windows.Forms.Label();
  192. this.label4 = new System.Windows.Forms.Label();
  193. this.txtSubject = new System.Windows.Forms.TextBox();
  194. this.ltAttachment = new System.Windows.Forms.ListView();
  195. this.splitter1 = new System.Windows.Forms.Splitter();
  196. this.panel1 = new System.Windows.Forms.Panel();
  197. this.txtMessage = new System.Windows.Forms.RichTextBox();
  198. this.groupBox2.SuspendLayout();
  199. this.panel1.SuspendLayout();
  200. this.SuspendLayout();
  201. // 
  202. // mainMenu1
  203. // 
  204. this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  205.   this.menuItem1,
  206.   this.menuItem5,
  207.   this.menuItem6});
  208. // 
  209. // menuItem1
  210. // 
  211. this.menuItem1.Index = 0;
  212. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  213.   this.menuItem2,
  214.   this.menuItem3,
  215.   this.menuItem8,
  216.   this.menuItem4});
  217. this.menuItem1.Text = "邮件(M)";
  218. // 
  219. // menuItem2
  220. // 
  221. this.menuItem2.Index = 0;
  222. this.menuItem2.Text = "立即发送";
  223. this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
  224. // 
  225. // menuItem3
  226. // 
  227. this.menuItem3.Index = 1;
  228. this.menuItem3.Text = "草稿";
  229. // 
  230. // menuItem8
  231. // 
  232. this.menuItem8.Index = 2;
  233. this.menuItem8.Text = "-";
  234. // 
  235. // menuItem4
  236. // 
  237. this.menuItem4.Index = 3;
  238. this.menuItem4.Text = "退出";
  239. this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
  240. // 
  241. // menuItem5
  242. // 
  243. this.menuItem5.Index = 1;
  244. this.menuItem5.Text = "";
  245. // 
  246. // menuItem6
  247. // 
  248. this.menuItem6.Index = 2;
  249. this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  250.   this.menuItem7});
  251. this.menuItem6.Text = "插入(I)";
  252. // 
  253. // menuItem7
  254. // 
  255. this.menuItem7.Index = 0;
  256. this.menuItem7.Text = "增加附件";
  257. this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
  258. // 
  259. // toolBar1
  260. // 
  261. this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
  262. this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
  263. this.tbSend,
  264. this.tbSave,
  265. this.toolBarButton1,
  266. this.tbAttachment});
  267. this.toolBar1.ButtonSize = new System.Drawing.Size(100, 80);
  268. this.toolBar1.DropDownArrows = true;
  269. this.toolBar1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  270. this.toolBar1.ImageList = this.imlSendMail;
  271. this.toolBar1.Location = new System.Drawing.Point(0, 0);
  272. this.toolBar1.Name = "toolBar1";
  273. this.toolBar1.ShowToolTips = true;
  274. this.toolBar1.Size = new System.Drawing.Size(800, 41);
  275. this.toolBar1.TabIndex = 3;
  276. this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
  277. // 
  278. // tbSend
  279. // 
  280. this.tbSend.ImageIndex = 0;
  281. this.tbSend.Text = "发送";
  282. // 
  283. // tbSave
  284. // 
  285. this.tbSave.ImageIndex = 1;
  286. this.tbSave.Text = "保存";
  287. // 
  288. // toolBarButton1
  289. // 
  290. this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
  291. // 
  292. // tbAttachment
  293. // 
  294. this.tbAttachment.ImageIndex = 2;
  295. this.tbAttachment.Text = "附件";
  296. // 
  297. // imlSendMail
  298. // 
  299. this.imlSendMail.ImageSize = new System.Drawing.Size(16, 16);
  300. this.imlSendMail.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imlSendMail.ImageStream")));
  301. this.imlSendMail.TransparentColor = System.Drawing.Color.Transparent;
  302. // 
  303. // imlAttachment
  304. // 
  305. this.imlAttachment.ImageSize = new System.Drawing.Size(16, 16);
  306. this.imlAttachment.TransparentColor = System.Drawing.Color.Transparent;
  307. // 
  308. // conAttachment
  309. // 
  310. this.conAttachment.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  311.   this.menuItem9,
  312.   this.menuItem10,
  313.   this.menuItem11,
  314.   this.menuItem12,
  315.   this.menuItem13,
  316.   this.menuItem14});
  317. // 
  318. // menuItem9
  319. // 
  320. this.menuItem9.Index = 0;
  321. this.menuItem9.Shortcut = System.Windows.Forms.Shortcut.Alt0;
  322. this.menuItem9.Text = "增加(A)...";
  323. this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click_1);
  324. // 
  325. // menuItem10
  326. // 
  327. this.menuItem10.Index = 1;
  328. this.menuItem10.Shortcut = System.Windows.Forms.Shortcut.Alt1;
  329. this.menuItem10.Text = "删除(D)...";
  330. this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
  331. // 
  332. // menuItem11
  333. // 
  334. this.menuItem11.Index = 2;
  335. this.menuItem11.Text = "-";
  336. // 
  337. // menuItem12
  338. // 
  339. this.menuItem12.Index = 3;
  340. this.menuItem12.Shortcut = System.Windows.Forms.Shortcut.Alt2;
  341. this.menuItem12.Text = "打开(O)...";
  342. this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
  343. // 
  344. // menuItem13
  345. // 
  346. this.menuItem13.Index = 4;
  347. this.menuItem13.Text = "-";
  348. // 
  349. // menuItem14
  350. // 
  351. this.menuItem14.Index = 5;
  352. this.menuItem14.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  353.    this.menuItem15,
  354.    this.menuItem17,
  355.    this.menuItem16});
  356. this.menuItem14.Text = "排列图标(V)...";
  357. // 
  358. // menuItem15
  359. // 
  360. this.menuItem15.Index = 0;
  361. this.menuItem15.Text = "大图标";
  362. this.menuItem15.Click += new System.EventHandler(this.menuItem15_Click_1);
  363. // 
  364. // menuItem17
  365. // 
  366. this.menuItem17.Index = 1;
  367. this.menuItem17.Text = "小图标";
  368. this.menuItem17.Click += new System.EventHandler(this.menuItem17_Click);
  369. // 
  370. // menuItem16
  371. // 
  372. this.menuItem16.Index = 2;
  373. this.menuItem16.Text = "列表";
  374. this.menuItem16.Click += new System.EventHandler(this.menuItem16_Click_1);
  375. // 
  376. // groupBox2
  377. // 
  378. this.groupBox2.Controls.Add(this.cbHtml);
  379. this.groupBox2.Controls.Add(this.txtTo);
  380. this.groupBox2.Controls.Add(this.label3);
  381. this.groupBox2.Controls.Add(this.label4);
  382. this.groupBox2.Controls.Add(this.txtSubject);
  383. this.groupBox2.Dock = System.Windows.Forms.DockStyle.Top;
  384. this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
  385. this.groupBox2.Location = new System.Drawing.Point(0, 41);
  386. this.groupBox2.Name = "groupBox2";
  387. this.groupBox2.Size = new System.Drawing.Size(800, 104);
  388. this.groupBox2.TabIndex = 4;
  389. this.groupBox2.TabStop = false;
  390. this.groupBox2.Text = "邮件头";
  391. // 
  392. // cbHtml
  393. // 
  394. this.cbHtml.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
  395. this.cbHtml.Items.AddRange(new object[] {
  396. "HTML邮件",
  397. "纯文本邮件"});
  398. this.cbHtml.Location = new System.Drawing.Point(704, 72);
  399. this.cbHtml.Name = "cbHtml";
  400. this.cbHtml.Size = new System.Drawing.Size(88, 20);
  401. this.cbHtml.TabIndex = 8;
  402. this.cbHtml.Text = "HTML邮件";
  403. // 
  404. // txtTo
  405. // 
  406. this.txtTo.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  407. | System.Windows.Forms.AnchorStyles.Left) 
  408. | System.Windows.Forms.AnchorStyles.Right)));
  409. this.txtTo.Location = new System.Drawing.Point(88, 72);
  410. this.txtTo.Name = "txtTo";
  411. this.txtTo.Size = new System.Drawing.Size(584, 21);
  412. this.txtTo.TabIndex = 6;
  413. this.txtTo.Text = "yinhu82@163.com,yinhu82@sina.com.cn";
  414. // 
  415. // label3
  416. // 
  417. this.label3.Location = new System.Drawing.Point(24, 72);
  418. this.label3.Name = "label3";
  419. this.label3.Size = new System.Drawing.Size(48, 23);
  420. this.label3.TabIndex = 5;
  421. this.label3.Text = "收件人:";
  422. this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  423. // 
  424. // label4
  425. // 
  426. this.label4.Location = new System.Drawing.Point(16, 32);
  427. this.label4.Name = "label4";
  428. this.label4.Size = new System.Drawing.Size(64, 23);
  429. this.label4.TabIndex = 4;
  430. this.label4.Text = "邮件主题:";
  431. this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  432. // 
  433. // txtSubject
  434. // 
  435. this.txtSubject.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
  436. | System.Windows.Forms.AnchorStyles.Left) 
  437. | System.Windows.Forms.AnchorStyles.Right)));
  438. this.txtSubject.Location = new System.Drawing.Point(88, 32);
  439. this.txtSubject.Name = "txtSubject";
  440. this.txtSubject.Size = new System.Drawing.Size(664, 21);
  441. this.txtSubject.TabIndex = 7;
  442. this.txtSubject.Text = "最新版";
  443. // 
  444. // ltAttachment
  445. // 
  446. this.ltAttachment.AllowDrop = true;
  447. this.ltAttachment.ContextMenu = this.conAttachment;
  448. this.ltAttachment.Dock = System.Windows.Forms.DockStyle.Bottom;
  449. this.ltAttachment.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
  450. this.ltAttachment.LargeImageList = this.imlAttachment;
  451. this.ltAttachment.Location = new System.Drawing.Point(0, 392);
  452. this.ltAttachment.Name = "ltAttachment";
  453. this.ltAttachment.Size = new System.Drawing.Size(800, 97);
  454. this.ltAttachment.TabIndex = 5;
  455. this.ltAttachment.DragDrop += new System.Windows.Forms.DragEventHandler(this.listView1_DragDrop);
  456. this.ltAttachment.DragEnter += new System.Windows.Forms.DragEventHandler(this.listView1_DragEnter);
  457. // 
  458. // splitter1
  459. // 
  460. this.splitter1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
  461. this.splitter1.Dock = System.Windows.Forms.DockStyle.Bottom;
  462. this.splitter1.Location = new System.Drawing.Point(0, 388);
  463. this.splitter1.Name = "splitter1";
  464. this.splitter1.Size = new System.Drawing.Size(800, 4);
  465. this.splitter1.TabIndex = 6;
  466. this.splitter1.TabStop = false;
  467. // 
  468. // panel1
  469. // 
  470. this.panel1.Controls.Add(this.txtMessage);
  471. this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
  472. this.panel1.Location = new System.Drawing.Point(0, 145);
  473. this.panel1.Name = "panel1";
  474. this.panel1.Size = new System.Drawing.Size(800, 243);
  475. this.panel1.TabIndex = 7;
  476. // 
  477. // txtMessage
  478. // 
  479. this.txtMessage.Dock = System.Windows.Forms.DockStyle.Fill;
  480. this.txtMessage.Location = new System.Drawing.Point(0, 0);
  481. this.txtMessage.Name = "txtMessage";
  482. this.txtMessage.Size = new System.Drawing.Size(800, 243);
  483. this.txtMessage.TabIndex = 0;
  484. this.txtMessage.Text = "呵呵 这是第一封试验邮件";
  485. // 
  486. // SendMailForm
  487. // 
  488. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  489. this.ClientSize = new System.Drawing.Size(800, 489);
  490. this.Controls.Add(this.panel1);
  491. this.Controls.Add(this.splitter1);
  492. this.Controls.Add(this.ltAttachment);
  493. this.Controls.Add(this.groupBox2);
  494. this.Controls.Add(this.toolBar1);
  495. this.Menu = this.mainMenu1;
  496. this.Name = "SendMailForm";
  497. this.Text = "Send";
  498. this.Load += new System.EventHandler(this.SendMailForm_Load);
  499. this.groupBox2.ResumeLayout(false);
  500. this.panel1.ResumeLayout(false);
  501. this.ResumeLayout(false);
  502. }
  503. #endregion
  504. //发送邮件
  505. private void menuItem2_Click(object sender, System.EventArgs e)
  506. {
  507. bool html=false;
  508. if(this.cbHtml .Text=="HTML邮件"|this.cbHtml .Text =="纯文本邮件")
  509. {
  510. if(this.cbHtml .Text=="HTML邮件")
  511. html=true;
  512. else if(this.cbHtml .Text=="纯文本邮件")
  513. html=false;
  514. }
  515. else 
  516. {
  517. MessageBox.Show ("请做出有效的邮件格式选择");
  518. this.cbHtml .Text ="HTML邮件";
  519. return;
  520. }
  521. SmtpMail send=new SmtpMail (array);
  522. Cursor.Current =Cursors.WaitCursor ;
  523. send.Charset ="GB2312";
  524. send.Priority ="Normal";
  525. send.eSmtp=true;
  526. send.MailServerUserName =SystemManage.UserName ;
  527. send.MailServerPassWord =SystemManage.PassWd ;
  528. send.Send (SystemManage.SmtpName,SystemManage.MailAddress ,SystemManage.AccountName,this.txtTo .Text ,html,this.txtSubject .Text ,this.txtMessage .Text );
  529. Cursor.Current =Cursors.Default ;
  530. SystemManage.letterNumber ++;
  531. ltAttachment  .Items .Clear ();
  532. this.Close ();
  533. }
  534. private void menuItem4_Click(object sender, System.EventArgs e)
  535. {
  536. this.Close ();
  537. }
  538. //添加附件名称和图标的函数
  539. private void btnAdd_Click()
  540. {
  541. if(this.openFileDialog1 .ShowDialog()==DialogResult.OK)
  542. {
  543. array.Add (this.openFileDialog1 .FileName );
  544. AddIcon(this.openFileDialog1.FileName);
  545. ltAttachment  .Visible =true;
  546. this.splitter1 .Visible =true;
  547. }
  548. }
  549. /// <summary>
  550. /// 判断加入的文件格式,并添加到附件ListView中
  551. /// </summary>
  552. /// <param name="fileType"></param>
  553. private void AddIcon(string fileType)
  554. {
  555. //string type = fileType.Substring(fileType.Length - 3);
  556. try 
  557. {
  558. string Name = fileType.Substring(fileType.LastIndexOf(@"") + 1);
  559. this.imlAttachment.Images.Add(GetIcon(fileType));
  560. this.ltAttachment.LargeImageList = this.imlAttachment;
  561. this.ltAttachment .SmallImageList =this.imlAttachment ;
  562. this.ltAttachment .Items .Add (Name,i++);
  563. }
  564. catch(Exception ex)
  565. {
  566. MessageBox.Show(ex.ToString ());
  567. }
  568. }
  569. private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
  570. {
  571. switch(this.toolBar1 .Buttons.IndexOf (e.Button ))
  572. {
  573. case 0:
  574. menuItem2_Click(null,null);
  575. break;
  576. case 1:
  577. this.saveFileDialog1.Title ="保存信件";
  578. this.saveFileDialog1 .Filter ="文本文件(*.txt)|*.txt|All Files(*.*)|*.*";
  579. AddExtra extra=new AddExtra( array);
  580. string info="SMTP地址:"+SystemManage.SmtpName+"rn用户名:"+SystemManage.UserName+"rn密码:"+SystemManage.PassWd+"rn发信人地址:"+SystemManage.MailAddress+"rn主题:"+this.txtSubject .Text+"rn收信人地址:"+this.txtTo .Text +"rn信件内容:"+this.txtMessage .Text+"rn";
  581. if(this.saveFileDialog1 .ShowDialog ()==DialogResult.OK )
  582. {
  583. extra.SaveLetter (this.saveFileDialog1 .FileName,info);
  584. MessageBox.Show ("信件保存成功!");
  585. }
  586. break;
  587. case 3:
  588. btnAdd_Click();
  589. break;
  590. default:
  591. MessageBox.Show ("此功能还没有完善");
  592. break;
  593. }
  594. }
  595. private void menuItem7_Click(object sender, System.EventArgs e)
  596. {
  597. btnAdd_Click();
  598. }
  599. private void SendMailForm_Load(object sender, System.EventArgs e)
  600. {
  601. ltAttachment  .Visible =false;
  602. this.splitter1 .Visible =false;
  603. }
  604. //附件的"删除"操作
  605. private void menuItem10_Click(object sender, System.EventArgs e)
  606. {
  607. try
  608. {
  609. if(ltAttachment  .Items .Count >1)
  610. {
  611. //因为在添加一个附件的时候,刚好也是添加了一个ListView中的文件名
  612. //所以他们的索引值是相同的。所以可以通过选中的ListView的索引值来删除附件
  613. array.RemoveAt (ltAttachment.SelectedItems [0].Index );
  614. ltAttachment .Items .RemoveAt (ltAttachment  .SelectedIndices[0] );
  615. }
  616. else if(ltAttachment  .Items .Count ==1)
  617. {
  618. array.RemoveAt(ltAttachment .SelectedItems [0].Index);
  619. ltAttachment .Items .RemoveAt (ltAttachment  .SelectedIndices[0] );
  620. this.splitter1 .Visible =false;
  621. ltAttachment  .Visible =false;
  622. }
  623. }
  624. catch(Exception ex)
  625. {
  626. MessageBox.Show (ex.ToString ());
  627. }
  628. }
  629. private void menuItem9_Click_1(object sender, System.EventArgs e)
  630. {
  631. btnAdd_Click();
  632. }
  633. private void menuItem15_Click_1(object sender, System.EventArgs e)
  634. {
  635. ltAttachment  .View =View.LargeIcon ;
  636. }
  637. private void menuItem17_Click(object sender, System.EventArgs e)
  638. {
  639. ltAttachment  .View =View.SmallIcon ;
  640. }
  641. private void menuItem16_Click_1(object sender, System.EventArgs e)
  642. {
  643. ltAttachment  .View =View.List;
  644. }
  645. //"打开"操作
  646. private void menuItem12_Click(object sender, System.EventArgs e)
  647. {
  648. try
  649. {
  650. if (this.ltAttachment.SelectedItems[0].ToString() != "")
  651. {
  652. int length = this.ltAttachment.SelectedItems[0].ToString().Length;
  653. string ss = this.ltAttachment.SelectedItems[0].ToString().Substring(15, length - 16);
  654. ShellExecute(IntPtr.Zero, "Open", ss, null, null, 0);
  655. }
  656. }
  657. catch(Exception ex)
  658. {
  659. MessageBox.Show (ex.ToString ());
  660. }
  661. }
  662. //以下的两个事件是用来支持在添加附件时候的拖放操作的
  663. private void listView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
  664. {
  665. try
  666. {
  667. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  668. {
  669. string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
  670. for (int i = 0; i < files.Length; i++)
  671. {
  672. array.Add(files[i]);
  673. AddIcon(files[i]);
  674. }
  675. }
  676. }
  677. catch(Exception ex)
  678. {
  679. MessageBox.Show (ex.ToString ());
  680. }
  681. }
  682. private void listView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
  683. {
  684. try
  685. {
  686. if (e.Data.GetDataPresent(DataFormats.FileDrop))
  687. e.Effect = DragDropEffects.Copy;
  688. else
  689. e.Effect = DragDropEffects.None;
  690. }
  691. catch(Exception ex)
  692. {
  693. MessageBox.Show (ex.ToString ());
  694. }
  695. }
  696. }
  697. }