title: Sending email: asp.net mvc4
body: <p>I am trying to send mails from my application to different recipients as entered by the user. </p>
  
  <p>My model class,</p>
  
  <pre><code>public class Mail
   {
      public int MailId { get; set; }
  
      public string Mail_To { get; set; }
  
      public string Mail_From { get; set; }
  
      public string Mail_Subject { get; set; }
  
      public string Mail_Contents { get; set; }
  
      public DateTime Mail_Date { get; set; }
  
      [ForeignKey("Anganwadi")]
      public int AnganwadiId { get; set; }
      public virtual Anganwadi Anganwadi { get; set; }
  }
  </code></pre>
  
  <p>My controller looks like,</p>
  
  <pre><code>[HttpPost]
      public ViewResult SendMail(Mail mail) 
      {
          try
          {
  if (ModelState.IsValid)
              {
                  mail.Mail_Date = System.DateTime.Now;
                  mail.AnganwadiId = 1;
                  mail.Mail_From = "k@gmail.com";
                  mail.Mail_Subject = "From Anganwadi";
                  MailMessage message = new MailMessage();
                  message.From = new MailAddress(mail.Mail_From);
                  message.To.Add(new MailAddress("v@gmail.com"));
                  message.Subject = mail.Mail_Subject;
                  message.Body = mail.Mail_Contents;
  
                  SmtpClient client = new SmtpClient();
                  client.Send(message);
                  return View("Thanks", mail);
              }
              else
              {
                  return View();
              }
          }
          catch (Exception e)
          {
              return View(e);
          }
      }
  </code></pre>
  
  <p>In my web config i added following</p>
  
  <pre><code>&lt;system.net&gt;
    &lt;mailSettings&gt;
      &lt;smtp from="test@foo.com"&gt;
        &lt;network host="smtp.efernssolutions.com" port="587" defaultCredentials="true" /&gt;
      &lt;/smtp&gt;
    &lt;/mailSettings&gt;
  &lt;/system.net&gt;
  </code></pre>
  
  <p>Gets an error </p>
  
  <pre><code>Mailbox unavailable. The server response was: authsmtp12: Host 117.196.165.214: No unauthenticated relaying permitted
  </code></pre>
  
  <p>Am not sure this is correct way. please help me.</p>
  
  <p>Thanks,</p>