title: Sending email from a .net application via a mail server with a self-signed SSL certificate
body: <p>I installed hmailserver 5.3.2 and configured it. It receives and sends
  emails normally, but I wanted to use it to send emails from a .net/C#
  application located in another server, and for that I wanted to use
  SSL communication. Before, the application was configured to send
  emails via gmail, on port 587 and it worked ok, but now we want to use
  our own mail server. We first configured the application to connect on
  smtp.domain.com on port 25 and that works, it sends the email.</p>
  
  <p>Then we
  created a self signed certificate to test the if we could send the
  message through a secure channel.I created the certificate with
  openSSL setting common name as: mail.domain.com, smtp.domain.com,
  *.domain.com, domain.com. I opened port 587 on the firewall and
  configured hmailserver to use a certificate for inbound connections on
  that port.
  None of the certificates I created worked (I tried one and then
  created another one and so on), generating the following (generic) exception in the
  application:</p>
  
  <pre><code>System.Exception: _COMPlusExceptionCode = -532459699
  </code></pre>
  
  <p>Of course I also tried to connect via telnet: telnet smtp.domain.com
  587, and I just got a blank screen. It is not a firewall issue since
  when I disable the ssl on port 587 I can connect normally.
  Looking at the log doesn't even show an attempt to connect when using
  587 with SSL.</p>
  
  <p>I already checked these questions: <a href="https://stackoverflow.com/questions/2153790/getting-smtpclient-to-work-with-a-self-signed-ssl-certificate">Getting SmtpClient to work with a self signed SSL certificate</a> and <a href="https://stackoverflow.com/questions/526711/using-a-self-signed-certificate-with-nets-httpwebrequest-response">Using a self-signed certificate with .NET’s HttpWebRequest/Response</a>, but it didn't solve my problem. The approach with <a href="http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.servercertificatevalidationcallback.aspx" rel="nofollow noreferrer">ServerCertificateValidationCallback</a> didn't have any influence.</p>
  
  <p>I tried with ports 25 (which is also proposed in one of the questions above), 465, 587, and with all 3 it happens the same: The initial handshake (SYN / SYN-ACK / ACK) and after about 80s the connection is closed (FIN), nothing in between. </p>
  
  <p>Do I have to install that certificate somewhere so the .net application sees it as trusted? I mean, I already installed it as a Trusted Root Certification Authority and could check by running mmc, so I have no idea where to go now...</p>
  
  <p>Thanks for the help!</p>
  
  <p>PS: Not sure if this belongs to ServerFault since it concerns a C# application but also a mail server...</p>
  
  <p>EDIT: Code sample:</p>
  
  <pre><code>ServicePointManager.ServerCertificateValidationCallback = 
  (sender, certificate, chain, sslPolicyErrors) =&gt; true;
  
  SmtpClient mailClient = new SmtpClient("smtp.domain.com");
  mailClient.Credentials = new NetworkCredential("username@domain.com", "pwd");
  mailClient.Port = 587;
  mailClient.EnableSsl = true;
  MailMessage mailMessage = new MailMessage("mailAddressFrom", "mailAddressTo", "subject", "body");
  mailMessage.IsBodyHtml = true;
  mailClient.Send(mailMessage);
  </code></pre>
  
  <p>EDIT 2: Log (Based on Ramunas' suggestion):</p>
  
  <pre><code>"TCPIP" 3588    "2010-06-23 10:02:49.685"   "TCPConnection - Posting AcceptEx on 0.0.0.0:465"
  "DEBUG" 3588    "2010-06-23 10:02:49.809"   "Creating session 24039"
  "TCPIP" 772 "2010-06-23 10:04:29.639"   "TCPConnection - SSL handshake with client failed. Error code: 2, Message: End of file, Remote IP: X"
  "DEBUG" 772 "2010-06-23 10:04:29.639"   "Ending session 24039"
  </code></pre>