Sending Email from Rows in GridView

I have a scenario in which, I've already done some work but for my further process I need some help.

I've a ExcelSheet which is imported to the Grid View and suppose there are 10 records in it. Now I've to send an email and email retrieved from the GridView but I want that at first email will send to first 5 records then after taking 5 seconds email resend to the next 5 records.

Following is my email sending code:

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;
        }
    }

Answers


Take count of the iteration (or use for instead of foreach) and when you reach the fifth iteration put a Thread.Sleep(5000);

Something like that:

int counter = 0;
foreach (GridViewRow gr in DataGridView.Rows)
{
    if (counter > 0 && counter % 5.0 == 0)
    {
        Thread.Sleep(5000);
    }

//More stuff sending mail...
    counter++;
}

Need Your Help

How to determine or provide road type with Geocoding?

android google-maps google-maps-android-api-2 geocoding

Currently I am working on an app which has some sort of navigation through a line drawn over the roads. This line goes through some waypoints on his way from start to finish.

Jasmine Testing angularjs controller

angularjs unit-testing jasmine karma-jasmine

I am trying to test my controller but I get undefined when running karma start