如何在ASP.NET中使用Parallel.ForEach和Grid View

时间:2014-03-28 10:59:51

标签: asp.net parallel-processing

我创建了一个发送批量邮件的应用程序,但我的应用程序花了太多时间发送所有电子邮件。这是我的代码:

  foreach (GridViewRow grow in GridView1.Rows)
        {
            string Emails = grow.Cells[0].Text.Trim();

            string file = Server.MapPath("~/Mail.html");
            string mailbody = System.IO.File.ReadAllText(file);
            string to = Emails;
            string from = "xyz@gmail.com";
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = "Auto Response Email";
            msg.Body = mailbody;
            msg.BodyEncoding = Encoding.UTF8;
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.live.com", 25);
            client.UseDefaultCredentials = false;   
            System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz@gmail.com", "password");
            client.EnableSsl = true;
            client.UseDefaultCredentials = true;
            client.Credentials = basicCredential;
            try
            {
                client.Send(msg);
                cnfrm.Text = "Email Sended Successfully";
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        } 

因此,对于它的解决方案,我使用Parallel.ForEach,但我发现自己无法在GridView中使用它。

  Parallel.ForEach(GridView1.Rows, GridViewRow =>
        {

            string Emails = grow.Cells[0].Text.Trim();

            string file = Server.MapPath("~/Mail.html");
            string mailbody = System.IO.File.ReadAllText(file);
            string to = Emails;
            string from = "xyz@gmail.com";
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = "Auto Response Email";
            msg.Body = mailbody;
            msg.BodyEncoding = Encoding.UTF8;
            msg.IsBodyHtml = true;
            SmtpClient client = new SmtpClient("smtp.live.com", 25);
            client.UseDefaultCredentials = false;
            System.Net.NetworkCredential basicCredential = new System.Net.NetworkCredential("xyz689@gmail.com", "password");
            client.EnableSsl = true;
            client.UseDefaultCredentials = true;
            client.Credentials = basicCredential;
            try
            {
                client.Send(msg);
                cnfrm.Text = "Email Sended Successfully";
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        });

有谁能告诉我如何使用它!!

1 个答案:

答案 0 :(得分:0)

我认为您不能像多线程一样访问Gridview行,在主线程中提取文本然后使用foreach。另请查看PartitionerForEach

一起使用

以下是它的外观示例:

var emailtext = new List<string>()
foreach (GridViewRow grow in GridView1.Rows)
{
        emailtext.Add(grow.Cells[0].Text.Trim());
}

//split and load balance the List
Partitioner<string> partitioner = Partitioner.Create(emailtext, true); 
Parallel.ForEach(partitioner, mailtext =>
{
    //your email sending code here
    //NB: 'mailtext' is your string to send
}

注意:大多数邮件提供商限制您每个会话或每小时发送的电子邮件数量,因此您可能需要在应用程序中考虑这一点。