title: How to send email from controller in MVC 4 .net?
body: <p>I have following code which works fine if I host the website in IIS of my local system. But if I host the website on my Windows Server, it doesn't send mail.</p>
  
  <p>Code of Controller:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Index()
  {
      string tomail = "test@godaddyserver.com";
      string SMTPServer = "smtpout.secureserver.net";
      string SMTPUserName = "test@godaddyserver.com";
      string SMTPPassword = "test123456";
      string strMailTitle = "Inquiry Mail";
      MailMessage mail = new MailMessage();
      SmtpClient SmtpServer = new SmtpClient(SMTPServer);
      SmtpServer.Credentials = new NetworkCredential(SMTPUserName, SMTPPassword);
      mail.From = new MailAddress(tomail, strMailTitle);
      SmtpServer.Port = 25;
      mail.To.Add(tomail);
      mail.Subject = "Inquiry Mail";
      mail.IsBodyHtml = true;
  
      string mailTemplatePath = Server.MapPath("~/template/AdminContactUs.html");
      StringBuilder MyStringBuilder = new StringBuilder();
  
      if (System.IO.File.Exists(mailTemplatePath))
          MyStringBuilder.Append(System.IO.File.ReadAllText(mailTemplatePath));
  
      mail.Body = MyStringBuilder.ToString();
      try
      {
          SmtpServer.ServicePoint.MaxIdleTime = 1;
          SmtpServer.Send(mail);
      }
      catch (Exception e)
      {
  
      }
  }
  </code></pre>
  
  <p>One thing I forgot to add is that it is working fine with Exchange Server email(test@mydomain.com) and SMTP(smtp1.mydomain.com). Current email and SMTP is of Godaddy. However, it is not working with Godaddy, Hostgator or Gmail.</p>
  
  <p>Can anybody please suggest me if I am missing anything?</p>