1
4

2 回答 2

3

Try creating a new MailMessage after you check the checkbox, what you're doing now is reusing the same message for each iteration and adding a new mail address without clearing the previous ones.

protected void btnSendEmail_Click(object sender, EventArgs e)
{

    foreach (GridViewRow gr in GridView1.Rows)
    {
        CheckBox cb = (CheckBox)gr.FindControl("chkItem");
        if (cb.Checked)
        {
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add(new MailAddress(GridView1.DataKeys[gr.RowIndex]["Assigned_To"].ToString()));
            mailMessage.From = new System.Net.Mail.MailAddress(System.Configuration.ConfigurationManager.AppSettings["fromEmailAddress"]);
            mailMessage.Priority = System.Net.Mail.MailPriority.High;
            //Text/HTML
            mailMessage.IsBodyHtml = false;
            string mySubURI = HttpContext.Current.Request.Url.AbsoluteUri.ToString().Replace("Test.aspx", "ABC.aspx");
            mySubURI += String.Format("&ID={0}", gr.Cells[0].Text);
            mailMessage.Body = "this is just test... " + " " + Environment.NewLine + mySubURI;
            mailMessage.Subject = "Test";


            System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient();
            try
            {
                smtpClient.Send(mailMessage);

                //Response.Write("<B>Email Has been sent successfully.</B>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }
    }
于 2012-10-25T20:52:18.097 回答
0

I think your problem is this: you're creating the mail message once, but re-using it for every row. So for every grid row you add a new To entry ... but you've still got the old ones!

于 2012-10-25T20:53:48.717 回答