title: SMTP - SSL Certificate Issue - C# - Why this code works?
body: <p>Now, this question has several versions in Stack Overflow like this <a href="https://stackoverflow.com/questions/777607/the-remote-certificate-is-invalid-according-to-the-validation-procedure-using/18796688">most viewed question</a> in which most of the answers advise the users to turn off SSL as a method to bypass the code.</p>
  
  <p>I was getting the same exception while trying to send email.</p>
  
  <blockquote>
    <p>System.Security.Authentication.AuthenticationException:
    The remote certificate is invalid according to the validation
    procedure.</p>
  </blockquote>
  
  <p>This was my code</p>
  
  <pre><code>private void sendAMail(String toAddress, String messageBody)
          {
              String msg = "Sending mail to : " + toAddress;
  
              MailMessage mail = new MailMessage();
              mail.To.Add(toAddress);
              mail.From = new MailAddress("from@mydomain.com");
              mail.Subject = "Subject: Test Mail";
              mail.Body = messageBody;
              mail.IsBodyHtml = true;            
  
              SmtpClient smtp = new SmtpClient();
  
              smtp.Host = "myhostname.com";            
              smtp.Credentials = new System.Net.NetworkCredential("sender@sample.com", "");
              smtp.EnableSsl = true;
              smtp.Port = 587;            
              smtp.Send(mail);            
          }
  </code></pre>
  
  <p>While trying several things, finally I tried to print the SSL certificate from the server as mentioned here.<a href="https://stackoverflow.com/questions/5109752/use-c-sharp-to-get-an-smtp-server-certificate/%22print%20SSL%20Cert%22">Print SSL Cert</a></p>
  
  <p>Then, the exception is gone.!!! I am not able to figure out why.</p>
  
  <p>This is the code that worked</p>
  
  <pre><code>private void sendAMail(String toAddress, String messageBody)
          {
              String msg = "Sending mail to : " + toAddress;
  
              MailMessage mail = new MailMessage();
              mail.To.Add(toAddress);
              mail.From = new MailAddress("from@mydomain.com");
              mail.Subject = "Subject: Test Mail";
              mail.Body = messageBody;
              mail.IsBodyHtml = true;            
  
              //Added this line here
              System.Net.ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(RemoteServerCertificateValidationCallback);
              SmtpClient smtp = new SmtpClient();
  
              smtp.Host = "myhostname.com";            
              smtp.Credentials = new System.Net.NetworkCredential("sender@sample.com", "");
              smtp.EnableSsl = true;
              smtp.Port = 587;            
              smtp.Send(mail);            
          }
  
  
  private bool RemoteServerCertificateValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
  {
      //Console.WriteLine(certificate);
      return true;
  }
  </code></pre>
  
  <p>Please explain me the reason it works while the original code is still throwing exception.</p>