/// <summary> /// POP login /// </summary> /// <param name="host">Host of email server</param> /// <param name="port">Port of server</param> /// <param name="username">Username</param> /// <param name="password">Password</param> /// <param name="connectionSecurity">Connection security/Encryption Type</param> /// <returns>bool value</returns> public bool Login(string host, int port, string username, string password, NGConnectionSecurity connectionSecurity) { try { Logger.Trace("Connecting to " + host + ":" + port + " with username:"******" and secutity:" + connectionSecurity.ToString()); _bSsl = false; _Client = new TcpClient(host, port); _ClientStream = _Client.GetStream(); _StreamReader = new StreamReader(_ClientStream); _StreamWriter = new StreamWriter(_ClientStream); _SslStream = new SslStream(_ClientStream); if (connectionSecurity == NGConnectionSecurity.SSL_TLS) { _SslStream.AuthenticateAsClient(host); _bSsl = true; _SslStreamReader = new StreamReader(_SslStream); _SslStreamWriter = new StreamWriter(_SslStream); } if (!this.CheckResponse(NGPopStatusCode.POP_OK)) { Dispose(); return(false); } if (connectionSecurity == NGConnectionSecurity.STARTTLS) { this.Write(string.Format(NGPopCommands.STARTTLS_CMD, NGPopCommands.EOL)); if (!this.CheckResponse(NGPopStatusCode.POP_OK)) { Dispose(); return(false); } _SslStream.AuthenticateAsClient(host); _bSsl = true; _SslStreamReader = new StreamReader(_SslStream); _SslStreamWriter = new StreamWriter(_SslStream); } this.Write(string.Format(NGPopCommands.LOGIN_USER_CMD, username, NGPopCommands.EOL)); if (!this.CheckResponse(NGPopStatusCode.POP_OK)) { Dispose(); return(false); } this.Write(string.Format(NGPopCommands.LOGIN_PASS_CMD, password, NGPopCommands.EOL)); if (!this.CheckResponse(NGPopStatusCode.POP_OK)) { Dispose(); return(false); } Logger.Trace("Connection to " + host + ":" + port + " successful"); return(true); } catch (Exception ex) { Logger.Error("Error in Login", ex); return(false); } }
/// <summary> /// SMTP login /// </summary> /// <param name="host">Host of email server</param> /// <param name="port">Port of server</param> /// <param name="username">Username</param> /// <param name="password">Password</param> /// <param name="connectionSecurity">Connection security/Encryption Type</param> /// <returns>bool value</returns> public bool Login(string host, int port, string username, string password, NGConnectionSecurity connectionSecurity) { try { Logger.Trace("Connecting to " + host + ":" + port + " with username:"******" and secutity:" + connectionSecurity.ToString()); username = Convert.ToBase64String(Encoding.UTF8.GetBytes(username)); password = Convert.ToBase64String(Encoding.UTF8.GetBytes(password)); _bSsl = false; _Client = new TcpClient(host, port); _ClientStream = _Client.GetStream(); _StreamReader = new StreamReader(_ClientStream); _StreamWriter = new StreamWriter(_ClientStream); _SslStream = new SslStream(_ClientStream); if (connectionSecurity == NGConnectionSecurity.SSL_TLS) { _SslStream.AuthenticateAsClient(host); _bSsl = true; _SslStreamReader = new StreamReader(_SslStream); _SslStreamWriter = new StreamWriter(_SslStream); } var response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.ServiceReady.ToStringValue())) { Dispose(); return(false); } this.Write(string.Format(NGSmtpCommands.HELO_CMD, Dns.GetHostName(), EOF)); response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.Ok.ToStringValue())) { Dispose(); return(false); } if (connectionSecurity == NGConnectionSecurity.STARTTLS) { this.Write(string.Format(NGSmtpCommands.STARTTLS_CMD, EOF)); response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.ServiceReady.ToStringValue())) { Dispose(); return(false); } _SslStream.AuthenticateAsClient(host); _bSsl = true; _SslStreamReader = new StreamReader(_SslStream); _SslStreamWriter = new StreamWriter(_SslStream); this.Write(string.Format(NGSmtpCommands.EHLO_CMD, Dns.GetHostName(), EOF)); response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.Ok.ToStringValue())) { Dispose(); return(false); } } this.Write(string.Format(NGSmtpCommands.AUTH_LOGIN_CMD, EOF)); response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.NextAcceptBase64.ToStringValue())) { Dispose(); return(false); } this.Write(string.Format("{0}{1}", username, EOF)); response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.NextAcceptBase64.ToStringValue())) { Dispose(); return(false); } this.Write(string.Format("{0}{1}", password, EOF)); response = this.Read(); if (!response.StartsWith(NGSmtpStatusCode.Authenticated.ToStringValue())) { Dispose(); return(false); } Logger.Trace("Connection to " + host + ":" + port + " successful"); return(true); } catch (Exception ex) { Logger.Error("Error in Login", ex); return(false); } }