我有一个场景,我已经完成了一些工作但是为了我的进一步过程,我需要一些帮助。
我有一个导入到网格视图的ExcelSheet,并假设其中有10条记录。现在我要发送一封从GridView检索到的电子邮件和电子邮件,但我希望首先发送电子邮件将发送到前5条记录,然后在5秒后发送电子邮件重新发送到接下来的5条记录。
以下是我的电子邮件发送代码:
void Send_Mail()
{
try
{
string Pass, FromEmailid, HostAdd;
foreach (GridViewRow gr in DataGridView.Rows)
{
HostAdd = ConfigurationManager.AppSettings["Host"].ToString();
FromEmailid = ConfigurationManager.AppSettings["FromMail"].ToString();
Pass = ConfigurationManager.AppSettings["Password"].ToString();
Label LblName = gr.FindControl("LblName") as Label;
Label LblMail = gr.FindControl("LblEmail") as Label;
string Name = LblName.Text;
string Mail = LblMail.Text;
string subject = TxtSubject.Text;
SmtpClient client = new SmtpClient();
MailMessage msg = new MailMessage();
NetworkCredential credentials = new NetworkCredential(FromEmailid, Pass);
client.Host = HostAdd;
client.Port = 25;
client.UseDefaultCredentials = false;
client.Credentials = credentials;
client.EnableSsl = true;
MailAddress from = new MailAddress(FromEmailid);
msg.IsBodyHtml = true;
msg.Subject = subject;
msg.Body = ReadTemplate(Name);
msg.To.Add(Mail);
msg.From = from;
client.Send(msg);
LblMessage.Text = "Email Send Successfully";
LblMessage.Visible = true;
}
}
catch (Exception Ex)
{
LblMessage.Text = Ex.Message;
LblMessage.Visible = true;
}
}
答案 0 :(得分:1)
计算迭代次数(或使用for
代替foreach
),当您到达第五次迭代时,请设置Thread.Sleep(5000);
类似的东西:
int counter = 0;
foreach (GridViewRow gr in DataGridView.Rows)
{
if (counter > 0 && counter % 5.0 == 0)
{
Thread.Sleep(5000);
}
//More stuff sending mail...
counter++;
}