Hi nabilabolo,
Check this example. Now please take its reference and correct your code.
Model
public class Student
{
public string Id { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public bool IsSelected { get; set; }
public string Email { get; set; }
}
Namespaces
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using System.Net.Mail;
using System.Threading.Tasks;
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
List<Student> students = GetStudents();
return View(students);
}
private static List<Student> GetStudents()
{
List<Student> students = new List<Student>();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT Id,FName,LName,IsSelected,Email FROM Students";
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
while (sdr.Read())
{
students.Add(new Student
{
Id = sdr["Id"].ToString(),
FName = sdr["FName"].ToString(),
LName = sdr["LName"].ToString(),
IsSelected = Convert.ToBoolean(sdr["IsSelected"]),
Email = sdr["Email"].ToString()
});
}
con.Close();
}
}
return students;
}
[HttpPost]
public ActionResult Index(List<Student> students)
{
//Create a temporary DataTable
DataTable dtStudents = new DataTable();
dtStudents.Columns.AddRange(new DataColumn[2] {
new DataColumn("Name", typeof(string)),
new DataColumn("Email",typeof(string)) });
foreach (Student student in students)
{
if (student.IsSelected)
{
//Copy the Checked Rows to DataTable.
dtStudents.Rows.Add(student.FName + " " + student.LName, student.Email);
}
}
string subject = "Welcome Email";
string body = "Hello {0},<br /><br />Welcome to ASPSnippets<br /><br />Thanks.";
//Using Parallel Multi-Threading send multiple bulk email.
Parallel.ForEach(dtStudents.AsEnumerable(), row =>
{
SendEmail(row["Email"].ToString(), subject, string.Format(body, row["Name"]));
});
return RedirectToAction("Index");
}
private bool SendEmail(string recipient, string subject, string body)
{
MailMessage mm = new MailMessage("sender@gmail.com", recipient);
mm.Subject = subject;
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "<password>";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
return true;
}
}
View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<_Checked_Row_Email.Models.Student>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Index</title>
</head>
<body>
<%using (Html.BeginForm("Index", "Home", FormMethod.Post))
{%>
<table>
<tr>
<th>Id</th>
<th>FName</th>
<th>LName</th>
<th>IsSelected</th>
</tr>
<% foreach (var student in Model) { %>
<tr>
<td><%: student.Id%></td>
<td><%: student.FName%></td>
<td><%: student.LName%></td>
<td><%:Html.CheckBoxFor(m => student.IsSelected)%></td>
</tr>
<% } %>
</table>
<br /><input type="submit" value="Submit" />
<%} %>
</body>
</html>