title: SmtpClient: The remote certificate is invalid according to the validation procedure. Exchange server
body: <p>My client is using an Exchange mail server to send out emails from a ASP.NET MVC website.  It was working before now we're running into an error.  We are able to Telnet to the email server on port 5877 so not blocked by firewall. Not sure what the issue might be... maybe cert related for the exchange server?</p>
  <pre><code>General Exception
  StackTrace: at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)
  at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
  at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
  at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
  at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
  at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
  at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
  at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
  at System.Net.Mail.SmtpConnection.Flush()
  at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn)
  at System.Net.Mail.EHelloCommand.Send(SmtpConnection conn, String domain)
  at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
  at System.Net.Mail.SmtpClient.GetConnection()
  at System.Net.Mail.SmtpClient.Send(MailMessage message)
  at ClientWebsite.Mail.send_Exchange(String to, String subject, String message, Boolean debug, Int32 sender_param) in C:\Work\Visual Studio Projects\ClientWebsite\ClientWebsite\App_Data\Mail.cs:line 154System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size) at System.Net.Mail.SmtpConnection.Flush() at System.Net.Mail.ReadLinesCommand.Send(SmtpConnection conn) at System.Net.Mail.EHelloCommand.Send(SmtpConnection conn, String domain) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) at ClientWebsite.Mail.send_Exchange(String to, String subject, String message, Boolean debug, Int32 sender_param) in C:\Work\Visual Studio Projects\ClientWebsite\ClientWebsite\App_Data\Mail.cs:line 154
  </code></pre>
  <pre><code>
             SmtpClient client = new SmtpClient(); 
             client = new SmtpClient(smtpSection.Network.Host, smtpSection.Network.Port);
             client.EnableSsl = true;
             client.Credentials = new NetworkCredential(smtpSection.Network.UserName, smtpSection.Network.Password);
             var mailMessage = new MailMessage
              {
                  IsBodyHtml = true,
                  Subject = subject,
                  Body = message
              };
  
              mailMessage.To.Add(to);
              mailMessage.From = new MailAddress(smtpSection.From);
  
              try
              {
                  client.Send(mailMessage);
              }
              catch (SmtpFailedRecipientException se)
              {
                  if (debug)
                  {
                      System.Web.HttpContext.Current.Response.Write(&quot;SmtpFailedRecipientException&lt;BR&gt;&quot;);
                      System.Web.HttpContext.Current.Response.Write(&quot;StackTrace:&quot; + se.StackTrace.Replace(System.Environment.NewLine, &quot;&lt;br&gt;&quot;));
                      // variable se is already the right type, so no need to cast it      
                      System.Web.HttpContext.Current.Response.Write(&quot;FailedRecipient:&quot; + se.FailedRecipient + &quot;&lt;Hr&gt;&quot;);
                      System.Web.HttpContext.Current.Response.Write(se.ToString());
                      System.Web.HttpContext.Current.Response.End();
                  }
              }
              catch (Exception e)
              {
                  if (debug)
                  {
                      System.Web.HttpContext.Current.Response.Write(&quot;General Exception&lt;BR&gt;&quot;);
                      System.Web.HttpContext.Current.Response.Write(&quot;StackTrace:&quot; + e.StackTrace.Replace(System.Environment.NewLine, &quot;&lt;br&gt;&quot;));
                      // variable se is already the right type, so no need to cast it      
                      System.Web.HttpContext.Current.Response.Write(e.ToString());
                      System.Web.HttpContext.Current.Response.End();
                  }
  
              }
  
  
          }
  </code></pre>