1

我正在尝试在 Visual Studio Ultimate 2013 中的 Mvc.5.2.3 和 Razor.3.2.3 中使用 Postal,我有许多要为这个项目生成的表单,并且想使用视图,因为我想将内容发布到数据库中以及使用 Postal 发送包含表单信息的电子邮件。我还为页眉和页脚创建了部分视图,因此它们始终相同,并且只有电子邮件的内容会因使用的表单而改变。将发送的电子邮件将发送给销售部门进行审核,第二封电子邮件需要发送给填写表格的人,表示感谢并显示他们与表格一起发送的信息。我希望这是有道理的。我让数据库正常工作,但让电子邮件系统工作遇到了很多麻烦,我刚刚重新开始,只是试图让电子邮件系统正常工作。

我的模特

    using Postal;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;


    namespace APP.Models.Forms.Company
    {


        public class ContactEmail : Email
        {
          public ContactEmail() : base("Contact")
          { }

          public int ContactId { get; set; }
          public Guid TicketId { get; set; }

          [Required(ErrorMessage = "Please Enter your First Name!")]
          [StringLength(100, MinimumLength = 3)]
          [DisplayName("First Name")]
          [Display(Order = 1)]
          public string FirstName { get; set; }

          [Required(ErrorMessage = "Please Enter your Last Name!")]
          [StringLength(100, MinimumLength = 3)]
          [DisplayName("Last Name")]
          [Display(Order = 2)]
          public string LastName { get; set; }

          [DisplayName("Business Name")]
          [Display(Order = 3)]
          public string BusinessName { get; set; }


          [Required(ErrorMessage = "You have not entered a phone numer, Please enter your phone number so we can get back to you!")]
          [DataType(DataType.PhoneNumber)]
          [DisplayName("Phone Number")]
          [RegularExpression(@"^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$", ErrorMessage = "Please enter proper format of one of the following: (555)555-5555, 555-555-5555, 5555555555")]
          [StringLength(32)]
          [Display(Order = 4)]
          public string Phone { get; set; }

          [Required(ErrorMessage = "You have not entered an Email address, Please enter your email address!")]
          [DataType(DataType.EmailAddress)]
          [DisplayName("Email Address")]
          [MaxLength(50)]
          [RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "The Email field is not valid, Please enter a valid email address!")]
          [Display(Order = 5)]
          public string UserEmailAddress { get; set; }

          [Required(ErrorMessage = "You have not entered a message, Please enter a message!")]
          [DataType(DataType.MultilineText)]
          [StringLength(2000)]
          [DisplayName("Message")]
          [Display(Order = 6)]
          public string Message { get; set; }

          public Source Source { get; set; }


          public HttpPostedFileBase Upload { get; set; }


          [Display(Name = "Full Name")]
          public string FullName
          {
              get
              {
                  return LastName + ", " + FirstName;
              }
          }

      }

  }

控制器:

    using APP.Models;
    using APP.Models.Forms.Company;
    using Postal;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Net.Mail;
    using System.Text;
    using System.Web;
    using System.Web.Mvc;

    namespace APP.Controllers
    {
        public class FormsController : Controller
        {
            private ApplicationDbContext db = new ApplicationDbContext();

            #region Main Forms Page
            // Forms Page Blank with unautherized access
            public ActionResult Index()
            {
                return View();
            }
            #endregion


            #region Contact_Form
            // GET: Forms/Create
            public ActionResult Contact()
            {

                return View();
            }

            // POST: Forms/Submit


            [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Send(ContactEmail form)
            {

                var email = new Email("Contact")
                {
                    To = "webmaster@somedomain.com",
                    MyModel = ContactEmail //Says its a "type" but used like a variable.


                }
                email.Send();



            }



    #endregion

    #region Condo Form
    #endregion

    #region Personal Flood Form
    #endregion

    #region Home Insurance Form
    #endregion

    #region Renters Insurance Form
    #endregion

    #region WaterCraft Insurance Form
    #endregion

    #region Life Insurance Form
    #endregion

    #region Business Flood Form
    #endregion

    #region Business Risk Form
    #endregion

    #region Business Inland Marine Form
    #endregion

    #region Business Group Health Form
    #endregion

    #region Form
    #endregion

    #region Not Available Forms Page
    // Forms Page Blank with unautherized access
    public ActionResult Not_Available()
    {
        return View();
    }
    #endregion

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }
}

}

表单视图:

    @model APP.Models.Forms.Company.ContactEmail


    @{
        ViewBag.Title = "Create";
        Layout = "~/Views/Shared/_Layout_LandingPages.cshtml";
    }
    <div class="box">
        <h2>Contact Form</h2>


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

        <div class="form-horizontal">
            <h4>Contact </h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group">
                @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="col-lg-12">
            <div class="form-group">
                @Html.LabelFor(model => model.BusinessName, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.BusinessName, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.BusinessName, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                @Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    @Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="col-lg-6">
            <div class="form-group">
                @Html.LabelFor(model => model.UserEmailAddress, htmlAttributes: new { @class = "control-label col-md-4" })
                <div class="col-md-8">
                    @Html.EditorFor(model => model.UserEmailAddress, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.UserEmailAddress, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>
        <div class="col-lg-12">
            <div class="form-group">
                @Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
                </div>
            </div>

        </div></div>


            <hr />
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Submit Form" class="btn btn-default" />&emsp;
                    <input type="reset" value="Reset Form" class="btn btn-default" />
                </div>
            </div>
        </div>
        }

        <div>


            @Html.ActionLink("Cancel", "Index", "Home")
        </div>
    </div>


    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

我的电子邮件视图是 Contact.cshtml,位于视图下的电子邮件文件夹中。

4

1 回答 1

2

您已经有一个电子邮件对象,所以只需调用发送:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Send(ContactEmail form)
{
    form.Send();

    // You could also save this to the database here...
}
于 2015-04-30T16:46:54.847 回答