/// <summary> /// Checks the settings. /// </summary> /// <param name="smtpServerHost">The SMTP server host.</param> /// <param name="port">The port.</param> /// <param name="secureConnection">The secure connection.</param> /// <param name="authenticate">if set to <c>true</c> [authenticate].</param> /// <param name="user">The user.</param> /// <param name="password">The password.</param> /// <returns></returns> public static SmtpSettingsResult CheckSettings(string smtpServerHost, int port, SecureConnectionType secureConnection, bool authenticate, string user, string password) { // OZ Remove White Space [2009-04-28] smtpServerHost = smtpServerHost.Trim(); user = user.Trim(); // try { SmtpClient client = new SmtpClient(smtpServerHost, port); client.SecureConnectionType = secureConnection; client.Authenticate = authenticate; client.User = user; client.Password = password; client.Send(); } catch (Exception ex) { Trace.WriteLine(ex, "SmtpBox.CheckSettings"); Mediachase.Ibn.Log.WriteError("Check Smtp Settings Error: " + ex.ToString()); return(SmtpSettingsResult.None); } return(SmtpSettingsResult.AllOk); //return SmtpClientUtility.CheckSettings(smtpServerHost, port, //secureConnection, //authenticate, //user, password); }
public SecureConnection(SecureConnectionVersion version, SecureConnectionType type, Connection connection, DigitalSignature digitalSignature, BufferManager bufferManager) { _type = type; _connection = connection; _digitalSignature = digitalSignature; _bufferManager = bufferManager; _myVersion = version; }
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 void Save() { int port = int.Parse(Port, CultureInfo.InvariantCulture); SecureConnectionType secureConnectionType = (SecureConnectionType)Enum.Parse(typeof(SecureConnectionType), SecureConnection); bool authenticate = bool.Parse(Authenticate); DBHelper2.DBHelper.RunTextScalar("INSERT INTO [SmtpBox] ([Name],[Server],[Port],[SecureConnection],[Authenticate],[User],[Password],[IsDefault]) VALUES (@Name,@Server,@Port,@SecureConnection,@Authenticate,@User,@Password,1)" , DBHelper.MP("@Name", SqlDbType.NVarChar, 255, Server) , DBHelper.MP("@Server", SqlDbType.NVarChar, 255, Server) , DBHelper.MP("@Port", SqlDbType.Int, port) , DBHelper.MP("@SecureConnection", SqlDbType.Int, (int)secureConnectionType) , DBHelper.MP("@Authenticate", SqlDbType.Bit, authenticate) , DBHelper.MP("@User", SqlDbType.NVarChar, 255, User) , DBHelper.MP("@Password", SqlDbType.NVarChar, 255, Password) ); }
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); }
/// <summary> /// Checks the settings. /// </summary> /// <param name="smtpServerHost">The SMTP server host.</param> /// <param name="port">The port.</param> /// <param name="secureConnection">The secure connection.</param> /// <param name="authenticate">if set to <c>true</c> [authenticate].</param> /// <param name="user">The user.</param> /// <param name="password">The password.</param> /// <returns></returns> public static SmtpSettingsResult CheckSettings(string smtpServerHost, int port, SecureConnectionType secureConnection, bool authenticate, string user, string password) { // OZ Remove White Space [2009-04-28] smtpServerHost = smtpServerHost.Trim(); user = user.Trim(); // try { SmtpClient client = new SmtpClient(smtpServerHost, port); client.SecureConnectionType = secureConnection; client.Authenticate = authenticate; client.User = user; client.Password = password; client.Send(); } catch (Exception ex) { Trace.WriteLine(ex, "SmtpBox.CheckSettings"); Mediachase.Ibn.Log.WriteError("Check Smtp Settings Error: " + ex.ToString()); return SmtpSettingsResult.None; } return SmtpSettingsResult.AllOk; //return SmtpClientUtility.CheckSettings(smtpServerHost, port, //secureConnection, //authenticate, //user, password); }
public LdapServerBuilder UseSecureConnection(SecureConnectionType value, bool trustAllSslCertificates = false) { ldapServer.SecureConnectionType = value; ldapServer.TrustAllSslCertificates = trustAllSslCertificates; return(this); }