internal void GetConnection(ServicePoint servicePoint)
{
if (this.isConnected)
{
throw new InvalidOperationException(SR.GetString("SmtpAlreadyConnected"));
}
if (Logging.On)
{
Logging.Associate(Logging.Web, this, servicePoint);
}
this.connectionPool = ConnectionPoolManager.GetConnectionPool(servicePoint, "", m_CreateConnectionCallback);
PooledStream pooledStream = this.connectionPool.GetConnection(this, null, this.Timeout);
while ((((SmtpPooledStream) pooledStream).creds != null) && (((SmtpPooledStream) pooledStream).creds != this.credentials))
{
this.connectionPool.PutConnection(pooledStream, pooledStream.Owner, this.Timeout, false);
pooledStream = this.connectionPool.GetConnection(this, null, this.Timeout);
}
if (Logging.On)
{
Logging.Associate(Logging.Web, this, pooledStream);
}
lock (this)
{
this.pooledStream = pooledStream;
}
((SmtpPooledStream) pooledStream).creds = this.credentials;
this.responseReader = new SmtpReplyReaderFactory(pooledStream.NetworkStream);
pooledStream.UpdateLifetime();
if (((SmtpPooledStream) pooledStream).previouslyUsed)
{
this.isConnected = true;
}
else
{
LineInfo info = this.responseReader.GetNextReplyReader().ReadLine();
if (info.StatusCode != SmtpStatusCode.ServiceReady)
{
throw new SmtpException(info.StatusCode, info.Line, true);
}
try
{
this.extensions = EHelloCommand.Send(this, this.client.clientDomain);
this.ParseExtensions(this.extensions);
}
catch (SmtpException exception)
{
if ((exception.StatusCode != SmtpStatusCode.CommandUnrecognized) && (exception.StatusCode != SmtpStatusCode.CommandNotImplemented))
{
throw exception;
}
HelloCommand.Send(this, this.client.clientDomain);
this.supportedAuth = SupportedAuth.Login;
}
if (this.enableSsl)
{
if (!this.serverSupportsStartTls && !(pooledStream.NetworkStream is TlsStream))
{
throw new SmtpException(SR.GetString("MailServerDoesNotSupportStartTls"));
}
StartTlsCommand.Send(this);
TlsStream stream2 = new TlsStream(servicePoint.Host, pooledStream.NetworkStream, this.clientCertificates, servicePoint, this.client, null);
pooledStream.NetworkStream = stream2;
this.channelBindingToken = stream2.GetChannelBinding(ChannelBindingKind.Unique);
this.responseReader = new SmtpReplyReaderFactory(pooledStream.NetworkStream);
this.extensions = EHelloCommand.Send(this, this.client.clientDomain);
this.ParseExtensions(this.extensions);
}
if (this.credentials != null)
{
for (int i = 0; i < this.authenticationModules.Length; i++)
{
Authorization authorization;
if (this.AuthSupported(this.authenticationModules[i]))
{
NetworkCredential credential = this.credentials.GetCredential(servicePoint.Host, servicePoint.Port, this.authenticationModules[i].AuthenticationType);
if (credential != null)
{
authorization = this.SetContextAndTryAuthenticate(this.authenticationModules[i], credential, null);
if ((authorization != null) && (authorization.Message != null))
{
info = AuthCommand.Send(this, this.authenticationModules[i].AuthenticationType, authorization.Message);
if (info.StatusCode != SmtpStatusCode.CommandParameterNotImplemented)
{
goto Label_0363;
}
}
}
}
continue;
Label_02F2:
authorization = this.authenticationModules[i].Authenticate(info.Line, null, this, this.client.TargetName, this.channelBindingToken);
if (authorization == null)
{
throw new SmtpException(SR.GetString("SmtpAuthenticationFailed"));
}
info = AuthCommand.Send(this, authorization.Message);
if (info.StatusCode == ((SmtpStatusCode) 0xeb))
{
this.authenticationModules[i].CloseContext(this);
this.isConnected = true;
return;
}
Label_0363:
if (info.StatusCode == ((SmtpStatusCode) 0x14e))
{
goto Label_02F2;
}
}
}
this.isConnected = true;
}
}