2011-04-07 71 views
0
public void Main() 
    { 
     timer1.Interval = 10000; 
     timer1.Elapsed += new ElapsedEventHandler(SaatMail); 
     timer1.Start(); 

    } 


public void SaatMail(object sender, ElapsedEventArgs e) 
{ 

     timer1.Stop(); 

     //kayıt yapılan en son değeri dataread yardımıyla çekerek, mail gonder servisine gönderiyor. 
     SqlConnection baglanti = new SqlConnection(@""); 
     SqlCommand komut = new SqlCommand("select OtoMailID,Adet,BasTarih,Aciklama,Kime from OtoMail where Sonuc=0 ", baglanti); 
     baglanti.Open(); 
     SqlDataAdapter da = new SqlDataAdapter(); 
     //komut.ExecuteNonQuery(); 
     da.SelectCommand = komut; 
     DataTable dt=new DataTable(); 

     da.Fill(dt); 


    foreach(DataRow dr in dt.Rows) 
    { 


     Sayi = Convert.ToInt32(dr["Adet"]); 
     Kime = (dr["Kime"].ToString()); 
     BasTarih = Convert.ToDateTime(dr["BasTarih"]); 
     OtoMailID = Convert.ToInt32(dr["OtoMailID"]); 
     Aciklama = (dr["Aciklama"].ToString()); 


      SqlConnection baglanti5 = new SqlConnection(@""); 
      SqlCommand ekle = new SqlCommand("update OtoMail set [email protected] where [email protected] ", baglanti5); 
      baglanti5.Open(); 
      mailgonder(Sayi, Kime, islemdeger, Aciklama); 
      ekle.Parameters.AddWithValue("@Sonuc", 1); 
      ekle.Parameters.AddWithValue("@OtoMailID", OtoMailID); 
      ekle.ExecuteNonQuery(); 

      islemdeger = 1; 


      baglanti5.Close(); 
      baglanti5.Dispose(); 
      ekle.Dispose(); 

    } 
    timer1.Start(); 

} 

private void mailgonder(int Sayi, string Kime, int islemdeger, string Aciklama) 
{ 

    MailMessage mvMail = new MailMessage(); 
    mvMail.To = Kime; 
    mvMail.From = "[email protected]"; 
    mvMail.Body = Aciklama; 
    mvMail.Subject = "OTOMATİK BİLGİLENDİRME MAİL"; 
    SmtpMail.SmtpServer = "mailserver.info.com"; 

    int i = 0; 

    while (i < Sayi) 
    { 
     SmtpMail.Send(mvMail); 
     i++; 
    } 

} 

上面的函數很好的工作。但是,我想一個一個地發送郵件。例如,我想發送兩個郵件10秒的間隔。以上功能發送郵件,但連續發送。我該怎麼辦?郵件發送問題

在此先感謝。

乾杯,

+0

有什麼建議? – Arbelac 2011-04-07 15:24:57

回答

1

,使用睡眠功能(僞代碼):

while (i < Sayi) 
{ 
    SmtpMail.Send(mvMail); 
    i++; 
    if (i%2) //send two mail wait 10 sec and then restart 
     Sleep(10000); 
} 

我%2種手段:我MOD 2