title: Sending email with System.Net.Mail.SmtpClient in .Net 4/C# with TLS and SmtpAuth, works locally but not on production server(win 2008 r2)
body: <p>I run this code successfully on my development machine (Win 7, VS 2010)</p>
  
  <pre><code>SmtpClient client = new SmtpClient("servername.domain.tld");
  client.Port = 25;
  client.Credentials = new NetworkCredential("username", "password");
  client.EnableSsl = true;
  client.Send(new MailMessage("destination@domain.tld", "sender@domain.tld", "subject", "body"));
  </code></pre>
  
  <p>My smtp server is Exim 4.7 running on debian with TLS and SmtpAuth enabled. It doesn't support relaying but if a user authenticates he can send mail to anyone.</p>
  
  <p>When I run the above code on our Win 2008 R2 production server I get this error message:</p>
  
  <blockquote>
    <p>System.Net.Mail.SmtpException: Server does not support secure connections.</p>
  </blockquote>
  
  <p>When I google this I read allot about connecting to port 465, etc. but this is not legacy SMTP over SSL aka. SSMTP/SMTPS. You can read more here: <a href="http://www.exim.org/exim-html-current/doc/html/spec_html/ch39.html" rel="nofollow">http://www.exim.org/exim-html-current/doc/html/spec_html/ch39.html</a></p>
  
  <p>So what I can understand port 25 <strong>is</strong> correct also it does work on my dev machine with port 25, TLS, etc.</p>
  
  <p>The other thing that shows up when I google this is to try with</p>
  
  <blockquote>
    <p>client.EnableSsl = false;</p>
  </blockquote>
  
  <p>when I do this I get the following error:</p>
  
  <blockquote>
    <p>System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: relay not permitted</p>
  </blockquote>
  
  <p>Which is correct as because if the client doesn't authenticate we don't relay mail.</p>
  
  <p>Here's the smtp server output from a failing session from the server:</p>
  
  <pre>2011-10-21 11:23:43 [2022] SMTP connection from [source.ip]:51712 I=[smtp.ip]:25 (TCP/IP connection count = 1)
  2011-10-21 11:23:48 [2072] ident connection to source.ip timed out
  2011-10-21 11:23:48 [2072] SMTP connection from servername.domain.tld (servername) [source.ip]:51712 I=[smtp.ip]:25 lost
  2011-10-21 11:23:48 [2072] no MAIL in SMTP connection from servername.domain.tld (servername) [source.ip]:51712 I=[smtp.ip]:25 D=0s C=EHLO
  </pre>
  
  <p>Here's the smtp server output from a working session from my dev machine:</p>
  
  <pre>
  2011-10-21 12:04:05 [2022] SMTP connection from [source.ip]:63294 I=[smtp.ip]:25 (TCP/IP connection count = 1)
  2011-10-21 12:04:10 [2077] ident connection to source.ip timed out
  2011-10-21 12:04:11 [2077] 1RHBxT-0000XV-7P  for to@domain.tld
  2011-10-21 12:04:11 [2079] cwd=/var/spool/exim4 3 args: /usr/sbin/exim4 -Mc 1RHBxT-0000XV-7P
  2011-10-21 12:04:12 [2079] 1RHBxT-0000XV-7P => to@domain.tld F= P= R=dnslookup T=remote_smtp S=1058 H=mx2.hotmail.com [65.55.37.104]:25 C="250   Queued mail for delivery" QT=1s DT=1s
  2011-10-21 12:04:12 [2079] 1RHBxT-0000XV-7P Completed QT=1s
  </pre>
  
  <p>Anyone have any insights into what I can do to make this work?</p>
  
  <p>Thanks, goddang</p>