【问题标题】:Sending Email based on Selected checkbox根据选中的复选框发送电子邮件
【发布时间】:2017-05-29 13:23:55
【问题描述】:

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

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>

【问题讨论】:

    标签: c# jquery email checkbox


    【解决方案1】:
       @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();
        }
    

    【讨论】:

    • 感谢您的帮助!但是我已经更改了代码,当我单击按钮时,没有任何变化
    • @using (Html.BeginForm("SendReminder", "ControllerName", FormMethod.Post)) {//在这个里面写你的cshtml } 并且还在SendReminder post方法上放一个调试器。查看是否启用了调试器。
    • @using (Html.BeginForm("SendReminder", "ControllerName", FormMethod.Post) 从已退出 SendReminder 操作方法的控制器中更改“ControllerName”。
    【解决方案2】:
    @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();
    }
    

    【讨论】:

    • 您不需要在复选框内传递 fname。仅在复选框值中传递电子邮件 ID,例如 - 。要获取 fname,请写入此 。上面我已经写了完整的cshtml。请参考上面的代码。
    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 2011-01-14
    • 1970-01-01
    • 2014-07-09
    • 2019-07-19
    相关资源
    最近更新 更多