/// <summary> /// Creates a new command result object with the results and response /// </summary> /// <param name="results"></param> /// <param name="response"></param> /// <param name="time">The amount of time it took for this command to execute</param> public CommandResult(ArrayList results, IMAPResponse response, TimeSpan time) { _resultData = results; _resultResponse = response; _elapsedTime = time; }
/// <summary> /// Establish a connection to the IMAP Server. Calls Capability() on success. /// </summary> /// <returns></returns> public IMAPResponse Connect() { IMAPResponse response = IMAPResponse.IMAP_SUCCESS_RESPONSE; _commandCount = 0; try { _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); //_socket.IOControl(IOControlCode.KeepAliveValues, ) SetTcpKeepAlive(_socket, 1000, 1000); _socket.SendBufferSize = int.MaxValue; _socket.ReceiveBufferSize = int.MaxValue; _socket.SendTimeout = int.MaxValue; _socket.ReceiveTimeout = int.MaxValue; _socket.Connect(ServerHost, ServerPort); _nstream = new NetworkStream(_socket, true); //_tcpClient = new TcpClient(ServerHost, ServerPort); if (UseSSL) { //_sslStream = new SslStream(_tcpClient.GetStream(), false, // ValidateServerCertificate, // null); _sslStream = new SslStream(_nstream, false, ValidateServerCertificate, null); try { _sslStream.AuthenticateAsClient(ServerHost); } catch (AuthenticationException e) { Log(LogType.ERROR, e.Message); //_tcpClient.Close(); _socket.Close(); //_isConnected = false; return(IMAPResponse.IMAP_FAILURE_RESPONSE); } _sslReader = new StreamReader(_sslStream); } else { _stdStream = _nstream;//_tcpClient.GetStream(); _stdReader = new StreamReader(_nstream); } string result = ReadLine(); if (result.Contains("OK")) { Log(LogType.IMAP, result); //_isConnected = true; Capability(); } else { Log(LogType.IMAP, result); response = IMAPResponse.IMAP_FAILURE_RESPONSE; } } catch (Exception e) { //Console.WriteLine(e.Message); Log(LogType.ERROR, e.Message); //_isConnected = false; } return(response); }
/// <summary> /// Creates a new command result object with the results and response /// </summary> /// <param name="results"></param> /// <param name="response"></param> /// <param name="time">The amount of time it took for this command to execute</param> public CommandResult(ArrayList results, IMAPResponse response, TimeSpan time) { _resultData = results; _resultResponse = response; _elapsedTime = time; }
/// <summary> /// Executes the specified command on this connection /// </summary> /// <param name="cmd"></param> /// <returns></returns> public CommandResult ExecuteCommand(ICommand cmd) { if (!IsConnected) { return(null); } ArrayList resultArray = new ArrayList(); PrepareCommand(cmd); IMAPResponse response = IMAPResponse.IMAP_SUCCESS_RESPONSE; byte[] data = GetStringBytes(cmd.CommandString); Stopwatch sw = new Stopwatch(); try { bool sendData = false; long bytesReceived = 0; long totalBytes = 0; do { WriteToStream(data, 0, data.Length); sendData = false; while (true) { string result = ReadLine(); if (result == null) { break; } sw.Start(); resultArray.Add(result); if (result.StartsWith(cmd.ResponseOK)) { //Log(LogType.IMAP, result); response = IMAPResponse.IMAP_SUCCESS_RESPONSE; break; } if ((cmd.ResponseGoAhead.Length > 0) && (result.StartsWith(cmd.ResponseGoAhead))) { sendData = true; data = GetStringBytes(cmd.CommandData); resultArray.Clear(); break; } if (result.StartsWith(cmd.ResponseNO) || result.StartsWith(cmd.ResponseBAD)) { //Log(LogType.IMAP, result); response = IMAPResponse.IMAP_FAILURE_RESPONSE; break; } if (resultArray.Count == 1) { totalBytes = DetermineTotalSize(result); } else { //if (cmd is MessagePartCommand) // bytesReceived += GetStringBytes(result, GetCurrentEncoding()).LongLength; //else bytesReceived += GetStringBytes(result).LongLength; } if (bytesReceived > totalBytes) { totalBytes = bytesReceived; } cmd.OnCommandDataReceived(bytesReceived, totalBytes); if (cmd.GetType() != typeof(MessagePartCommand)) { Log(LogType.IMAP, result); } } } while (sendData); sw.Stop(); //if (cmd is MessagePartCommand) // System.Diagnostics.Debugger.Break(); } catch (Exception e) { _logger.Log(LogType.IMAP, e.Message); } //Log(LogType.IMAP, ""); return(new CommandResult(resultArray, response, sw.Elapsed)); }