SendMailForm.cs
资源名称:yjal.rar [点击查看]
上传用户:shjujing
上传日期:2022-07-28
资源大小:11244k
文件大小:25k
源码类别:
Email客户端
开发平台:
Visual C++
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Net ;
- using System.Net .Sockets ;
- using MailSend;
- using System.Data ;
- using System.Runtime .InteropServices ;
- namespace MailManageSystem
- {
- /// <summary>
- /// Send 的摘要说明。
- /// </summary>
- public class SendMailForm : System.Windows.Forms.Form
- {
- private System.Windows.Forms.MainMenu mainMenu1;
- private System.Windows.Forms.MenuItem menuItem1;
- private System.Windows.Forms.MenuItem menuItem2;
- private System.Windows.Forms.MenuItem menuItem3;
- private System.Windows.Forms.MenuItem menuItem4;
- private System.Windows.Forms.MenuItem menuItem5;
- private System.Windows.Forms.MenuItem menuItem6;
- private System.Windows.Forms.MenuItem menuItem7;
- private System.Windows.Forms.MenuItem menuItem8;
- private System.ComponentModel.IContainer components;
- private System.Windows.Forms.OpenFileDialog openFileDialog1;//语言编码
- private System.Windows.Forms.ToolBar toolBar1;
- private System.Windows.Forms.ToolBarButton tbSend;
- private System.Windows.Forms.ToolBarButton tbSave;
- private System.Windows.Forms.ToolBarButton toolBarButton1;
- private System.Windows.Forms.ToolBarButton tbAttachment;
- private System.Windows.Forms.SaveFileDialog saveFileDialog1;
- private System.Windows.Forms.ImageList imlSendMail;
- private System.Windows.Forms.ImageList imlAttachment;
- private System.Windows.Forms.ContextMenu conAttachment;
- private System.Windows.Forms.MenuItem menuItem9;
- private System.Windows.Forms.MenuItem menuItem10;
- private System.Windows.Forms.MenuItem menuItem11;
- private System.Windows.Forms.MenuItem menuItem12;
- private System.Windows.Forms.MenuItem menuItem13;
- private System.Windows.Forms.MenuItem menuItem14;
- private System.Windows.Forms.MenuItem menuItem15;
- private System.Windows.Forms.MenuItem menuItem16;
- private System.Windows.Forms.MenuItem menuItem17;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.TextBox txtTo;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.TextBox txtSubject;
- private System.Windows.Forms.Splitter splitter1;
- private System.Windows.Forms.Panel panel1;
- private System.Windows.Forms.RichTextBox txtMessage;
- private ArrayList array=new ArrayList ();
- private System.Windows.Forms.ListView ltAttachment;
- private System.Windows.Forms.ComboBox cbHtml;
- int i = 0;
- //EntryPoint 给出Dll入口点的名称,如果没有给出,则用方法本身的名称。
- [DllImport("shell32.dll",EntryPoint="ShellExecute")]
- private static extern int ShellExecute(IntPtr hwnd, string lpOperation,string lpFile, string lpParameters, string lpDirectory, int nShowCmd);
- //函数SHGetFileInfo()的返回值也随uFlags的取值变化而有所不同。
- //通过调用SHGetFileInfo()可以由psfi参数得到文件的图标句柄,但要注意在uFlags参数中不使用SHGFI_PIDL时,SHGetFileInfo()不能获得"我的电脑"等虚似文件夹的信息。
- [DllImport("Shell32.dll")]
- private static extern int SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, SHGFI uFlags);
- [StructLayout(LayoutKind.Sequential)]
- private struct SHFILEINFO
- {
- //重载一个构造函数
- public SHFILEINFO(bool b)
- {
- hIcon = IntPtr.Zero ; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
- }
- public IntPtr hIcon;
- public int iIcon;
- public uint dwAttributes;
- [MarshalAs(UnmanagedType.LPStr, SizeConst =256)]
- public string szDisplayName;
- [MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
- public string szTypeName;
- };
- //图标类型的结构体
- private enum SHGFI
- {
- SHGFI_ICON =0x000000100, // get icon
- SHGFI_DISPLAYNAME =0x000000200, // get display name
- SHGFI_TYPENAME =0x000000400, // get type name
- SHGFI_ATTRIBUTES =0x000000800, // get attributes
- SHGFI_ICONLOCATION =0x000001000, // get icon location
- SHGFI_EXETYPE =0x000002000, // return exe type
- SHGFI_SYSICONINDEX =0x000004000, // get system icon index
- SHGFI_LINKOVERLAY =0x000008000, // put a link overlay on icon
- SHGFI_SELECTED =0x000010000, // show icon in selected state
- SHGFI_ATTR_SPECIFIED =0x000020000, // get only specified attributes
- SHGFI_LARGEICON =0x000000000, // get large icon
- SHGFI_SMALLICON =0x000000001, // get small icon
- SHGFI_OPENICON =0x000000002, // get open icon
- SHGFI_SHELLICONSIZE =0x000000004, // get shell size icon
- SHGFI_PIDL =0x000000008, // pszPath is a pidl
- SHGFI_USEFILEATTRIBUTES =0x000000010, // use passed dwFileAttribute
- SHGFI_ADDOVERLAYS =0x000000020, // apply the appropriate overlays
- SHGFI_OVERLAYINDEX =0x000000040 // Get the index of the overlay
- }
- /// <summary>
- /// 调用获的程序图标句柄的函数
- /// </summary>
- /// <param name="strPath">选定程序的绝对路径</param>
- /// <returns></returns>
- private static Icon GetIcon(string strPath)
- {
- try
- {
- SHFILEINFO info = new SHFILEINFO();
- int cbFileInfo = Marshal.SizeOf(info);
- SHGFI flags;
- flags = SHGFI.SHGFI_ICON|SHGFI.SHGFI_LARGEICON|SHGFI.SHGFI_ATTRIBUTES|SHGFI.SHGFI_EXETYPE;
- SHGetFileInfo(strPath, 32|0, ref info,(uint)cbFileInfo,flags);
- return Icon.FromHandle(info.hIcon);
- }
- catch(Exception ex)
- {
- MessageBox.Show (ex.ToString ());
- return null;
- }
- }
- public SendMailForm()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.components = new System.ComponentModel.Container();
- System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(SendMailForm));
- this.mainMenu1 = new System.Windows.Forms.MainMenu();
- this.menuItem1 = new System.Windows.Forms.MenuItem();
- this.menuItem2 = new System.Windows.Forms.MenuItem();
- this.menuItem3 = new System.Windows.Forms.MenuItem();
- this.menuItem8 = new System.Windows.Forms.MenuItem();
- this.menuItem4 = new System.Windows.Forms.MenuItem();
- this.menuItem5 = new System.Windows.Forms.MenuItem();
- this.menuItem6 = new System.Windows.Forms.MenuItem();
- this.menuItem7 = new System.Windows.Forms.MenuItem();
- this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
- this.toolBar1 = new System.Windows.Forms.ToolBar();
- this.tbSend = new System.Windows.Forms.ToolBarButton();
- this.tbSave = new System.Windows.Forms.ToolBarButton();
- this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
- this.tbAttachment = new System.Windows.Forms.ToolBarButton();
- this.imlSendMail = new System.Windows.Forms.ImageList(this.components);
- this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
- this.imlAttachment = new System.Windows.Forms.ImageList(this.components);
- this.conAttachment = new System.Windows.Forms.ContextMenu();
- this.menuItem9 = new System.Windows.Forms.MenuItem();
- this.menuItem10 = new System.Windows.Forms.MenuItem();
- this.menuItem11 = new System.Windows.Forms.MenuItem();
- this.menuItem12 = new System.Windows.Forms.MenuItem();
- this.menuItem13 = new System.Windows.Forms.MenuItem();
- this.menuItem14 = new System.Windows.Forms.MenuItem();
- this.menuItem15 = new System.Windows.Forms.MenuItem();
- this.menuItem17 = new System.Windows.Forms.MenuItem();
- this.menuItem16 = new System.Windows.Forms.MenuItem();
- this.groupBox2 = new System.Windows.Forms.GroupBox();
- this.cbHtml = new System.Windows.Forms.ComboBox();
- this.txtTo = new System.Windows.Forms.TextBox();
- this.label3 = new System.Windows.Forms.Label();
- this.label4 = new System.Windows.Forms.Label();
- this.txtSubject = new System.Windows.Forms.TextBox();
- this.ltAttachment = new System.Windows.Forms.ListView();
- this.splitter1 = new System.Windows.Forms.Splitter();
- this.panel1 = new System.Windows.Forms.Panel();
- this.txtMessage = new System.Windows.Forms.RichTextBox();
- this.groupBox2.SuspendLayout();
- this.panel1.SuspendLayout();
- this.SuspendLayout();
- //
- // mainMenu1
- //
- this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem1,
- this.menuItem5,
- this.menuItem6});
- //
- // menuItem1
- //
- this.menuItem1.Index = 0;
- this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem2,
- this.menuItem3,
- this.menuItem8,
- this.menuItem4});
- this.menuItem1.Text = "邮件(M)";
- //
- // menuItem2
- //
- this.menuItem2.Index = 0;
- this.menuItem2.Text = "立即发送";
- this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
- //
- // menuItem3
- //
- this.menuItem3.Index = 1;
- this.menuItem3.Text = "草稿";
- //
- // menuItem8
- //
- this.menuItem8.Index = 2;
- this.menuItem8.Text = "-";
- //
- // menuItem4
- //
- this.menuItem4.Index = 3;
- this.menuItem4.Text = "退出";
- this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
- //
- // menuItem5
- //
- this.menuItem5.Index = 1;
- this.menuItem5.Text = "";
- //
- // menuItem6
- //
- this.menuItem6.Index = 2;
- this.menuItem6.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem7});
- this.menuItem6.Text = "插入(I)";
- //
- // menuItem7
- //
- this.menuItem7.Index = 0;
- this.menuItem7.Text = "增加附件";
- this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
- //
- // toolBar1
- //
- this.toolBar1.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
- this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
- this.tbSend,
- this.tbSave,
- this.toolBarButton1,
- this.tbAttachment});
- this.toolBar1.ButtonSize = new System.Drawing.Size(100, 80);
- this.toolBar1.DropDownArrows = true;
- this.toolBar1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
- this.toolBar1.ImageList = this.imlSendMail;
- this.toolBar1.Location = new System.Drawing.Point(0, 0);
- this.toolBar1.Name = "toolBar1";
- this.toolBar1.ShowToolTips = true;
- this.toolBar1.Size = new System.Drawing.Size(800, 41);
- this.toolBar1.TabIndex = 3;
- this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
- //
- // tbSend
- //
- this.tbSend.ImageIndex = 0;
- this.tbSend.Text = "发送";
- //
- // tbSave
- //
- this.tbSave.ImageIndex = 1;
- this.tbSave.Text = "保存";
- //
- // toolBarButton1
- //
- this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
- //
- // tbAttachment
- //
- this.tbAttachment.ImageIndex = 2;
- this.tbAttachment.Text = "附件";
- //
- // imlSendMail
- //
- this.imlSendMail.ImageSize = new System.Drawing.Size(16, 16);
- this.imlSendMail.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imlSendMail.ImageStream")));
- this.imlSendMail.TransparentColor = System.Drawing.Color.Transparent;
- //
- // imlAttachment
- //
- this.imlAttachment.ImageSize = new System.Drawing.Size(16, 16);
- this.imlAttachment.TransparentColor = System.Drawing.Color.Transparent;
- //
- // conAttachment
- //
- this.conAttachment.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem9,
- this.menuItem10,
- this.menuItem11,
- this.menuItem12,
- this.menuItem13,
- this.menuItem14});
- //
- // menuItem9
- //
- this.menuItem9.Index = 0;
- this.menuItem9.Shortcut = System.Windows.Forms.Shortcut.Alt0;
- this.menuItem9.Text = "增加(A)...";
- this.menuItem9.Click += new System.EventHandler(this.menuItem9_Click_1);
- //
- // menuItem10
- //
- this.menuItem10.Index = 1;
- this.menuItem10.Shortcut = System.Windows.Forms.Shortcut.Alt1;
- this.menuItem10.Text = "删除(D)...";
- this.menuItem10.Click += new System.EventHandler(this.menuItem10_Click);
- //
- // menuItem11
- //
- this.menuItem11.Index = 2;
- this.menuItem11.Text = "-";
- //
- // menuItem12
- //
- this.menuItem12.Index = 3;
- this.menuItem12.Shortcut = System.Windows.Forms.Shortcut.Alt2;
- this.menuItem12.Text = "打开(O)...";
- this.menuItem12.Click += new System.EventHandler(this.menuItem12_Click);
- //
- // menuItem13
- //
- this.menuItem13.Index = 4;
- this.menuItem13.Text = "-";
- //
- // menuItem14
- //
- this.menuItem14.Index = 5;
- this.menuItem14.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
- this.menuItem15,
- this.menuItem17,
- this.menuItem16});
- this.menuItem14.Text = "排列图标(V)...";
- //
- // menuItem15
- //
- this.menuItem15.Index = 0;
- this.menuItem15.Text = "大图标";
- this.menuItem15.Click += new System.EventHandler(this.menuItem15_Click_1);
- //
- // menuItem17
- //
- this.menuItem17.Index = 1;
- this.menuItem17.Text = "小图标";
- this.menuItem17.Click += new System.EventHandler(this.menuItem17_Click);
- //
- // menuItem16
- //
- this.menuItem16.Index = 2;
- this.menuItem16.Text = "列表";
- this.menuItem16.Click += new System.EventHandler(this.menuItem16_Click_1);
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.cbHtml);
- this.groupBox2.Controls.Add(this.txtTo);
- this.groupBox2.Controls.Add(this.label3);
- this.groupBox2.Controls.Add(this.label4);
- this.groupBox2.Controls.Add(this.txtSubject);
- this.groupBox2.Dock = System.Windows.Forms.DockStyle.Top;
- this.groupBox2.FlatStyle = System.Windows.Forms.FlatStyle.System;
- this.groupBox2.Location = new System.Drawing.Point(0, 41);
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.Size = new System.Drawing.Size(800, 104);
- this.groupBox2.TabIndex = 4;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "邮件头";
- //
- // cbHtml
- //
- this.cbHtml.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
- this.cbHtml.Items.AddRange(new object[] {
- "HTML邮件",
- "纯文本邮件"});
- this.cbHtml.Location = new System.Drawing.Point(704, 72);
- this.cbHtml.Name = "cbHtml";
- this.cbHtml.Size = new System.Drawing.Size(88, 20);
- this.cbHtml.TabIndex = 8;
- this.cbHtml.Text = "HTML邮件";
- //
- // txtTo
- //
- this.txtTo.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.txtTo.Location = new System.Drawing.Point(88, 72);
- this.txtTo.Name = "txtTo";
- this.txtTo.Size = new System.Drawing.Size(584, 21);
- this.txtTo.TabIndex = 6;
- this.txtTo.Text = "yinhu82@163.com,yinhu82@sina.com.cn";
- //
- // label3
- //
- this.label3.Location = new System.Drawing.Point(24, 72);
- this.label3.Name = "label3";
- this.label3.Size = new System.Drawing.Size(48, 23);
- this.label3.TabIndex = 5;
- this.label3.Text = "收件人:";
- this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // label4
- //
- this.label4.Location = new System.Drawing.Point(16, 32);
- this.label4.Name = "label4";
- this.label4.Size = new System.Drawing.Size(64, 23);
- this.label4.TabIndex = 4;
- this.label4.Text = "邮件主题:";
- this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
- //
- // txtSubject
- //
- this.txtSubject.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
- | System.Windows.Forms.AnchorStyles.Left)
- | System.Windows.Forms.AnchorStyles.Right)));
- this.txtSubject.Location = new System.Drawing.Point(88, 32);
- this.txtSubject.Name = "txtSubject";
- this.txtSubject.Size = new System.Drawing.Size(664, 21);
- this.txtSubject.TabIndex = 7;
- this.txtSubject.Text = "最新版";
- //
- // ltAttachment
- //
- this.ltAttachment.AllowDrop = true;
- this.ltAttachment.ContextMenu = this.conAttachment;
- this.ltAttachment.Dock = System.Windows.Forms.DockStyle.Bottom;
- this.ltAttachment.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
- this.ltAttachment.LargeImageList = this.imlAttachment;
- this.ltAttachment.Location = new System.Drawing.Point(0, 392);
- this.ltAttachment.Name = "ltAttachment";
- this.ltAttachment.Size = new System.Drawing.Size(800, 97);
- this.ltAttachment.TabIndex = 5;
- this.ltAttachment.DragDrop += new System.Windows.Forms.DragEventHandler(this.listView1_DragDrop);
- this.ltAttachment.DragEnter += new System.Windows.Forms.DragEventHandler(this.listView1_DragEnter);
- //
- // splitter1
- //
- this.splitter1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
- this.splitter1.Dock = System.Windows.Forms.DockStyle.Bottom;
- this.splitter1.Location = new System.Drawing.Point(0, 388);
- this.splitter1.Name = "splitter1";
- this.splitter1.Size = new System.Drawing.Size(800, 4);
- this.splitter1.TabIndex = 6;
- this.splitter1.TabStop = false;
- //
- // panel1
- //
- this.panel1.Controls.Add(this.txtMessage);
- this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
- this.panel1.Location = new System.Drawing.Point(0, 145);
- this.panel1.Name = "panel1";
- this.panel1.Size = new System.Drawing.Size(800, 243);
- this.panel1.TabIndex = 7;
- //
- // txtMessage
- //
- this.txtMessage.Dock = System.Windows.Forms.DockStyle.Fill;
- this.txtMessage.Location = new System.Drawing.Point(0, 0);
- this.txtMessage.Name = "txtMessage";
- this.txtMessage.Size = new System.Drawing.Size(800, 243);
- this.txtMessage.TabIndex = 0;
- this.txtMessage.Text = "呵呵 这是第一封试验邮件";
- //
- // SendMailForm
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(800, 489);
- this.Controls.Add(this.panel1);
- this.Controls.Add(this.splitter1);
- this.Controls.Add(this.ltAttachment);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.toolBar1);
- this.Menu = this.mainMenu1;
- this.Name = "SendMailForm";
- this.Text = "Send";
- this.Load += new System.EventHandler(this.SendMailForm_Load);
- this.groupBox2.ResumeLayout(false);
- this.panel1.ResumeLayout(false);
- this.ResumeLayout(false);
- }
- #endregion
- //发送邮件
- private void menuItem2_Click(object sender, System.EventArgs e)
- {
- bool html=false;
- if(this.cbHtml .Text=="HTML邮件"|this.cbHtml .Text =="纯文本邮件")
- {
- if(this.cbHtml .Text=="HTML邮件")
- html=true;
- else if(this.cbHtml .Text=="纯文本邮件")
- html=false;
- }
- else
- {
- MessageBox.Show ("请做出有效的邮件格式选择");
- this.cbHtml .Text ="HTML邮件";
- return;
- }
- SmtpMail send=new SmtpMail (array);
- Cursor.Current =Cursors.WaitCursor ;
- send.Charset ="GB2312";
- send.Priority ="Normal";
- send.eSmtp=true;
- send.MailServerUserName =SystemManage.UserName ;
- send.MailServerPassWord =SystemManage.PassWd ;
- send.Send (SystemManage.SmtpName,SystemManage.MailAddress ,SystemManage.AccountName,this.txtTo .Text ,html,this.txtSubject .Text ,this.txtMessage .Text );
- Cursor.Current =Cursors.Default ;
- SystemManage.letterNumber ++;
- ltAttachment .Items .Clear ();
- this.Close ();
- }
- private void menuItem4_Click(object sender, System.EventArgs e)
- {
- this.Close ();
- }
- //添加附件名称和图标的函数
- private void btnAdd_Click()
- {
- if(this.openFileDialog1 .ShowDialog()==DialogResult.OK)
- {
- array.Add (this.openFileDialog1 .FileName );
- AddIcon(this.openFileDialog1.FileName);
- ltAttachment .Visible =true;
- this.splitter1 .Visible =true;
- }
- }
- /// <summary>
- /// 判断加入的文件格式,并添加到附件ListView中
- /// </summary>
- /// <param name="fileType"></param>
- private void AddIcon(string fileType)
- {
- //string type = fileType.Substring(fileType.Length - 3);
- try
- {
- string Name = fileType.Substring(fileType.LastIndexOf(@"") + 1);
- this.imlAttachment.Images.Add(GetIcon(fileType));
- this.ltAttachment.LargeImageList = this.imlAttachment;
- this.ltAttachment .SmallImageList =this.imlAttachment ;
- this.ltAttachment .Items .Add (Name,i++);
- }
- catch(Exception ex)
- {
- MessageBox.Show(ex.ToString ());
- }
- }
- private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
- {
- switch(this.toolBar1 .Buttons.IndexOf (e.Button ))
- {
- case 0:
- menuItem2_Click(null,null);
- break;
- case 1:
- this.saveFileDialog1.Title ="保存信件";
- this.saveFileDialog1 .Filter ="文本文件(*.txt)|*.txt|All Files(*.*)|*.*";
- AddExtra extra=new AddExtra( array);
- 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";
- if(this.saveFileDialog1 .ShowDialog ()==DialogResult.OK )
- {
- extra.SaveLetter (this.saveFileDialog1 .FileName,info);
- MessageBox.Show ("信件保存成功!");
- }
- break;
- case 3:
- btnAdd_Click();
- break;
- default:
- MessageBox.Show ("此功能还没有完善");
- break;
- }
- }
- private void menuItem7_Click(object sender, System.EventArgs e)
- {
- btnAdd_Click();
- }
- private void SendMailForm_Load(object sender, System.EventArgs e)
- {
- ltAttachment .Visible =false;
- this.splitter1 .Visible =false;
- }
- //附件的"删除"操作
- private void menuItem10_Click(object sender, System.EventArgs e)
- {
- try
- {
- if(ltAttachment .Items .Count >1)
- {
- //因为在添加一个附件的时候,刚好也是添加了一个ListView中的文件名
- //所以他们的索引值是相同的。所以可以通过选中的ListView的索引值来删除附件
- array.RemoveAt (ltAttachment.SelectedItems [0].Index );
- ltAttachment .Items .RemoveAt (ltAttachment .SelectedIndices[0] );
- }
- else if(ltAttachment .Items .Count ==1)
- {
- array.RemoveAt(ltAttachment .SelectedItems [0].Index);
- ltAttachment .Items .RemoveAt (ltAttachment .SelectedIndices[0] );
- this.splitter1 .Visible =false;
- ltAttachment .Visible =false;
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show (ex.ToString ());
- }
- }
- private void menuItem9_Click_1(object sender, System.EventArgs e)
- {
- btnAdd_Click();
- }
- private void menuItem15_Click_1(object sender, System.EventArgs e)
- {
- ltAttachment .View =View.LargeIcon ;
- }
- private void menuItem17_Click(object sender, System.EventArgs e)
- {
- ltAttachment .View =View.SmallIcon ;
- }
- private void menuItem16_Click_1(object sender, System.EventArgs e)
- {
- ltAttachment .View =View.List;
- }
- //"打开"操作
- private void menuItem12_Click(object sender, System.EventArgs e)
- {
- try
- {
- if (this.ltAttachment.SelectedItems[0].ToString() != "")
- {
- int length = this.ltAttachment.SelectedItems[0].ToString().Length;
- string ss = this.ltAttachment.SelectedItems[0].ToString().Substring(15, length - 16);
- ShellExecute(IntPtr.Zero, "Open", ss, null, null, 0);
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show (ex.ToString ());
- }
- }
- //以下的两个事件是用来支持在添加附件时候的拖放操作的
- private void listView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
- {
- try
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop))
- {
- string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
- for (int i = 0; i < files.Length; i++)
- {
- array.Add(files[i]);
- AddIcon(files[i]);
- }
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show (ex.ToString ());
- }
- }
- private void listView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
- {
- try
- {
- if (e.Data.GetDataPresent(DataFormats.FileDrop))
- e.Effect = DragDropEffects.Copy;
- else
- e.Effect = DragDropEffects.None;
- }
- catch(Exception ex)
- {
- MessageBox.Show (ex.ToString ());
- }
- }
- }
- }