概要:
         子线程工作,发送成功邮件做标记,逐条提取数据,气泡提示信息,最小化到托盘,实时进度条
 
代码:
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Threading;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;
 
namespace MailGroupSends
{
    public partial class Form1 : Form
    {

        private string subject = string.Empty, mailto = string.Empty, body = string.Empty;

        private int sucNum = 0;

        private int TotalDate=0;

        private string ConnString

        {
            get
            {

                string pathFull = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

                string path = pathFull.Substring(0, pathFull.LastIndexOf(@"\"));

                return @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @"\mail.mdb";

            }
        }

        private bool mailSent = false; //邮件是否发送成功

        private int mailTotalCount = 0;

        private int CategoryId = 0;

        private int SentCount = 0; //已发送数

        private int UnitConversion = 1; //默认为秒

       
        /// 发送间隔

        private int Interval

        {
            get
            {

                int timer = 0;

                int totalMis = (TotalDate * UnitConversion * 1000);

                timer = totalMis / (mailTotalCount-SentCount);

                return timer;

            }
        }

private void Form1_Load(object sender, EventArgs e)

        {

            Control.CheckForIllegalCrossThreadCalls = false;

            InitMailCategory();
            SentMailCount();
            LoadMailList();
            ClearTxt(txtAnnexPath);
           InitTime();
        }
/// 发送

        private void btnSendMail_Click(object sender, EventArgs e)

        {
            TotalDate = Convert.ToInt32(this.txtDateCount.Text.Trim());
            ControlEnabled(false);
            subject = this.txtSubject.Text.Trim();
            body = this.rtxtBody.Text.Trim();

            this.progressBar1.Visible = true;

            this.lblProgress.Visible = true;

            //用子线程工作
           new Thread(new ThreadStart(StartMailSend)).Start();
        }
/// 邮件发送

        private bool Send(string mailTo, string subject, string body)

        {
            try
            {

                if (!IsEmail(mailTo)) //邮箱格式验证

                {

                    WriteToTxt(DateTime.Now + "   Faild      " + mailTo, txtLogPath + "   Error Message:邮箱格式不正确");

                    MarkErrorMail(mailTo);

                    return false;

                }

                MailMessage msg = new MailMessage();

                msg.From = new MailAddress("***@****", "xiaoyaosr", Encoding.UTF8);

                if (mailTo.IndexOf(",") > -1)

                    msg.Bcc.Add(mailTo);
                else
                    msg.To.Add(mailTo);
                msg.Subject = subject;
                msg.Body = body;
 

                ArrayList annexList = ReadTxt(txtAnnexPath);

                for (int i = 0; i < annexList.Count; i++)

                {
                    msg.Attachments.Add(new Attachment(annexList[i].ToString()));
                }
 

                SmtpClient smtp = new SmtpClient("mail.163.com");

                smtp.Credentials = new NetworkCredential("***@****", "*******");
                smtp.Send(msg);
                mailSent = true;
                sucNum++;
            }

            catch (Exception ex)

            {

                WriteToTxt(DateTime.Now + "   Faild   Error Message:" + ex.Message, txtLogPath);

                this.notifyIcon1.ShowBalloonTip(Interval, "", ex.Message, ToolTipIcon.None);

                System.Threading.Thread.Sleep(Interval * 2);
                KillThread();
                MarkErrorMail(mailTo);
                mailSent = false;
            }

            return mailSent;

        }
 
//开始发送
        public void StartMailSend()
        {

            MailSend mailSend = new MailSend();

            mailSend.iTotalCount = mailTotalCount;

            mailSend.interval = Interval;
            mailSend.iSentCount = SentCount;
            mailSend.onMailSendProgress += new MailSend.dMailSendProgress(mailSend_onMailSendProgress);
            mailSend.Start();
        }
 
        //同步更新

        void mailSend_onMailSendProgress(int total, int current)

        {
            try
            {

                if (this.InvokeRequired)

                {

                    this.Invoke(new MailSend.dMailSendProgress(mailSend_onMailSendProgress), new object[] { total, current });

                }
                else
                {
                    InitMailList();

                    string percent = (((current + 1) *100) / mailTotalCount).ToString() + "%";

                    string progress = percent + " " + (current + 1) + "/" + mailTotalCount;

                    this.lableTimer.Text = progress;

                    this.notifyIcon1.ShowBalloonTip(Interval,"", progress,ToolTipIcon.None);//气泡提示