根据选中的复选框发送电子邮件

时间:2017-01-14 05:17:28

标签: c# jquery email checkbox

我为复选框创建了一个列,我想向已经过检查的用户发送电子邮件。我该怎么做呢?非常感谢您的帮助!

Image output from the code I created - Send Email

控制器:

public ActionResult SendEmail()
        {


            using (DatabaseEntities dbc = new DatabaseEntities())
            {
                DbSet<user> dbs = dbc.users;
                ViewBag.ButtonText = "Send Reminder Email";

                return View("View", model);

            }

        }

View.cshtml

@using SendEmail.Models @model List
<user>

  @{ Layout = "~/Views/Shared/_Layout.cshtml"; }

  <h2>@ViewBag.ButtonText</h2>


    $(document).ready(function() {
      $('#checkBoxAll').click(function() {
        if ($(this).is(":checked"))
          $('.chkCheckBox').prop('checked', true);
        else
          $('.chkCheckBox').prop('checked', false);




      });
    });
  </script>


  @if (ViewBag.ButtonText == "Send Email") {
  <table>
    <tr>

      <th>Name</th>
      <th>Email</th>
      <th>
        <input type="checkbox" id="checkBox" />Select All</th>

    </tr>
    @foreach (user u in Model) {
    <tr>

      <td>@i.name</td
      <td>@u.email</td>
      <td>
        <input type="checkbox" class="chkCheckBox1" value="@u.id"/>

      </td>

    </tr>

    }
  </table>
  }



  <div>
    <p>
      <input type="submit" value="Send" />
    </p>
  </div>

2 个答案:

答案 0 :(得分:0)

   @using SendEmail.Models @model List
   <user>

  @{ Layout = "~/Views/Shared/_Layout.cshtml"; }

  <h2>@ViewBag.ButtonText</h2>


  <script type="text/javascript" src="@Url.Content(" ~/Scripts/ ")jquery-1.10.2.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $('#checkBoxAll').click(function() {
    if ($(this).is(":checked"))
      $('.chkCheckBoxId').prop('checked', true);
    else
      $('.chkCheckBoxId').prop('checked', false);

  });
});
 </script>

 @if (ViewBag.ButtonText == "Send Reminder Email") {
@using (Html.BeginForm("SendReminder", "ControllerName", FormMethod.Post))
{
 <table>
<tr>

  <th>First Name</th>
  <th>Last Name</th>
  <th>Email</th>
  <th>
    <input type="checkbox" id="checkBoxAll" />Select All</th>

</tr>
@foreach (user u in Model) {
<tr>

  <td>@u.first_name</td>
  <td>@u.last_name</td>
  <td>@u.email</td>
  <td>
    <input type="checkbox" class="chkCheckBoxId" value="@u.email" name="userId" />

  </td>

</tr>

   }
 </table>
 }
 }
<div>
<p>
  <input type="submit" value=@ViewBag.ButtonText />
</p>
 </div>

行动结果

 public ActionResult SendReminder(int id)
    {
        using (DatabaseEntities dbc = new DatabaseEntities())
        {
            DbSet<user> dbs = dbc.users;
            IQueryable<user> q = from p in dbs
            where p.Booking.event_id == id
            select p;
            List<user> model = q.ToList<user>();
            ViewBag.ButtonText = "Send Reminder Email";

            return View("ViewDetails", model);

        }

    }

[HttpPost]
    public ActionResult SendReminder(string[] userId)
    {            
        using (SmtpClient objSmtpClient = new SmtpClient())
        {
            for (int emailCount = 0; emailCount < userId.Length; emailCount++)
            {
                if (!string.IsNullOrEmpty(userId[emailCount]))
                {
                    using (MailMessage mail = new MailMessage("sender@gmail.com", userId[emailCount].Trim()))
                    {
                        mail.Subject = "Test Email";
                        mail.Body = "Test Body";
                        mail.IsBodyHtml = true;
                        using (SmtpClient smtp = new SmtpClient())
                        {
                            smtp.Host = "smtp.gmail.com";
                            smtp.Port = 587;
                            smtp.Credentials = new    System.Net.NetworkCredential("sender@gmail.com", "password");
                            smtp.EnableSsl = true;
                            mail.IsBodyHtml = true;
                            smtp.Send(mail);
                        }
                    }
                }
            }
        }
        return View();
    }

答案 1 :(得分:0)

@using SendEmail.Models @model List
 <user>
 @{ Layout = "~/Views/Shared/_Layout.cshtml"; }

 <h2>@ViewBag.ButtonText</h2>


 <script type="text/javascript" src="@Url.Content(" ~/Scripts/ ")jquery-1.10.2.js"></script>
  <script type="text/javascript">
   $(document).ready(function() {
   $('#checkBoxAll').click(function() {
   if ($(this).is(":checked"))
  $('.chkCheckBoxId').prop('checked', true);
 else
  $('.chkCheckBoxId').prop('checked', false);

 });
});
</script>

@if (ViewBag.ButtonText == "Send Reminder Email") {
@using (Html.BeginForm("SendReminder", "ControllerName", FormMethod.Post))
{
 <table>
  <tr>

 <th>First Name</th>
 <th>Last Name</th>
 <th>Email</th>
 <th>
<input type="checkbox" id="checkBoxAll" />Select All</th>

</tr>
   @foreach (user u in Model) {
<tr>

<td>@u.first_name 
 <input type="hidden" name="hdnFName" value="@u.first_name" />
</td>
<td>@u.last_name</td>
<td>@u.email</td>
<td>
 <input type="checkbox" class="chkCheckBoxId" value="@u.email" name="userId" />

 </td>

</tr>

}
</table>
 }
}
<div>
<p>
<input type="submit" value=@ViewBag.ButtonText />
</p>
</div>

控制器代码

[HttpPost]
public ActionResult SendReminder(string[] userId, FormCollection formCollection)
{ 
    string fName=String.Empty;
    string[] arrFName = Convert.ToString(formCollection["hdnFName"]).Split(',');           
    using (SmtpClient objSmtpClient = new SmtpClient())
    {
        for (int emailCount = 0; emailCount < userId.Length; emailCount++)
        {
        //first name
         fName= arrFName[emailCount];
            if (!string.IsNullOrEmpty(userId[emailCount]))
            {
                using (MailMessage mail = new MailMessage("sender@gmail.com", userId[emailCount].Trim()))
                {
                    mail.Subject = "Test Email";
                    mail.Body = "Test Body";
                    mail.IsBodyHtml = true;
                    using (SmtpClient smtp = new SmtpClient())
                    {
                        smtp.Host = "smtp.gmail.com";
                        smtp.Port = 587;
                        smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "password");
                        smtp.EnableSsl = true;
                        mail.IsBodyHtml = true;
                        smtp.Send(mail);
                    }
                }
            }
        }
    }
    return View();
}