2011-04-13 42 views
0

我希望對以下問題提供一些幫助。我試圖發送電子郵件給在GridView中檢查的用戶。我搜索了四周,發現很多看起來很像的主題,但我找不到工作解決方案。那麼我知道我錯過了什麼,也許在我的選擇聲明。但我無法弄清楚。在GridView的複選框中發送多個電子郵件不起作用

我的代碼顯示了一個gridview,讓我們說2個用戶,許多項目和來自用戶的電子郵件地址。我爲每個用戶選擇了1項,因此應發送2封電子郵件。發送2封郵件,但只發送到1個電子郵件地址,即他在數據庫中首先遇到的電子郵件地址。我試着搜索用戶標識,但無法做到這一點,我仍然在我的頭幾個月開發.NET。

看到這裏我的代碼:

public void Email() 
    { 
     string conn = "Data Source=pc-..."; 
     LabelSendGridAbove.Text = "<b>Title</b><br /><br /> Text... <br /><br /> "; 

     LabelSendGridBetween.Text = "<br /><br /> More text. " + 
      "<br /><br /><br /> Regards, <br /><br /> " ; 

     LabelSendGridUnder.Text = "<br /><br />--------------------------------------"; 


     System.Data.SqlClient.SqlConnection sqlConn = new System.Data.SqlClient.SqlConnection(conn); 
     sqlConn.Open(); 
     SqlCommand sendGrid = 
      new SqlCommand("SELECT statement for gridview which will be send in the email ", sqlConn); 

     GridView grd = new GridView(); 

     // Css for the gridview which will be send in mail  
     grd.BorderStyle = System.Web.UI.WebControls.BorderStyle.None; 
     grd.GridLines = GridLines.None; 
     grd.RowStyle.HorizontalAlign = HorizontalAlign.Center; 
     grd.Width = 600; 

     foreach (DataControlField field in grd.Columns) 
     { 
      field.ItemStyle.Width = Unit.Percentage(100/grd.Columns.Count); 
     } 

     if (sendGrid != null) 
     { 
      grd.DataSource = sendGrid.ExecuteReader(); 
      grd.DataBind(); 
     } 

     StringBuilder sb = new StringBuilder(); 

     StringWriter sw = new StringWriter(sb); 
     HtmlTextWriter htw = new HtmlTextWriter(sw); 
     grd.RenderControl(htw); 

     sqlConn.Close(); 


     foreach (GridViewRow r in GridViewOrder.Rows) 
     { 
      if (((CheckBox)r.Cells[0].FindControl("CheckBoxGetProduct")).Checked == true) 
      { 
       System.Data.SqlClient.SqlConnection sqlConn3 = new System.Data.SqlClient.SqlConnection(conn); 
       sqlConn3.Open(); 

        if (((Label)r.Cells[0].FindControl("LabelEmailUsr")) != null) 
        { 
         Label txtUser = (Label)GridViewOrder.FindControl("LabelEmailUsr"); 
         if (txtUser != null) 
         { 
          LabelTestMail.Visible = true; 
          LabelTestMail.Text = txtUser.Text.ToString(); 
         } 

         SqlCommand emailAdres = new SqlCommand("SELECT tblUsers.tUserEmail FROM tblUsers", sqlConn3); 

         GridViewOrder.DataBind(); 

         string email = Convert.ToString(emailAdres.ExecuteScalar()); 
         LabelTestMail.Text = email; 

         try 
         { 
          MailMessage mail = new MailMessage(); 
          mail.To.Add(email.ToString()); 
          mail.Bcc.Add("SomeEmail"); 
          mail.From = new MailAddress("FromWho"); 
          mail.Subject = "SomeSubject"; 
          string Body = LabelSendGridAbove.Text + sb.ToString() + LabelSendGridBetween.Text + "<br /><img alt=\"\" hspace=0 src=\"cid:imageId\" align=baseline border=0 >" + LabelSendGridUnder.Text + "<BR>"; 

          AlternateView htmlView = AlternateView.CreateAlternateViewFromString(Body, null, "text/html"); 
          LinkedResource imagelink = new LinkedResource(Server.MapPath(".") + @"\logo\Logo.jpg"); 
          imagelink.ContentId = "imageId"; 
          imagelink.TransferEncoding = System.Net.Mime.TransferEncoding.Base64; 
          htmlView.LinkedResources.Add(imagelink); 
          mail.AlternateViews.Add(htmlView); 
          SmtpClient smtp = new SmtpClient("relay.skynet.be"); 
          smtp.Send(mail); 
         } 
         catch (Exception ex) 
         { 
          Response.Write(ex.Message); 
         } 
        } 
      }    
     } 
    } 

我希望不會有這個代碼太多的錯誤。但如果有人知道我應該添加到代碼中以使其工作,因此它會將2封電子郵件發送到2個不同的電子郵件地址,這將非常好。

謝謝。

+0

我改變了代碼,所以它可能更有意義。不過,同樣的問題。 – Dieter 2011-04-13 19:03:06

回答

1

如果我理解正確,發送兩封郵件,但只發送到一個地址...... DB的第一個地址......我是否正確?

如果是這樣,那麼這個問題行是

SqlCommand emailAdres = new SqlCommand("SELECT tblUsers.tUserEmail FROM tblUsers", sqlConn3); 

在我看來,你正在呼籲對...每個循環的每個迭代相同的電子郵件地址。嘗試添加一個where子句和一個搜索參數。

+0

你好,謝謝你的回覆。我嘗試添加where子句,但是我發現很難編寫正確的代碼。目前看起來像這樣: 'Label lblUsrId =(Label)GridViewOrder.FindControl(「LabelId」); SqlCommand emailAdres = new SqlCommand(「SELECT tblGebruikers.tUsrId,tblUsers.tUsrEmail」+ 「FROM tblUsers WHERE tUsrId = @ tUsrId」,sqlConn3); emailAdres.Parameters.AddWithValue(「@ tUsrId」,lblUsrId); GridViewOrder.DataBind(); string email = Convert.ToString(emailAdres.ExecuteScalar());' 我在最後一行發生錯誤。 – Dieter 2011-04-13 19:25:01

+0

將where子句更改爲:'Label lblUsrId =(Label)GridViewOrder.FindControl(「LabelId」); SqlCommand emailAdres = new SqlCommand(「SELECT tblUsers.tUsrEmail FROM tblUsers WHERE tUsrId = @ tUsrId」,sqlConn3); emailAdres.Parameters.AddWithValue(「@ tUsrId」,lblUsrId.Text); emailAdres.ExecuteScalar();'New error says:對象引用未設置爲對象的實例。我很喜歡這裏卡住。 – Dieter 2011-04-13 20:23:55

相關問題