Wednesday, 21 November 2018

Send Email in ASP.NET MVC

In order to send emails using the SMTP server in ASP.NET MVC application, you can use the "WebMail" helper class with a method called "Send" using the namespace "using System.Web.Helpers". For details, please follow the steps below.

First of all, we have to create a new Asp.Net Application; let us see the description of how to create it.
  • Open Visual Studio
  • File>New>Project
  • Visual C#>Web>ASP.NET Web Application(.NET Framework)>Enter the Project Name and Browse the file path and click OK, then select MVC click OK.
  • Open the "Web.config" file and add the following code for SMTP settings.
  

    
      
        
      
    
  
Note: 
  • Set username is your Gmail(email) and password is your email password.
  • You can use any SMTP server to send emails using respective port number(Here I am using Gmail).

  • Create a "RegisterModel.cs" class in the Model folder and add the following code.
using System.ComponentModel.DataAnnotations;

namespace EmailNotification.Models
{
    public class RegisterModel
    {
        [Required]
        public string Name { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        public string Subject { get; set; }

        [Required]
        public string Message { get; set; }
    }
}

  • Go to Views > Home  Open an "Index.cshtml" file and add the following code.
@model EmailNotification.Models.RegisterModel

@{
    ViewBag.Title = "Home Page";
}


@if (TempData["Success"] != null)
{
    
@TempData["Success"]
} @if (TempData["Error"] != null) {
@TempData["Error"]
} @using (Html.BeginForm("SendEmail", "Home", FormMethod.Post)) { @Html.AntiForgeryToken()

Send Email Using SMTP Server


@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control", @type = "email" } }) @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Subject, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Subject, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Subject, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
} @section Scripts { @Scripts.Render("~/bundles/jqueryval") }

  • Go to the Controller folder and open "HomeController.cs" file and add the following code.
using EmailNotification.Models;
using System.Web.Helpers;
using System.Web.Mvc;

namespace EmailNotification.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult SendEmail(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                string body = "Hi " + model.Name + "

";
                body += "
" + model.Message + "
"; WebMail.Send(model.Email, model.Subject, body, null, null, null, true, null, null, null, null, null, null); TempData["Success"] = "Email sent successfully."; return RedirectToAction("Index"); } TempData["Error"] = "Error occured while sending Email."; return RedirectToAction("Index"); } } }

  • Run the application and enter valide records then click send button.
  • Email sent successfully, see the result below.


No comments:

Post a Comment