你需要添加命名空间: using System.Net.Mail;
                                   using System.Net.Mime;
                                   using System.Net;
                                   using System.IO;
      

      第一步,添加一个Form Load事件。在这个事件的代码里,主要实现窗体初始化的工作。这些代码同样可以放在Form的构造函数中完成。

        private void Form1_Load(object sender, EventArgs e)
        {
            //添加俩个smpt服务器的名称
            cmbBoxSMTP.Items.Add(“smtp.163.com”);
            cmbBoxSMTP.Items.Add(“smtp.gmail.com”);
            //设置为下拉列表
            cmbBoxSMTP.DropDownStyle = ComboBoxStyle.DropDownList;
            //默认选中第一个选项
            cmbBoxSMTP.SelectedIndex = 0;
            //在下面添加你想要初始化的内容,比如显示姓名、用户名等

        }

     第二步,添加一个”添加”按钮的单击事件。用来实现选择文件,并把文件添加到TreeView中。

        //添加按钮的单击事件
        private void btnAdd_Click(object sender, EventArgs e)
        {
            //定义并初始化一个OpenFileDialog类的对象
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.InitialDirectory = Application.StartupPath;
            openFile.FileName = “”;
            openFile.RestoreDirectory = true;
            openFile.Multiselect = false;

            //显示打开文件对话框,并判断是否单击了确定按钮
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                //得到选择的文件名
                string fileName = openFile.FileName;
                //将文件名添加到TreeView中
                treeViewFileList.Nodes.Add(fileName);
            }
        }

     第三步,添加一个”删除”按钮的单击事件,用来实现选中一个TreeView项后,把该选项删除掉。

        //删除按钮的单击事件
        private void btnDelete_Click(object sender, EventArgs e)
        {
            //判断是否选中了节点
            if (treeViewFileList.SelectedNode != null)
            {
                //得到选择的节点
                TreeNode tempNode = treeViewFileList.SelectedNode;
                //删除选中的节点
                treeViewFileList.Nodes.Remove(tempNode);
            }
            else
            {
                MessageBox.Show(“请选择要删除的附件。”);
            }
        }

     第四步,也是最关键的一步,”发送”按钮的单击事件,用来实现发送邮件功能。

        //发送按钮的单击事件
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //确定smtp服务器地址。实例化一个Smtp客户端
                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(cmbBoxSMTP.Text);
                //生成一个发送地址
                string strFrom = string.Empty;
                if (cmbBoxSMTP.SelectedText == “smtp.163.com”)
                    strFrom = txtUserName.Text + “@163.com”;
                else
                    strFrom = txtUserName.Text + “@gmail.com”;

                //构造一个发件人地址对象
                MailAddress from = new MailAddress(strFrom, txtDisplayName.Text, Encoding.UTF8);
                //构造一个收件人地址对象
                MailAddress to = new MailAddress(txtEmail.Text, txtToName.Text, Encoding.UTF8);

                //构造一个Email的Message对象
                MailMessage message = new MailMessage(from, to);

                //为 message 添加附件
                foreach (TreeNode treeNode in treeViewFileList.Nodes)
                {
                    //得到文件名
                    string fileName = treeNode.Text;
                    //判断文件是否存在
                    if (File.Exists(fileName))
                    {
                        //构造一个附件对象
                        Attachment attach = new Attachment(fileName);
                        //得到文件的信息
                        ContentDisposition disposition = attach.ContentDisposition;
                        disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
                        disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
                        disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
                        //向邮件添加附件
                        message.Attachments.Add(attach);
                    }
                    else
                    {
                        MessageBox.Show(“文件” + fileName + “未找到!”);
                    }
                }

                //添加邮件主题和内容
                message.Subject = txtSubject.Text;
                message.SubjectEncoding = Encoding.UTF8;
                message.Body = rtxtBody.Text;
                message.BodyEncoding = Encoding.UTF8;

                //设置邮件的信息
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml = false;

                //如果服务器支持安全连接,则将安全连接设为true。
                //gmail支持,163不支持,如果是gmail则一定要将其设为true
                if (cmbBoxSMTP.SelectedText == “smpt.163.com”)
                    client.EnableSsl = false;
                else
                    client.EnableSsl = true;

                //设置用户名和密码。
                //string userState = message.Subject;
                client.UseDefaultCredentials = false;
                string username = txtUserName.Text;
                string passwd = txtPassword.Text;
                //用户登陆信息
                NetworkCredential myCredentials = new NetworkCredential(username, passwd);
                client.Credentials = myCredentials;
                //发送邮件
                client.Send(message);
                //提示发送成功
                MessageBox.Show(“发送成功!”);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


 

下面是我的运行结果:

图2、发送Email

现在打开我的Gmail邮箱,看看是不是已经收到的邮件,我果然收到了,不信你看:

图3、收件箱查看邮件


 

     看到了吧,就是这么简单,赶紧动手自己做一个!最后需要说明一点,这里用的是Send方法进行发送的,你也可以使用SendAsync方法来发送邮件,如果采用了异步,你就需要设置一个回调函数来处理发送完成以后的工作.