/// <summary> /// Attempt to connect to the specified MX server. /// </summary> /// <param name="mx">MX Record of the server to connect to.</param> public async Task <bool> ConnectAsync(MXRecord mx) { _LastActive = DateTime.UtcNow; IsActive = true; await base.ConnectAsync(mx.Host, MtaParameters.Client.SMTP_PORT); _LastActive = DateTime.UtcNow; SmtpStream = new SmtpStreamHandler(this as TcpClient); _MXRecord = mx; // Read the Server greeting. string response = SmtpStream.ReadAllLines(); _LastActive = DateTime.UtcNow; if (!response.StartsWith("2")) { // If the MX is actively denying use service access, SMTP code 421 then we should inform // the ServiceNotAvailableManager manager so it limits our attepts to this MX to 1/minute. if (response.StartsWith("421")) { ServiceNotAvailableManager.Add(SmtpStream.LocalAddress.ToString(), MXRecord.Host, DateTime.UtcNow); } base.Close(); return(false); } IsActive = false; _LastActive = DateTime.UtcNow; return(true); }
/// <summary> /// Attempt to connect to the specified MX server. /// </summary> /// <param name="mx">MX Record of the server to connect to.</param> private async Task <MantaOutboundClientSendResult> ConnectAsync() { TcpClient = CreateTcpClient(); try { await TcpClient.ConnectAsync(_MXRecord.Host, MtaParameters.Client.SMTP_PORT); } catch (Exception) { return(new MantaOutboundClientSendResult(MantaOutboundClientResult.FailedToConnect, "421 Failed to connect", _VirtualMta, _MXRecord)); } SmtpStream = new SmtpStreamHandler(TcpClient); // Read the Server greeting. string response = SmtpStream.ReadAllLines(); // Check we get a valid banner. if (!response.StartsWith("2")) { TcpClient.Close(); // If the MX is actively denying use service access, SMTP code 421 then we should inform // the ServiceNotAvailableManager manager so it limits our attepts to this MX to 1/minute. if (response.StartsWith("421")) { return(new MantaOutboundClientSendResult(MantaOutboundClientResult.ServiceNotAvalible, response, _VirtualMta, _MXRecord)); } return(new MantaOutboundClientSendResult(MantaOutboundClientResult.FailedToConnect, response, _VirtualMta, _MXRecord)); } // We have connected, so say helli return(await ExecHeloAsync()); }