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. namespace SendMailUsingMVC4.Models
  6. {
  7. public class MyMailModel
  8. {
  9. public string To { get; set; }
  10. public string Subject { get; set; }
  11. public string Body { get; set; }
  12. }
  13. }
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. namespace SendMailUsingMVC4.Controllers
  11. {
  12. public class MailMessagingController : Controller
  13. {
  14. //
  15. // GET: /MailMessaging/
  16. public ActionResult Index()
  17. {
  18. return View();
  19. }
  20. [HttpPost]
  21. public ActionResult Index( MyMailModel objModelMail, HttpPostedFileBase fileUploader)
  22. {
  23. if (ModelState.IsValid)
  24. {
  25. string from = "[email protected]"; //any valid GMail ID
  26. using (MailMessage mail = new MailMessage(from, objModelMail.To))
  27. {
  28. mail.Subject = objModelMail.Subject;
  29. mail.Body = objModelMail.Body;
  30. if (fileUploader != null)
  31. {
  32. string fileName = Path.GetFileName(fileUploader.FileName);
  33. mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName));
  34. }
  35. mail.IsBodyHtml = false;
  36. SmtpClient smtp = new SmtpClient();
  37. smtp.Host = "smtp.gmail.com";
  38. smtp.EnableSsl = true;
  39. NetworkCredential networkCredential = new NetworkCredential(from, "Gmail Id Password");
  40. smtp.UseDefaultCredentials = true;
  41. smtp.Credentials = networkCredential;
  42. smtp.Port = 587;
  43. smtp.Host = "localhost";
  44. smtp.Send(mail);
  45. ViewBag.Message = "Sent";
  46. return View("Index", objModelMail);
  47. }
  48. }
  49. else
  50. {
  51. return View();
  52. }
  53. }
  54. }
  55. }
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. ViewBag.Title = "Sending Mail In MVC4";
  4. }
  5. <script src="~/Scripts/jquery-1.7.1.min.js"></script>
  6. <script>
  7. $(document).ready(function () {
  8. if ('@ViewBag.Message' == 'Sent') {
  9. alert('Mail has been sent successfully');
  10. }
  11. });
  12. </script>
  13. <fieldset>
  14. <legend>Send Email
  15. </legend>
  16. @using (@Html.BeginForm("Index", "MailMessaging", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" }))
  17. {
  18. @Html.ValidationSummary()
  19. <table style="background-color:aliceblue;">
  20. <tr>
  21. <td> To:
  22. </td>
  23. <td>
  24. @Html.TextBoxFor(m => m.To)
  25. </td>
  26. </tr>
  27. <tr>
  28. <td>Subject:
  29. </td>
  30. <td>
  31. @Html.TextBoxFor(m => m.Subject)
  32. </td>
  33. </tr>
  34. <tr>
  35. <td>Attachment
  36. </td>
  37. <td>
  38. <input type="file" name="fileUploader" />
  39. </td>
  40. </tr>
  41. <tr>
  42. <td>Mail Body:
  43. </td>
  44. <td>
  45. @Html.TextAreaFor(m => m.Body)
  46. </td>
  47. </tr>
  48. <tr>
  49. <td></td>
  50. <td>
  51. <input type="submit" value="Send" style="width: 100px;" /></td>
  52. </tr>
  53. </table>
  54. }
  55. </fieldset>
Now run your application:

output
Image 9