从GridView发送重复的电子邮件

时间:2012-10-25 20:47:24

标签: asp.net webforms

此代码的目的是将GridView中的电子邮件发送给其电子邮件列在其中的个人,因此代码工作正常,但它确实有些奇怪。我在这里解释一下,如果我的GridView看起来像这样

ID            Name              Email
4             Mike              Mike@.com
6             John              John@.com
9             David             David@.com

点击发送按钮后,

  1. Mike收到所有3封不同的电子邮件,其中包含邮件信息正文中ID4,ID6,ID9的URL中的所有3个ID。
  2. John只收到2封从ID6和ID9开始的电子邮件
  3. David只收到一封电子邮件,这是邮件邮件正文中ID9的电子邮件。
  4. 我在这里做错了什么?我需要每个人只收到他们各自的电子邮件。有人可以帮忙吗?请注意,DateKeyName中的GridView是“Assigned_To”,因此我不确定是否需要将其更改为“ID”。如果我这样做那么它会抛出一个错误。谢谢 这是代码:

    protected void btnSendEmail_Click(object sender, EventArgs e)
        {
            MailMessage mailMessage = new MailMessage();
            foreach (GridViewRow gr in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)gr.FindControl("chkItem");
                if (cb.Checked)
                {
    
                    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);
                    }
    
                }
            }
    

2 个答案:

答案 0 :(得分:3)

在选中复选框后尝试创建新的MailMessage,您现在正在做的是为每次迭代重复使用相同的消息并添加新的邮件地址而不清除以前的邮件地址。

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

        }
    }

答案 1 :(得分:0)

我认为你的问题是这样的:你创建了一次邮件消息,但是每一行都重复使用它。因此,对于每个网格行,您都添加了一个新的To条目......但是您仍然有旧的!