一个线程群发邮件

public bool run = false;
        public int count = 0;    //发送次数
        private static bool issuccess(string[] mailTo, string mailSubject, string mailBody)
        {
            string mailFrom = System.Configuration.ConfigurationSettings.AppSettings["config_mail"];
            string smtpServer = System.Configuration.ConfigurationSettings.AppSettings["config_smtp"];
            string userName = System.Configuration.ConfigurationSettings.AppSettings["config_mailname"];
            string password = System.Configuration.ConfigurationSettings.AppSettings["config_mailpassword"];

            try
            {
                SMTP MyStmp = new SMTP(mailFrom, mailTo, mailSubject, mailBody, smtpServer, userName, password);
                MyStmp.Send();
              
                return true;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message.ToString());
                return false;
            }
        }
        Thread myAttack;
        private void button1_Click(object sender, EventArgs e)
        {

            //开启线程来实现
             myAttack = new Thread(new ThreadStart(this.SendEmail));
            myAttack.Start();
           
        }

        private void SendEmail()
        {
            run = true;
            while (run)
            {
                toolStripStatusLabel1.Text="开始发邮件。。。。。。";
    
                
                string mailSubject = textBox1.Text.ToString();
                string mailBody = textBox2.Text.ToString();

                string dbpath = Application.StartupPath   @"\EmailDatabase\UserEmail.mdb";
                OleDbConnection conn = DbOperate.ConnOpen(dbpath);
                conn.Open();
                string sql = "select UserEmail from email";
                OleDbDataReader dr = DbOperate.Dr(sql, conn);
                string UserEmail;
                while (dr.Read())
                {
                    UserEmail = dr["UserEmail"].ToString();
                    string[] mailTo ={ UserEmail };
                   if (issuccess(mailTo, mailSubject, mailBody))
                    {
                        count ;
                        toolStripStatusLabel1.Text = "第"   count   "邮件发送成功,停10秒继续发邮件。。。。。。";
                    }
                    else
                    {
                        count ;
                        toolStripStatusLabel1.Text = "第"   count   "邮件发送失败,停10秒继续发邮件。。。。。。";
                    }
                    System.Threading.Thread.Sleep(10000);
                }

                toolStripStatusLabel1.Text = "全部邮件发送完毕。。。。。。"   count;
                run = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            run = false;
            myAttack.Abort();
            toolStripStatusLabel1.Text = "停止发邮件。。。。。。";
        }

posted @ 2008-10-23 10:42  Landy_di  阅读(249)  评论(0编辑  收藏  举报