Tuesday, 11 August 2020

Sending An E-Mail Using ASP.NET MVC

 For sending mail from ASP.NET MVC we use the "System.Net.Mail" namespace. Let's see how to do this.

  • Open Visual Studio
  • "File" -> "New" -> "Project..."
  • Choose Visual C#- Web then select ASP.NET MVC4 Web Application
  • Add a new Internet Application then click OK

Step 1

Create a new Model Class in the model folder.

The following is the code for the new Model.

MailModel.cs

  1. public class MailModel {  
  2.     public string From {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public string To {  
  7.         get;  
  8.         set;  
  9.     }  
  10.     public string Subject {  
  11.         get;  
  12.         set;  
  13.     }  
  14.     public string Body {  
  15.         get;  
  16.         set;  
  17.     }  
  18. }  

Step 2

Create a New SendMailerController in the Controller folder.

The following is the code for the design of the new Controller.

SendMailerController.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Mail;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace SendMail.Controllers {  
  8.     public class SendMailerController: Controller {  
  9.         //  
  10.         // GET: /SendMailer/   
  11.         public ActionResult Index() {  
  12.                 return View();  
  13.             }  
  14.             [HttpPost]  
  15.         public ViewResult Index(SendMail.Models.MailModel _objModelMail) {  
  16.             if (ModelState.IsValid) {  
  17.                 MailMessage mail = new MailMessage();  
  18.                 mail.To.Add(_objModelMail.To);  
  19.                 mail.From = new MailAddress(_objModelMail.From);  
  20.                 mail.Subject = _objModelMail.Subject;  
  21.                 string Body = _objModelMail.Body;  
  22.                 mail.Body = Body;  
  23.                 mail.IsBodyHtml = true;  
  24.                 SmtpClient smtp = new SmtpClient();  
  25.                 smtp.Host = "smtp.gmail.com";  
  26.                 smtp.Port = 587;  
  27.                 smtp.UseDefaultCredentials = false;  
  28.                 smtp.Credentials = new System.Net.NetworkCredential("username""password"); // Enter seders User name and password  
  29.                 smtp.EnableSsl = true;  
  30.                 smtp.Send(mail);  
  31.                 return View("Index", _objModelMail);  
  32.             } else {  
  33.                 return View();  
  34.             }  
  35.         }  
  36.     }  
  37. }  

Index.cshtml

  1. @model SendMail.Models.MailModel  
  2. @{  
  3. ViewBag.Title = "Index";  
  4. }  
  5. <h2>Index</h2>  
  6. <fieldset>  
  7.    <legend>  
  8.       Send Email  
  9.    </legend>  
  10.    @using (Html.BeginForm())  
  11.       {  
  12.       @Html.ValidationSummary()  
  13.       <p>From: </p>  
  14.       <p>@Html.TextBoxFor(m=>m.From)</p>  
  15.       <p>To: </p>  
  16.       <p>@Html.TextBoxFor(m=>m.To)</p>  
  17.       <p>Subject: </p>  
  18.       <p>@Html.TextBoxFor(m=>m.Subject)</p>  
  19.       <p>Body: </p>  
  20.       <p>@Html.TextAreaFor(m=>m.Body)</p>  
  21.    <input type ="submit" value ="Send" />  
  22.    } </fieldset> 
In the code above we have the following 3 fields:

 

  • To
  • Subject
  • Message

When the user clicks the "Send" button, the mail will be sent to the specified mail address that you provide in the To TextBox. So add the following code for the [HttpPost] Method for the send button click.

SendMailerController.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net.Mail;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace SendMail.Controllers {  
  8.     public class SendMailerController: Controller {  
  9.         //  
  10.         // GET: /SendMailer/   
  11.         public ActionResult Index() {  
  12.                 return View();  
  13.             }  
  14.             [HttpPost]  
  15.         public ViewResult Index(SendMail.Models.MailModel _objModelMail) {  
  16.             if (ModelState.IsValid) {  
  17.                 MailMessage mail = new MailMessage();  
  18.                 mail.To.Add(_objModelMail.To);  
  19.                 mail.From = new MailAddress(_objModelMail.From);  
  20.                 mail.Subject = _objModelMail.Subject;  
  21.                 string Body = _objModelMail.Body;  
  22.                 mail.Body = Body;  
  23.                 mail.IsBodyHtml = true;  
  24.                 SmtpClient smtp = new SmtpClient();  
  25.                 smtp.Host = "smtp.gmail.com";  
  26.                 smtp.Port = 587;  
  27.                 smtp.UseDefaultCredentials = false;  
  28.                 smtp.Credentials = new System.Net.NetworkCredential("username""password"); // Enter seders User name and password   
  29.                 smtp.EnableSsl = true;  
  30.                 smtp.Send(mail);  
  31.                 return View("Index", _objModelMail);  
  32.             } else {  
  33.                 return View();  
  34.             }  
  35.         }  
  36.     }  
  37. }  

Understanding the CodE

In the code above we have a,

  1. ViewResult Index(SendMail.Models.MailModel _objModelMail)  

 

user defined method. In this method, we have a parameter of our MailModel object. Now we create a MailMessage object.

  1. MailMessage mail = new MailMessage(); 
MailMessage is the main class for sending mail, it is in the System.Net.Mail namespace.

The MailMessage class has properties, the important ones are:

 

  • To
  • From
  • Cc
  • Bcc
  • Subject
  • Body

So we add our data into specified properties.

  1. mail.To.Add(_objModelMail.To);  
  2.   
  3. mail.From = new MailAddress(_objModelMail.From);  
  4. mail.Subject = _objModelMail.Subject;  
  5. string Body = _objModelMail.Body;  
  6. mail.Body = Body;  

For sending mail we need a SMTP Server, so in ASP.Net we have the SmtpClient class, we set the SMTP settings using the properties of that class.

  1. SmtpClient smtp = new SmtpClient();  
The SMTPClient class has these basic properties:

 

  • Host
  • Port
  • UseDefaultCredential
  • Credentials
  • EnableSsl
  • Send

  1. smtp.Host = "smtp.gmail.com";  
  2. smtp.Port = 587;  
  3. smtp.UseDefaultCredentials = false;  
  4. smtp.Credentials = new System.Net.NetworkCredential("username""password");  
  5. smtp.EnableSsl = true;  
In the code above is,

 

  1. smtp.Host = "smtp.gmail.com";  

 

That is the SMTP Host address of Gmail, if you want to use any other SMTP host service then please add a different SMTP host protocol, for example for Hotmail it is smtp.live.com.

For example, in,

Smtp.Port=587

587 is the port for Gmail, so for any other service port you need to change the port correspondingly.

  1. smtp.Credentials = new System.Net.NetworkCredential("username""password");  
Smtp.Credentials specifies the Network Crendentials of your Gmail id so please add your username and password instead of ("username", "password");

 

The following is for a secure mail server, so you enable your SSL layer.

  1. smtp.EnableSsl = true;  
Smtp.Send sends the mail so please add your MailMesssage object here. Then, based on the properties, your mail will be sent.

No comments:

Post a Comment