/// <summary> /// This method is called when asynchronous Connect method completes. /// </summary> /// <param name="ar">An IAsyncResult that stores state information and any user defined data for this asynchronous operation.</param> private void ConnectCallback(IAsyncResult ar) { try{ m_pSmtpClient.EndConnect(ar); // Start TLS requested, start switching to SSL. if (m_pActiveTarget.SslMode == SslMode.TLS) { m_pSmtpClient.BeginStartTLS(new AsyncCallback(this.StartTlsCallback), null); } // Authentication requested, start authenticating. else if (!string.IsNullOrEmpty(m_pActiveTarget.UserName)) { m_pSmtpClient.BeginAuthenticate(m_pActiveTarget.UserName, m_pActiveTarget.Password, new AsyncCallback(this.AuthenticateCallback), null); } else { long messageSize = -1; try{ messageSize = m_pRelayItem.MessageStream.Length - m_pRelayItem.MessageStream.Position; } catch { // Stream doesn't support seeking. } m_pSmtpClient.BeginMailFrom( this.From, messageSize, IsDsnSupported() ? m_pRelayItem.DSN_Ret : SMTP_DSN_Ret.NotSpecified, IsDsnSupported() ? m_pRelayItem.EnvelopeID : null, new AsyncCallback(this.MailFromCallback), null ); } } catch (Exception x) { try{ // Release IP usage. m_pServer.RemoveIpUsage(m_pActiveTarget.Target.Address); m_pActiveTarget = null; // Connect failed, if there are more target IPs, try next one. if (!this.IsDisposed && !this.IsConnected && m_pTargets.Count > 0) { BeginConnect(); } else { Dispose(x); } } catch (Exception xx) { Dispose(xx); } } }