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 Sonuc=@Sonuc where OtoMailID=@OtoMailID ", 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 = "info@info.com";
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 :(得分:1)
使用睡眠功能(伪代码):
while (i < Sayi)
{
SmtpMail.Send(mvMail);
i++;
if (i%2) //send two mail wait 10 sec and then restart
Sleep(10000);
}
i%2表示:i mod 2