title: Sending an automatic email in MVC
body: <p>I'm attempting to send the results of a form in email form to a set destination. I've given this a number of attempts myself and failed miserably. So I found a question that was asking the same and adapted the answer to fit my solution... I think.. </p>
  
  <p>The Answer I've been following is from <a href="https://stackoverflow.com/questions/16665795/asp-net-mvc-4-send-form-from-site-to-email"><strong>asp.net mvc 4 Send form from site to email</strong></a></p>
  
  <p>When I click the submit button on my form, it retrieves the data no problem. The issue is in sending the email. </p>
  
  <p>My code currently looks like this...</p>
  
  <p>Model:</p>
  
  <pre><code>namespace Domain.Models
  {
      public class Event
      {
  
          public int? ArrangerId { get; set; }
          public Arranger Arranger { get; set; }
      }
  }
  </code></pre>
  
  <p>It carries on like that. It quite a long list but it's all the same.</p>
  
  <p>View: </p>
  
  <pre><code>@model Domain.Models.Event
  @{
      ViewBag.Title = "Index";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  @using (Html.BeginForm("Submit", "EventSubmission"))
  {
      &lt;h3&gt;Your contact details&lt;/h3&gt;
  
      &lt;h5&gt;Name&lt;/h5&gt;
      @Html.TextBoxFor(o =&gt; o.Arranger.Contact_Name);
      &lt;br /&gt;
      &lt;h5&gt;Email address&lt;/h5&gt;
      @Html.TextBoxFor(o =&gt; o.Arranger.Contact_Phone);
      &lt;br/&gt;
      &lt;h5&gt;Phone number&lt;/h5&gt;
      @Html.TextBoxFor(o =&gt; o.Arranger.Contact_Email);
      ......
  }
  </code></pre>
  
  <p>Again, carries on in the same way</p>
  
  <p>Controller: </p>
  
  <pre><code>namespace EventsMap.Controllers
  {
      public class EventSubmissionController : Controller
      {
          public ActionResult Index()
          {
              return View(new Event());
          }
  
  
  
          [HttpGet]
          public ActionResult Submit()
          {
              return View();
          }
  
  
          [HttpPost]
          public ActionResult Submit(Event ev)
          {
              string Text = "&lt;html&gt; &lt;head&gt; &lt;/head&gt;" +
              " &lt;body style= \" font-size:12px; font-family: Arial\"&gt;" +
              ev.Location.Postcode +
              "&lt;/body&gt;&lt;/html&gt;";
  
              SendEmail("TestEmail@gmail.com", Text);
              Event tempForm = new Event();
              return View(tempForm);
          }
  
  
          public static bool SendEmail(string SentTo, string Text)
          {
              MailMessage msg = new MailMessage();
  
              msg.From = new MailAddress("TestEmail@gmail.com");
              msg.To.Add(SentTo);
              msg.Subject = "subject";
              msg.Body = Text;
              msg.Priority = MailPriority.High;
              msg.IsBodyHtml = true;
  
              SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
  
  
  
              client.UseDefaultCredentials = false;
              client.EnableSsl = false;
              client.DeliveryMethod = SmtpDeliveryMethod.Network;
              //client.EnableSsl = true;
  
              try
              {
                  client.Send(msg);
              }
              catch (Exception)
              {
                  return false;
              }
              return true;
          }
  
  
      }
  }
  </code></pre>
  
  <blockquote>
    <p>The exception that I get back is "Failure sending mail", "Unable to
    connect to the remote server", "An attempt was made to access a socket
    in a way forbidden by its access permissions 64.233.166.109:587"</p>
  </blockquote>
  
  <p>So clearly it is something wrong with the <code>smtp</code> business but other than that i'm lost.</p>