Send an Email In MVC 4 Application

For this I will create a new project as in the following:

mvc 4 web application
                                                                                 Image 1

internet application
                                                                           Image 2

Now right-click on the Model Folder in the solution and click on "Add" | "New Item...".

Add New Item
                                                                                 Image 3

Add a new class.

add class
                                                                                       Image 4

And here the MyMailModel.cs file has the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace SendMailUsingMVC4.Models  
  7. {  
  8.     public class MyMailModel  
  9.     {  
  10.         public string To { getset; }  
  11.         public string Subject { getset; }  
  12.         public string Body { getset; }  
  13.     }  
  14. }  
Now right-click on the Controller Folder and click on "Add" | "Controller...":

add new controller
                                                                                       Image 5

controller name
                                                                                    Image 6

Now place the following code in MailessageController:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Net.Mail;  
  7.  using System.IO;  
  8. using System.Net;  
  9. using SendMailUsingMVC4.Models;  
  10.   
  11. namespace SendMailUsingMVC4.Controllers  
  12. {  
  13.     public class MailMessagingController : Controller  
  14.     {  
  15.         //  
  16.         // GET: /MailMessaging/  
  17.   
  18.         public ActionResult Index()  
  19.         {  
  20.             return View();  
  21.         }  
  22.   
  23.         [HttpPost]  
  24.         public ActionResult Index( MyMailModel objModelMail, HttpPostedFileBase fileUploader)  
  25.         {  
  26.             if (ModelState.IsValid)  
  27.             {  
  28.                 string from = "[email protected]"//any valid GMail ID  
  29.                 using (MailMessage mail = new MailMessage(from, objModelMail.To))  
  30.                 {  
  31.                     mail.Subject = objModelMail.Subject;  
  32.                     mail.Body = objModelMail.Body;  
  33.                     if (fileUploader != null)  
  34.                     {  
  35.                         string fileName = Path.GetFileName(fileUploader.FileName);  
  36.                         mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));  
  37.                     }  
  38.                     mail.IsBodyHtml = false;  
  39.                     SmtpClient smtp = new SmtpClient();  
  40.                     smtp.Host = "smtp.gmail.com";  
  41.                     smtp.EnableSsl = true;  
  42.                     NetworkCredential networkCredential = new NetworkCredential(from, "Gmail Id Password");  
  43.                     smtp.UseDefaultCredentials = true;  
  44.                     smtp.Credentials = networkCredential;  
  45.                     smtp.Port = 587;  
  46.                     smtp.Host = "localhost";  
  47.                     smtp.Send(mail);  
  48.                     ViewBag.Message = "Sent";  
  49.                     return View("Index", objModelMail);  
  50.                 }  
  51.             }  
  52.             else  
  53.             {  
  54.                 return View();  
  55.             }  
  56.         }  
  57.   
  58.     }  
  59. }  
Now right-click on Index and click "Add View...":

add view
                                                                                    Image 7

my mail model
                                                                       Image 8

Now place the following code in your View:
  1. @model SendMailUsingMVC4.Models.MyMailModel  
  2.   
  3. @{  
  4.     ViewBag.Title = "Sending Mail In MVC4";  
  5. }  
  6. <script src="~/Scripts/jquery-1.7.1.min.js"></script>  
  7. <script>  
  8.     $(document).ready(function () {  
  9.         if ('@ViewBag.Message' == 'Sent') {  
  10.             alert('Mail has been sent successfully');  
  11.         }  
  12.     });  
  13. </script>  
  14. <fieldset>  
  15.     <legend>Send Email  
  16.     </legend>  
  17.     @using (@Html.BeginForm("Index""MailMessaging", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))  
  18.     {  
  19.         @Html.ValidationSummary()  
  20.         <table style="background-color:aliceblue;">  
  21.             <tr>  
  22.                 <td> To:  
  23.                 </td>  
  24.                 <td>  
  25.                     @Html.TextBoxFor(m => m.To)  
  26.                 </td>  
  27.             </tr>  
  28.             <tr>  
  29.                 <td>Subject:  
  30.                 </td>  
  31.                 <td>  
  32.                     @Html.TextBoxFor(m => m.Subject)  
  33.                 </td>  
  34.             </tr>  
  35.             <tr>  
  36.                 <td>Attachment  
  37.                 </td>  
  38.                 <td>  
  39.                     <input type="file" name="fileUploader" />  
  40.                 </td>  
  41.             </tr>  
  42.             <tr>  
  43.                 <td>Mail Body:  
  44.                 </td>  
  45.                 <td>  
  46.                     @Html.TextAreaFor(m => m.Body)  
  47.                 </td>  
  48.             </tr>  
  49.             <tr>  
  50.                 <td></td>  
  51.                 <td>  
  52.                     <input type="submit" value="Send" style="width: 100px;" /></td>  
  53.             </tr>  
  54.         </table>      
  55.     }  
  56. </fieldset>  
Now run your application:

output
                                                                       Image 9

 

Next Recommended Readings