📜 ⬆️ ⬇️

We send letters from ASP .NET MVC

Often applications need to send emails to users. Today I will show how this task is easily solved with the help of ActionMailer .Net. Its main advantage in the view of sent letters in the form of controller actions, the message body itself is represented by the display, as well as the fact that in debug mode it allows you to save letters directly to your hard drive without using an email server.

Create an empty project. With the help of NuGet'a add ActionMailer:
PM> Install-Package ActionMailer 


For the demonstration, we will create one Home controller, and add four actions to it: for the main page, sending, page in case of successful sending and in case of unsuccessful sending:
 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(); } } 

Now add a model for writing:
 public class EmailModel { public string Subject { get; set; } public string From { get; set; } public string To { get; set; } public string Body { get; set; } } 

Create a mapping for the Index action. We will have this unpretentious mold with the necessary fields:
 @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"/> } 

We get this form:

Now we will create a controller that will directly send our letters. Call it EmailController and inherit from MailerBase. Also add and implement the action of sending a letter:
 public class EmailController : MailerBase { public EmailResult SendEmail(EmailModel model) { To.Add(model.To); From = model.From; Subject = model.Subject; return Email("SendEmail", model); } } 

To, From, Subject - are the internal properties of the controller, and the values ​​from them will be used when sending the letter. It is worth noting that To is a list, thus mass mailings are possible. Spammers will rejoice.
Now add to our action view. In this case, the view will play the role of the body of the letter being sent, i.e. You can add html markup:
 @model TestSendEmail.Models.EmailModel @{ Layout = null; } <html> <body> <h1>@Model.Subject</h1> <p>@Model.Body</p> </body> </html> 

I will note more about the naming of the file type. In the default version, it will have the name: ResetPassword.cshtml. For ActionMailer to work correctly, you need to add .html so that the name takes the following form: ResetPassword.html.cshtml.
Also, for correct operation, you need to add parameters for how to send mail. The first option to save sent almost to the local disk:
 <mailSettings> <smtp deliveryMethod="SpecifiedPickupDirectory"> <specifiedPickupDirectory pickupDirectoryLocation="C:\" /> <network host="localhost" /> </smtp> </mailSettings> 

And the second option when using the mail server:
 <mailSettings> <smtp deliveryMethod="Network"> <network host="smtp.gmail.com" userName="myemail@gmail.com" password="mypassword" enableSsl="true"/> </smtp> </mailSettings> 

These lines must be added to the Web.config , in the <system.net> section.
Well, now add the logic of sending a letter to the action Index of our Home controller:
 [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); } 

Now when you click on the button, our letter will be sent (or saved to disk).

Additional materials:
http://geeksharp.com/2011/01/26/actionmailer-net-email-templates-for-the-mvc-crowd/

')

Source: https://habr.com/ru/post/146939/


All Articles