semye-静心

积累,点点滴滴
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

群发邮件太慢了!

Posted on 2007-07-26 18:04  semye  阅读(1070)  评论(4编辑  收藏  举报
我用以下方法群发邮件,测试时发了600封,费时1小时,这个速度也太慢了,有没其它方法?
 1        delegate void dsend(); 
 2        private void Button1_Click(object sender, System.EventArgs e)
 3        {
 4            dsend ds = new dsend(Exec);
 5            ds.BeginInvoke(null,null);
 6        }

 7        private void Exec()
 8        {
 9            for(int i=0;i<2000;i++)
10            {
11                SentEmail(i);
12            }

13        }

14        
15        public void SentEmail(int i)
16        {
17            
18            MailMessage msg = new MailMessage();
19            msg.From = "semye@airprice.com";
20            msg.To = "semye@airprice.com";
21            msg.Subject = "测试邮件群发";
22            msg.Body = System.DateTime.Now.ToString();
23            msg.BodyFormat = MailFormat.Html;
24            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate""1" ); 
25            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername",userName);
26            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
27            msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);
28            //msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "1");
29            SmtpMail.SmtpServer = "www.airprice.com";            
30            for(int j =0;;j++)
31            {
32                try
33                {
34                    SmtpMail.Send(msg);
35                    msg = null;                    
36                    WriteDaily(i,1,"",j);//写日志
37                    break;
38                }

39                catch(Exception ee)
40                {
41                    msg = null;
42                    if(j>1)
43                    {
44                        string a = ee.Message;
45                        WriteDaily(i,0,a,j);//写日志
46                        break;
47                    }

48                    else
49                    {
50                        Thread.Sleep(30000);
51                    }

52                
53                }

54            }

55        }

56        private void WriteDaily(int nb,int result,string error,int j)
57        {
58            string strconn = "server=.;uid=sa;pwd=;database=mytos";
59            string sql = "insert into SendMessage values("+nb+","+result+",'"+error+"','"+System.DateTime.Now+"',"+j+")";
60            SqlConnection conn = new  SqlConnection(strconn);
61            SqlCommand cmd  = new SqlCommand(sql,conn);
62            int i = 0;
63            try
64            {
65                conn.Open();
66                i=cmd.ExecuteNonQuery();
67                conn.Close();
68            }

69            catch(Exception ee)
70            {
71                string a = ee.Message;
72                conn.Close();
73            }

74        }