title: Issue sending mail message from ASP.NET MVC application hosted on GoDaddy
body: <p>I have a form on a MVC Web Application that is hosted over at GoDaddy that users can fill out and send to our office. I am currently testing it using both a Gmail account and a GoDaddy email account (linked to my hosting space).  With the Gmail code in place, the email will send fine from my localhost, but when I publish it to the web I get the following error:</p>
  
  <blockquote>
    <p>Request for the permission of type 'System.Net.Mail.SmtpPermission,
    System, Version=2.0.0.0, Culture=neutral,
    PublicKeyToken=b77a5c561934e089' failed.</p>
  </blockquote>
  
  <p>Here is the code (borrowed from <a href="https://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail/32336#32336">this post</a>) I am using, credentials have been changed and password removed:</p>
  
  <pre><code>var fromAddress = new MailAddress("iihs.eval@gmail.com", "FEA Drone");
  var toAddress = new MailAddress("improveithomeservices@gmail.com", "ImproveIt Home Services");
  const string fromPassword = "&lt;removed&gt;";
  var subject = string.Format("Energy Evaluation Request for {0} {1}", model.FirstName, model.LastName);
  var body = MailBody(results);
  
  var smtp = new SmtpClient
  {
      Host = "smtp.gmail.com",
      Port = 587,
      EnableSsl = true,
      DeliveryMethod = SmtpDeliveryMethod.Network,
      UseDefaultCredentials = false,
      Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  };
  using (var message = new MailMessage(fromAddress, toAddress)
  {
      Subject = subject,
      Body = body
  })
  {
      smtp.Send(message);
  }    
  </code></pre>
  
  <p>I then tried to use my GoDaddy email that I set up for this particular form, and again locally it sends. However, when this one is uploaded it just times out rather than give me any sort of useful information. Here is that code:</p>
  
  <pre><code>var fromAddress = new MailAddress("fea@goimproveit.com", "FEA Drone");
  var toAddress = new MailAddress("improveithomeservices@gmail.com", "ImproveIt! Home Services");
  const string fromPassword = "&lt;removed&gt;";
  var client = new SmtpClient
  {
      Host = "relay-hosting.secureserver.net",
      Port = 25,
      EnableSsl = false,
      DeliveryMethod = SmtpDeliveryMethod.Network,
      UseDefaultCredentials = false,
      Timeout = 20000,
      Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
  };
  
  using (var msg = new MailMessage(fromAddress, toAddress)
  {
      Subject = string.Format("Energy Evaluation Request for {0} {1}", model.FirstName, model.LastName),
      IsBodyHtml = false,
      Body = MailBody(results)
  })
  {
      client.Send(msg);
  }
  </code></pre>
  
  <p>Originally I had <code>smtpout.secureserver.net</code> for my GoDaddy Host, but I found out from <a href="http://help.godaddy.com/article/955" rel="nofollow noreferrer">this article</a> that I needed to change it to <code>relay-hosting.secureserver.net</code>. With the updated host information, the script runs but the mail message does not make it to the destination email inbox (or spam box).</p>
  
  <h2>Edit</h2>
  
  <p>Using Maxim's code, it seems I have gotten a "functioning" version in place. The email does not immediately appear in the destination inbox, but does so after about 15 minutes. Too bad it seems that GoDaddy is a giant PITA when it comes to programmatic emailing.</p>
  
  <p>Here is what I got:</p>
  
  <pre><code>var emailmessage = new System.Web.Mail.MailMessage()
  {
      Subject = subject,
      Body = body,
      From = fromAddress.Address,
      To = toAddress.Address,
      BodyFormat = MailFormat.Text,
      Priority = System.Web.Mail.MailPriority.High
  };
  
  SmtpMail.SmtpServer = "relay-hosting.secureserver.net";
  SmtpMail.Send(emailmessage);
  </code></pre>
  
  <p>Thanks again for the assistance, I hope that I can figure out how to get GoDaddy to cooperate better. If I can, I will post back with updates.</p>