private void btCheckSettings_Click(object sender, EventArgs e) { Page.Validate(); if (!Page.IsValid) { return; } SecureConnectionType type = SecureConnectionType.None; switch (rbSecurity.SelectedValue) { case "0": type = SecureConnectionType.None; break; case "1": type = SecureConnectionType.Ssl; break; case "2": type = SecureConnectionType.Tls; break; default: break; } string password = txtPassword.Text.Trim(); if (String.IsNullOrEmpty(password) && ViewState["Password"] != null) { password = ViewState["Password"].ToString(); } SmtpSettingsResult result = SmtpBox.CheckSettings(txtServer.Text, int.Parse(txtPort.Text), type, cbAuthenticate.Checked, txtUser.Text, password); lbSettingsValid.Visible = true; if (result == SmtpSettingsResult.AllOk) { lbSettingsValid.ForeColor = Color.Blue; lbSettingsValid.Text = LocRM.GetString("tOK"); } else { lbSettingsValid.ForeColor = Color.Red; lbSettingsValid.Text = LocRM.GetString("tError"); } }
internal static SmtpSettingsResult CheckSettings(string SmtpServerHost, int Port, SecureConnectionType SecureConnection, bool Authenticate, string User, string Password) { SmtpSettingsResult result = SmtpSettingsResult.None; if (SmtpServerHost == string.Empty) { SmtpServerHost = "localhost"; } try { // Progressive Method //byte[] inBuffer = new byte[10 * 1024]; //int realReceived = -1; string ResponseString = string.Empty; // Step 1. Open Socket Connection using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { #region Init Socket //IPHostEntry entry = Dns.GetHostEntry(SmtpServerHost); IPAddress address = GetHostAddressIPv4(SmtpServerHost); result |= SmtpSettingsResult.ServerName; socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 30000); socket.Connect(new IPEndPoint(address, Port)); #endregion Stream smptStream = new NetworkStream(socket); if (SecureConnection == SecureConnectionType.Ssl) { SslStream sslStream = new SslStream(smptStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); sslStream.AuthenticateAsClient(SmtpServerHost); smptStream = sslStream; } #region Welcome // Step 2. Read Welcome //realReceivet = socket.Receive(inByffer); //realReceived = StreamRead(smptStream,inByffer); //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 220: // Success break; default: throw new SmtpClientException(ResponseString); } #endregion #region HELO // Step 3. Send EHLO command IPAddress[] ipList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; if (ipList.Length > 0) { //socket.Send(System.Text.Encoding.Default.GetBytes(string.Format("EHLO [{0}]\r\n", ipList[0].ToString()))); StreamWrite(smptStream, "EHLO [{0}]\r\n", ipList[0].ToString()); } else { //socket.Send(System.Text.Encoding.Default.GetBytes("EHLO\r\n")); StreamWrite(smptStream, "EHLO\r\n"); } // Step 4. Read Response //realReceivet = socket.Receive(inByffer); //realReceived = StreamRead(smptStream, inByffer); //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 250: // Success break; default: throw new SmtpClientException(ResponseString); } #endregion if (SecureConnection == SecureConnectionType.Tls) { #region STARTTLS // Step. Send STARTTLS command StreamWrite(smptStream, "STARTTLS\r\n"); //realReceived = StreamRead(smptStream, inByffer); // Step. Read Response //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 220: // Success break; default: throw new SmtpClientException(ResponseString); } SslStream tlsSmtpStream = new SslStream(smptStream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); tlsSmtpStream.AuthenticateAsClient(SmtpServerHost); smptStream = tlsSmtpStream; #endregion } result |= SmtpSettingsResult.Connection; if (Authenticate) { #region AUTH LOGIN //socket.Send(System.Text.Encoding.Default.GetBytes("AUTH LOGIN\r\n")); StreamWrite(smptStream, "AUTH LOGIN\r\n"); // Step 4. Read Response //realReceivet = socket.Receive(inByffer); //realReceived = StreamRead(smptStream, inByffer); //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 334: // Success break; default: throw new SmtpClientException(ResponseString); } // Send User Name string UserNameB64 = Convert.ToBase64String(Encoding.Default.GetBytes(User)) + "\r\n"; //socket.Send(System.Text.Encoding.Default.GetBytes(UserNameB64)); StreamWrite(smptStream, UserNameB64); // Step 4. Read Response //realReceivet = socket.Receive(inByffer); //realReceived = StreamRead(smptStream, inByffer); //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 334: // Success break; default: throw new SmtpClientException(ResponseString); } // Send Password string PassB64 = Convert.ToBase64String(Encoding.Default.GetBytes(Password)) + "\r\n"; //socket.Send(System.Text.Encoding.Default.GetBytes(PassB64)); StreamWrite(smptStream, PassB64); // Step 4. Read Response //realReceivet = socket.Receive(inByffer); //realReceived = StreamRead(smptStream, inByffer); //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 235: // Success break; default: throw new SmtpClientException(ResponseString); } #endregion } result |= SmtpSettingsResult.Authentication; #region QUIT // Step 14. Send QUIT command //socket.Send(System.Text.Encoding.Default.GetBytes("QUIT\r\n")); StreamWrite(smptStream, "QUIT\r\n"); // Step 15. Close Connection //realReceivet = socket.Receive(inByffer); //realReceived = StreamRead(smptStream, inByffer); //ResponseString = Encoding.Default.GetString(inByffer, 0, realReceived); ResponseString = StreamReadString(smptStream); System.Diagnostics.Trace.WriteLine(ResponseString); switch (GetSmtpReplyCommand(ResponseString)) { case 221: // Success break; default: throw new SmtpClientException(ResponseString); } #endregion socket.Close(); } } catch (Exception ex) { System.Diagnostics.Trace.WriteLine(ex, "SmtpClient.CheckSetting"); } return(result); }