Tuesday, 28 November 2017

SENDING EMAILS IN ASP.NET MVC FROM CONTROLLER USING WEBMAIL HELPER Part 2

 SENDING EMAILS IN ASP.NET MVC FROM CONTROLLER USING WEBMAIL HELPER Part 2

    In my previous article on Sending Emails In ASP.NET MVC From Razor View, we have learned how to send emails from View using WebMail Class , Now in this article we will learn how to send emails in ASP.NET MVC from controller with the help WebMail helper class. So let's learn step by step so beginners can also learn how to send emails in ASP.NET MVC from controller.

    Prerequisites
    • Active internet connection.
    • Email id of any provider such as Gmail, Yahoo or your organization to send emails.
    Now let's create a simple MVC application to demonstrate this.

    Step 1: Create an MVC Application.
    Now let us start with a step by step approach from the creation of a simple MVC application as in the following:
    1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
    2. "File", then "New" and click "Project", then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click OK. After clicking, the following window will appear:




    3. As shown in the preceding screenshot, click on Empty template and check MVC option, then click OK. This will create an empty MVC web application whose Solution Explorer will look like the following:


     Step 2: Create Model Class.

    Now let us create the model class named EmployeeModel.cs by right clicking on model folder as in the following screenshot:


    Note: 

    It is not mandatory that Model class should be in Model folder, it is just for better readability you can create this class anywhere in the solution explorer. This can be done by creating different folder names or without folder name or in a separate class library.
    EmployeeModel.cs class code snippet: 

    1. using System.ComponentModel.DataAnnotations;  
    1.   
    1. namespace SendingEmailsFromController.Models  
    1. {  
    2.     public class EmployeeModel  
    3.     {  
    4.         [DataType(DataType.EmailAddress),Display(Name ="To")]  
    5.         [Required]  
    6.         public string ToEmail { getset; }  
    7.         [Display(Name ="Body")]  
    8.         [DataType(DataType.MultilineText)]  
    9.         public string EMailBody { getset; }  
    10.         [Display(Name ="Subject")]  
    11.         public string EmailSubject { getset; }  
    12.         [DataType(DataType.EmailAddress)]  
    13.         [Display(Name ="CC")]  
    14.         public string EmailCC { getset; }  
    15.         [DataType(DataType.EmailAddress)]  
    16.         [Display(Name ="BCC")]  
    17.         public string EmailBCC { getset; }  
    18.     }  
    19. }  
     Step 3 : Add Controller Class.

    Now let us add the MVC 5 controller as in the following screenshot:



    After clicking on Add button it will show the window. specify the Controller name as Home with suffix Controller.
    Note:
    The controller name must be having suffix as 'Controller' after specifying the name of controller. Now the default code in HomeController.cs will look like as follows,

    HomeController.cs
    1. using SendingEmailsFromController.Models;  
    2. using System;  
    3. using System.Web.Helpers;  
    4. using System.Web.Mvc;  
    1.   
    1. namespace SendingEmailsFromController.Controllers  
    1. {  
    2.     public class HomeController : Controller  
    3.     {  
    4.         // GET: Home  
    5.         public ActionResult SendEmail()  
    6.         {  
    7.              
    8.             return View();  
    9.         }  
    10.   
    11.         [HttpPost]  
    12.         public ActionResult SendEmail(EmployeeModel obj)  
    13.         {  
    14.             try  
    15.             {  
    16.                 //Configuring webMail class to send emails  
    17.                 //gmail smtp server  
    18.                 WebMail.SmtpServer = "smtp.gmail.com";   
    19.                 //gmail port to send emails  
    20.                 WebMail.SmtpPort = 587;  
    21.                 WebMail.SmtpUseDefaultCredentials = true;  
    22.                 //sending emails with secure protocol  
    23.                 WebMail.EnableSsl = true;   
    24.                 //EmailId used to send emails from application  
    25.                 WebMail.UserName = "YourGamilId@gmail.com";  
    26.                 WebMail.Password = "YourGmailPassword";  
    27.                 //Sender email address.  
    28.                 WebMail.From = "SenderGamilId@gmail.com";  
    29.                 //Send email  
    30.                 WebMail.Send(to: obj.ToEmail, subject: obj.EmailSubject, body: obj.EMailBody, cc: obj.EmailCC, bcc: obj.EmailBCC, isBodyHtml: true);  
    31.                 ViewBag.Status = "Email Sent Successfully.";  
    32.       }  
    33.       catch (Exception)  
    34.       {  
    35.                 ViewBag.Status = "Problem while sending email, Please check details.";  
    36.   
    37.        }  
    38.             return View();  
    39.         }  
    40.     }  
    41. }  
     Step 4 : 

    Create strongly typed view named SendEmail using EmployeeModel class.

    Right click on View folder of created application and choose add view , select employee model class and scaffolding template as create . As shown in the following image,



    Now open the SendEmail.cshtml view , Then following default code you will see which is generated by MVC scaffolding template as,
    SendEmail.cshtml 
    1. @model SendingEmailsFromController.Models.EmployeeModel  
    2.   
    3. @{  
    4.     ViewBag.Title = "www.compilemode.com";  
    5. }  
    6. @using (Html.BeginForm())  
    7. {  
    8.     @Html.AntiForgeryToken()  
    9.   
    10.   
    11.     <div class="form-horizontal">  
    12.         <hr />  
    13.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
    14.         <div class="form-group">  
    15.             @Html.LabelFor(model => model.ToEmail, htmlAttributes: new { @class = "control-label col-md-2" })  
    16.             <div class="col-md-10">  
    17.                 @Html.EditorFor(model => model.ToEmail, new { htmlAttributes = new { @class = "form-control" } })  
    18.                 @Html.ValidationMessageFor(model => model.ToEmail, ""new { @class = "text-danger" })  
    19.             </div>  
    20.         </div>  
    21.   
    22.         <div class="form-group">  
    23.             @Html.LabelFor(model => model.EMailBody, htmlAttributes: new { @class = "control-label col-md-2" })  
    24.             <div class="col-md-10">  
    25.                 @Html.EditorFor(model => model.EMailBody, new { htmlAttributes = new { @class = "form-control" } })  
    26.                 @Html.ValidationMessageFor(model => model.EMailBody, ""new { @class = "text-danger" })  
    27.             </div>  
    28.         </div>  
    29.   
    30.         <div class="form-group">  
    31.             @Html.LabelFor(model => model.EmailSubject, htmlAttributes: new { @class = "control-label col-md-2" })  
    32.             <div class="col-md-10">  
    33.                 @Html.EditorFor(model => model.EmailSubject, new { htmlAttributes = new { @class = "form-control" } })  
    34.                 @Html.ValidationMessageFor(model => model.EmailSubject, ""new { @class = "text-danger" })  
    35.             </div>  
    36.         </div>  
    37.   
    38.         <div class="form-group">  
    39.             @Html.LabelFor(model => model.EmailCC, htmlAttributes: new { @class = "control-label col-md-2" })  
    40.             <div class="col-md-10">  
    41.                 @Html.EditorFor(model => model.EmailCC, new { htmlAttributes = new { @class = "form-control" } })  
    42.                 @Html.ValidationMessageFor(model => model.EmailCC, ""new { @class = "text-danger" })  
    43.             </div>  
    44.         </div>  
    45.   
    46.         <div class="form-group">  
    47.             @Html.LabelFor(model => model.EmailBCC, htmlAttributes: new { @class = "control-label col-md-2" })  
    48.             <div class="col-md-10">  
    49.                 @Html.EditorFor(model => model.EmailBCC, new { htmlAttributes = new { @class = "form-control" } })  
    50.                 @Html.ValidationMessageFor(model => model.EmailBCC, ""new { @class = "text-danger" })  
    51.             </div>  
    52.         </div>  
    53.   
    54.         <div class="form-group">  
    55.             <div class="col-md-offset-2 col-md-10">  
    56.                 <input type="submit" value="Send" class="btn btn-primary" />  
    57.             </div>  
    58.         </div>  
    59.         <div class="form-group">  
    60.             <div class="col-md-offset-2 col-md-10 text-success">  
    61.                 @ViewBag.Status  
    62.             </div>  
    63.         </div>  
    64.     </div>  
    65.   
    66. }  
     Now after adding the model , view and controller our application solution explorer will look as follows,


    Now we have done all coding to send emails using WebMail class.

    Step 5 : Now run the application.

No comments:

Post a Comment