ASP.NET MVC发送带有数据库信息的电子邮件

时间:2019-01-26 18:22:23

标签: asp.net-mvc email

因此,我正在开发一个用于跟踪员工假期的应用程序。

在我的应用中,我有一个基于SQL表的表单,员工可以在该表中申请休假HolidayRequestForm。在称为Employee

的另一个表中跟踪所有员工信息(例如,工作时间)

我已经开发了表单来创建假期请求,当表单提交给区域经理时,该请求也会发送电子邮件。

表单/视图:

    @using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal" style=" position:relative; top:20px;border-radius: 0px; border-color: #F47B20; border-style: solid; border-width: 5px; background-repeat: no-repeat; background-position: right; padding: 60px; background-size: contain; background-color:white ">
        <h2 align="center">Holiday Request Form</h2>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.EmployeeID, "EmployeeID", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("EmployeeID", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.EmployeeID, "", new { @class = "text-danger" })
            </div>
        </div>


        <div class="form-group">
            @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
                @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FinishDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FinishDate, new { htmlAttributes = new { @class = "form-control", autocomplete = "off" } })
                @Html.ValidationMessageFor(model => model.FinishDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HoursTaken, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HoursTaken, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HoursTaken, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Comments, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextAreaFor(
             model => model.Comments,
   new { placeholder = "Comments", style = "width: 400px; height: 200px;" })
                @Html.ValidationMessageFor(model => model.Comments, "", new { @class = "text-danger" })
            </div>
        </div>

        @Html.HiddenFor(model => model.StartDate)

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-warning" onclick="SendEmail()" />
            </div>
        </div>
    </div>
}

 var SendEmail = function () {

    $.ajax({
        type: "Post",
        url: "/HolidayRequestForms/SendMailToManager",
        success: function (data) {

            alert("Success");


        }


    })

}

电子邮件的HolidayRequestFormsController操作:

 public JsonResult SendMailToManager()
    {
        bool result = false;

        result = SendEmail("conor8630@gmail.com", "LotusWorks Holiday Request", "Hi there [AreaManagerEmail],<br><br>[EmployeeEmail] has requested a holiday<br><br>The Employee will not be available to work From: <b>[StartDate]</b> to <b>[FinishDate]</b>.<br><br>Please forward this email to [ManagerEmail] with a response of Accept or Reject<br><br><br>Kind Regards,<br><br>LotusWorks Holiday Tracker");

        return Json(result, JsonRequestBehavior.AllowGet);


    }

    public bool SendEmail(string toEmail, string subject, string emailBody)
    {

        try
        {
            string senderEmail = System.Configuration.ConfigurationManager.AppSettings["SenderEmail"].ToString();
            string senderPassword = System.Configuration.ConfigurationManager.AppSettings["SenderPassword"].ToString();

            SmtpClient client = new SmtpClient("smtp-mail.outlook.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(senderEmail, senderPassword);

            MailMessage mailMesage = new MailMessage(senderEmail, toEmail, subject, emailBody);
            mailMesage.IsBodyHtml = true;
            mailMesage.BodyEncoding = UTF8Encoding.UTF8;
            client.Send(mailMesage);

            return true;
        }

        catch (Exception)
        {
            return false;

        }

    }

我希望让电子邮件的收件人成为该员工的区域经理。我的Employee表中有一列称为AreaManagerEmail

在电子邮件正文中,我想获取基于Employee表以及员工提交的信息的信息,例如。假期的开始日期。

这可能是我设置电子邮件的方式吗?

0 个答案:

没有答案