System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult.Authenticate C# (CSharp) Method

Authenticate() private method

private Authenticate ( ) : void
return void
            private void Authenticate()
            {
                //if no credentials were supplied, try anonymous
                //servers don't appear to anounce that they support anonymous login.
                if (_connection._credentials != null)
                {
                    while (++_currentModule < _connection._authenticationModules.Length)
                    {
                        //only authenticate if the auth protocol is supported
                        ISmtpAuthenticationModule module = _connection._authenticationModules[_currentModule];
                        if (!_connection.AuthSupported(module))
                        {
                            continue;
                        }

                        NetworkCredential credential = _connection._credentials.GetCredential(_host, _port, module.AuthenticationType);
                        if (credential == null)
                            continue;
                        Authorization auth = _connection.SetContextAndTryAuthenticate(module, credential, _outerResult);

                        if (auth != null && auth.Message != null)
                        {
                            IAsyncResult result = AuthCommand.BeginSend(_connection, _connection._authenticationModules[_currentModule].AuthenticationType, auth.Message, s_authenticateCallback, this);
                            if (!result.CompletedSynchronously)
                            {
                                return;
                            }

                            LineInfo info = AuthCommand.EndSend(result);

                            if ((int)info.StatusCode == 334)
                            {
                                _authResponse = info.Line;
                                if (!AuthenticateContinue())
                                {
                                    return;
                                }
                            }
                            else if ((int)info.StatusCode == 235)
                            {
                                module.CloseContext(_connection);
                                _connection._isConnected = true;
                                break;
                            }
                        }
                    }
                }

                _connection._isConnected = true;
                InvokeCallback();
            }

Usage Example

 private static void AuthenticateContinueCallback(IAsyncResult result)
 {
     if (!result.CompletedSynchronously)
     {
         SmtpConnection.ConnectAndHandshakeAsyncResult asyncState = (SmtpConnection.ConnectAndHandshakeAsyncResult)result.AsyncState;
         try
         {
             LineInfo info = AuthCommand.EndSend(result);
             if (info.StatusCode == ((SmtpStatusCode)0xeb))
             {
                 asyncState.connection.authenticationModules[asyncState.currentModule].CloseContext(asyncState.connection);
                 asyncState.connection.isConnected = true;
                 asyncState.InvokeCallback();
             }
             else
             {
                 if (info.StatusCode == ((SmtpStatusCode)0x14e))
                 {
                     asyncState.authResponse = info.Line;
                     if (!asyncState.AuthenticateContinue())
                     {
                         return;
                     }
                 }
                 asyncState.Authenticate();
             }
         }
         catch (Exception exception)
         {
             asyncState.InvokeCallback(exception);
         }
     }
 }
All Usage Examples Of System.Net.Mail.SmtpConnection.ConnectAndHandshakeAsyncResult::Authenticate