private bool SendStartTls() { IAsyncResult result = StartTlsCommand.BeginSend(_connection, SendStartTlsCallback, this); if (result.CompletedSynchronously) { StartTlsCommand.EndSend(result); TlsStreamAuthenticate(); return true; } return false; }
private static void SendEHelloCallback(IAsyncResult result) { if (!result.CompletedSynchronously) { SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState; try { try { asyncState.connection.extensions = EHelloCommand.EndSend(result); asyncState.connection.ParseExtensions(asyncState.connection.extensions); if (asyncState.connection.pooledStream.NetworkStream is TlsStream) { asyncState.Authenticate(); return; } } catch (SmtpException exception) { if ((exception.StatusCode != SmtpStatusCode.CommandUnrecognized) && (exception.StatusCode != SmtpStatusCode.CommandNotImplemented)) { throw exception; } if (!asyncState.SendHello()) { return; } } if (asyncState.connection.EnableSsl) { if (!asyncState.connection.serverSupportsStartTls && !(asyncState.connection.pooledStream.NetworkStream is TlsStream)) { throw new SmtpException(SR.GetString("MailServerDoesNotSupportStartTls")); } asyncState.SendStartTls(); } else { asyncState.Authenticate(); } } catch (Exception exception2) { asyncState.InvokeCallback(exception2); } } }