Sending Email In ASP.NET MVC

In any web application, we may need to send Email for feedback, for messages, or for any other purpose. In this blog, I will show how to do it step by step.

  1. Start a new MVC project.
  2. Give it some name.
  3. Select ASP.NET template.
  4. Open your Home Controller (or any controller)
  5. In Controller, create an ActionResult method SendEmail (you can give it any name).
    1. public ActionResult SendEmail() {  
    2.     return View();  
    3. }  
  6. Add an empty View for this action method and add the following code.
    1. @ {  
    2.     ViewBag.Title = "SendEmail";  
    3. } < h2 > SendEmail < /h2> < br / > < br / > @if(ViewBag.Error != null) { < h2 > @ViewBag.Error < /h2>  
    4. } < form method = "post"  
    5. action = "SendEmail" > < div class = "container" > < span class = "form-control-static" > Receiver: < /span> < input class = "form-control"  
    6. type = "text"  
    7. name = "receiver" / > < br / > < span class = "form-control-static" > Subject: < /span> < input class = "form-control"  
    8. type = "text"  
    9. name = "subject" / > < br / > < span class = "form-control-static" > Message: < /span> < input class = "form-control"  
    10. type = "text"  
    11. name = "message" / > < br / > < input class = "btn btn-primary"  
    12. type = "submit"  
    13. value = "Send" / > < /div> < /form>  
  7. Now, add another action method with the same name and Httppost request as following,
    1. [HttpPost]  
    2. public ActionResult SendEmail(string receiver, string subject, string message) {  
    3.     try {  
    4.         if (ModelState.IsValid) {  
    5.             var senderEmail = new MailAddress("[email protected]""Jamil");  
    6.             var receiverEmail = new MailAddress(receiver, "Receiver");  
    7.             var password = "Your Email Password here";  
    8.             var sub = subject;  
    9.             var body = message;  
    10.             var smtp = new SmtpClient {  
    11.                 Host = "smtp.gmail.com",  
    12.                     Port = 587,  
    13.                     EnableSsl = true,  
    14.                     DeliveryMethod = SmtpDeliveryMethod.Network,  
    15.                     UseDefaultCredentials = false,  
    16.                     Credentials = new NetworkCredential(senderEmail.Address, password)  
    17.             };  
    18.             using(var mess = new MailMessage(senderEmail, receiverEmail) {  
    19.                 Subject = subject,  
    20.                     Body = body  
    21.             }) {  
    22.                 smtp.Send(mess);  
    23.             }  
    24.             return View();  
    25.         }  
    26.     } catch (Exception) {  
    27.         ViewBag.Error = "Some Error";  
    28.     }  
    29.     return View();  
    30. }  
  8. Be sure that the strings in the arguments of Httppost method are same with that of name of inputs in the View because with these strings, inputs will be accessed from the View. And, write your password of Gmail Account in the blank space. Emails will be sent using this account.

  9. Now, save your project. It will look like the following in the browser.

Fill the fields and check for Email. It will work.
Ebook Download
View all
Learn
View all