I want to send an email with the value from the checkbox (retrieve from database).
I'm using 'for' to retrieve the selected item from checkbox. But the email was sending based on how many selection I made. What is the best way to retrieve the checkbox values and attached the values in the email?
Below is my code:
sending_checkbox_value.aspx
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sending_checkbox_value.aspx.cs" Inherits="sending_checkbox_value" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<script>
function SelectAll(id) {
var frm = document.forms[0];
for (i = 0; i < frm.elements.length; i++) {
if (frm.elements[i].type == "checkbox") {
frm.elements[i].checked = document.getElementById(id).checked;
}
}
}
</script>
<body>
<form id="form1" runat="server">
<div>
This form is to test to sending an email<br />
<br />
Email address:
<asp:TextBox ID="Email" runat="server"></asp:TextBox>
<asp:SqlDataSource ID="reg_type_ds" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbConn %>" SelectCommand="SELECT [ID], [UserType]FROM [UserType]"></asp:SqlDataSource>
<asp:GridView ID="RegistrationType" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="reg_type_ds" EnableModelValidation="True" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<%-- if we no need Check box in header then hide it below Header Template and also comment server side row data bound code--%>
<HeaderTemplate>
<asp:CheckBox ID="SelectAll" runat="server" OnCheckedChanged="SelectAll_CheckedChanged"/>
</HeaderTemplate>
<EditItemTemplate>
<asp:CheckBox ID="Chkbx" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="UserType" HeaderText="Registration Type" SortExpression="UserType" />
</Columns>
</asp:GridView>
<br />
<asp:Button ID="submit_btn" runat="server" Text="Submit" OnClick="submit_btn_Click" />
</div>
</form>
</body>
</html>
the code behind: sending_checkbox_value.aspx.cs
Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
public partial class Sending_email : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_btn_Click(object sender, EventArgs e)
{
for (int i = 0; i <= RegistrationType.Rows.Count - 1; i++)
{
string toemail = RegistrationType.Rows[i].Cells[1].Text;
GridViewRow row = RegistrationType.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
sendMail(toemail);
}
Response.Write(toemail);
}
}
void sendMail(String result)
{
//register the email and password for network credential
string UserName = "[email protected]";
string Password = "abcdefgh";
using (MailMessage message = new MailMessage()){
//set the sender email
message.From = new MailAddress(Email.Text.ToString());
//set the receiver email
message.To.Add(new MailAddress("[email protected]"));
// set the message subject
message.Subject = "New Email from " + Email.Text.ToString();
//to enable the email content to have text formating
message.IsBodyHtml = true;
//set the email content/body
message.Body = "<strong><u>Sending testing email</u></strong><br><br>Email from : " + Email.Text.ToString() + "<br> Registration type : " + result;
//set the smtp resource
SmtpClient client = new SmtpClient("mail.business.com", 26);
//set the network credential
System.Net.NetworkCredential networkcred = new System.Net.NetworkCredential();
networkcred.UserName = UserName;
networkcred.Password = Password;
client.UseDefaultCredentials = false;
client.Credentials = networkcred;
client.Send(message);
Response.Write("E-mail sent! <br>");
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.Header))
{
((CheckBox)e.Row.FindControl("SelectAll")).Attributes.Add("onclick", "javascript:SelectAll('" + ((CheckBox)e.Row.FindControl("SelectAll")).ClientID + "')");
}
}
protected void SelectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk;
foreach (GridViewRow rowItem in RegistrationType.Rows)
{
chk = (CheckBox)(rowItem.Cells[0].FindControl("CheckBox1"));
chk.Checked = ((CheckBox)sender).Checked;
}
}
}