title: Sending mail with SmtpClient not working, while running the same code in different projects does
body: <p>I am working in Visual Studio 2012, On Windows 7,  I run the program on a Windows Server 2008 R2 machine, both are 64-bit.  I have been using the same code in many processes and I always has worked fine.  However, recently when I use the same code now I get the following exception,</p>
  
  <pre><code>System.Net.Mail.SmtpException: Failure sending mail. ---&gt;
  System.Net.WebException: Unable to connect to the remote server ---&gt; 
  System.Net.Sockets.SocketException: An attempt was made to access a socket
  in a way forbidden by its access permissions xxx.xxx.xxx.xxx:25
  </code></pre>
  
  <p>And the code,</p>
  
  <pre><code>public static void emailReport(string to, string from, string subject, 
      string body, string attachment, string hostAddress)
      {
          MailMessage mail = new MailMessage();
          SmtpClient SmtpServer = new SmtpClient(hostAddress);
          foreach (string email in to.Split(','))
              mail.To.Add(new MailAddress(email));
          mail.From = new MailAddress(from);
          mail.Subject = subject;
          mail.Body = body;
          mail.IsBodyHtml = true;
          SmtpServer.Send(mail);
      }
  </code></pre>
  
  <ul>
  <li>Older processes that use this exact code still work and can send
  mail, using the same host address and port. </li>
  <li>Looking at the sockets
  with <em>netstat -o</em>, I can see that the socket is not being held by any
  process.</li>
  <li>.net Framework 4.5 is on both my local machine and the server.</li>
  <li>I have Visual Studio 2012 on the server and have tried recreating the project there to see if there is any change, there wasn't.</li>
  <li>I can connect, on the server, to the mail host using <em>telnet smtp.mydomain.com 25</em></li>
  </ul>
  
  <p>Essentially starting this week any new versions of applications (.exe) using this code don't send email, but old versions of the same applications do.
  I suspect this is a problem with the environment I am running the process in or perhaps the way I am building the project.  I am not sure how to fix this problem.</p>