PM> Install-Package ActionMailer
public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost] public ActionResult Index(EmailModel model) { throw new NotImplementedException(); } public ActionResult Success() { return View(); } public ActionResult Error() { return View(); } }
public class EmailModel { public string Subject { get; set; } public string From { get; set; } public string To { get; set; } public string Body { get; set; } }
@model TestSendEmail.Models.EmailModel @{ Layout = "~/Views/Shared/_Layout.cshtml"; ViewBag.Title = "Index"; } @using(Html.BeginForm("Index", "Home", FormMethod.Post, new { @class = "well"})) { @Html.ValidationSummary() <label>Subject</label> @Html.TextBoxFor(m => m.Subject, new { @class = "input-xxlarge" }) <label>To</label> @Html.TextBoxFor(m => m.To, new { @class = "input-xxlarge" }) <label>From</label> @Html.TextBoxFor(m => m.From, new { @class = "input-xxlarge" }) <label>Body</label> @Html.TextAreaFor(m => m.Body, new { @class = "input-xxlarge" }) <br/> <input type="submit" class="btn" value="Send"/> }
public class EmailController : MailerBase { public EmailResult SendEmail(EmailModel model) { To.Add(model.To); From = model.From; Subject = model.Subject; return Email("SendEmail", model); } }
@model TestSendEmail.Models.EmailModel @{ Layout = null; } <html> <body> <h1>@Model.Subject</h1> <p>@Model.Body</p> </body> </html>
<mailSettings> <smtp deliveryMethod="SpecifiedPickupDirectory"> <specifiedPickupDirectory pickupDirectoryLocation="C:\" /> <network host="localhost" /> </smtp> </mailSettings>
<mailSettings> <smtp deliveryMethod="Network"> <network host="smtp.gmail.com" userName="myemail@gmail.com" password="mypassword" enableSsl="true"/> </smtp> </mailSettings>
[HttpPost] public ActionResult Index(EmailModel model) { if (ModelState.IsValid) { try { new EmailController().SendEmail(model).Deliver(); return RedirectToAction("Success"); } catch (Exception) { return RedirectToAction("Error"); } } return View(model); }
Source: https://habr.com/ru/post/146939/
All Articles