/// <summary> /// Get the smtp server information from /// the configuration file, the host and /// port of the smtp server is located in /// the default section, this data is used /// to connect to the smtp server. /// </summary> private void GetSmtpServerHost() { try { // Create a new connection adapter. _connection = new SmtpConnectionAdapter(); // If no host name set then // use the defualt values. if (String.IsNullOrEmpty(_smtpServerName)) { // Create a new default host type // an load the values from the configuration // file into the default host type. ProxySmtpServerDefaultHost defaultHost = (ProxySmtpServerDefaultHost)System.Configuration.ConfigurationManager.GetSection( "ProxySmtpServerGroup/ProxySmtpServerDefaultHost"); // If the port is greater than zero then // assign the port number. if (defaultHost.HostSection.PortAttribute > 0) { _connection.Port = defaultHost.HostSection.PortAttribute; } // Get the smtp server. _connection.Server = defaultHost.HostSection.HostAttribute; } else { // Create a new host type // an load the values from the configuration // file into the host type. ProxySmtpServerHosts hosts = (ProxySmtpServerHosts)System.Configuration.ConfigurationManager.GetSection( "ProxySmtpServerGroup/ProxySmtpServerHosts"); // If the port is greater than zero then // assign the port number. if (hosts.HostSection[_smtpServerName].PortAttribute > 0) { _connection.Port = hosts.HostSection[_smtpServerName].PortAttribute; } // Get the smtp server. _connection.Server = hosts.HostSection[_smtpServerName].HostAttribute; } } catch (Exception e) { // Detect a thread abort exception. if (e is ThreadAbortException) { Thread.ResetAbort(); } base.Write("SmtpTlsProxyServer", "GetListeningPort", e.Message, 246, WriteTo.EventLog, LogType.Error); } }
/// <summary> /// Default constructor. /// </summary> /// <param name="tcpClient">The tcp client channel connection from server to client.</param> /// <param name="connection">The connection adapter used to connect to the server.</param> public SmtpTlsProxyConnection(TcpClient tcpClient, SmtpConnectionAdapter connection) { try { // The tcp client to server channel. // Assign the network stream from the client // channel stream. _tcpClient = tcpClient; _connection = connection; // Create a new state object. ServerSocketState state = new ServerSocketState(); state.TcpClient = _tcpClient; // This starts the asynchronous read thread. // The data will be saved into readBuffer. _tcpClient.Client.BeginReceive(_readBuffer, 0, READ_BUFFER_SIZE, SocketFlags.None, new AsyncCallback(InitializingSSLDataReceiver), state); // Get the machine name. string machineName = Environment.MachineName; // Send a welcome message to the client. SendNonSSLClientCommand("220 " + machineName + " ESMTP Secure Smtp Proxy Server - Authorized Accounts Only"); } catch (Exception e) { // Detect a thread abort exception. if (e is ThreadAbortException) { Thread.ResetAbort(); } base.Write("SmtpTlsProxyConnection", "Constructor", e.Message, 65, WriteTo.EventLog, LogType.Error); } }
/// <summary> /// Default constructor. /// </summary> /// <param name="tcpClient">The tcp client channel connection from server to client.</param> /// <param name="connection">The connection adapter used to connect to the server.</param> public SmtpSslProxyConnection(TcpClient tcpClient, SmtpConnectionAdapter connection) { try { // The tcp client to server channel. // Assign the network stream from the client // channel stream. _tcpClient = tcpClient; _connection = connection; _sslStream = new SslStream(tcpClient.GetStream()); // Certificate file containing a public key // and private key for secure data transfer. X509Certificate2 certificate = GetServerCredentials(); // Load the certificate into the // secure stream used for secure communication. _sslStream.AuthenticateAsServer(certificate, false, SslProtocols.Ssl3, false); // Create a new state object. ServerSocketState state = new ServerSocketState(); state.SslStream = _sslStream; // This starts the asynchronous read thread. // The data will be saved into readBuffer. _sslStream.BeginRead(_readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DataReceiver), state); // Create a new smtp client and // attempt to make a connection // get the response from the server. bool ret = GetSocket(); // If return false. if (!ret) { // Write an error response back to // the smtp client. _sslStream.Write(Encoding.ASCII.GetBytes("-ERR" + "\r\n")); } else { // Create a new state object. ServerSocketState stateSocket = new ServerSocketState(); stateSocket.EmailMessage = new StringBuilder(); stateSocket.SslStream = _sslStream; stateSocket.Socket = _socket; // Start receiving data asynchrounusly. _socket.BeginReceive(_receiveBuffer, 0, READ_BUFFER_SIZE, SocketFlags.None, new AsyncCallback(ReceiveCallback), stateSocket); } } catch (Exception e) { // Detect a thread abort exception. if (e is ThreadAbortException) { Thread.ResetAbort(); } base.Write("SmtpSslProxyConnection", "Constructor", e.Message, 65, WriteTo.EventLog, LogType.Error); } }