/// <summary> /// Send command to server and retrieve response /// </summary> /// <param name="command">Command to send Imap Server</param> /// <param name="sResultArray">Imap Server response</param> /// <returns>ImapResponseEnum type</returns> protected ImapResponseEnum SendAndReceiveByNumLines(string command, ref ArrayList sResultArray, int nNumLines) { IMAP_COMMAND_VAL++; int nLineCount = 0; string sCommand = IMAP_COMMAND_IDENTIFIER + command; ImapResponseEnum eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE; Log(LogTypeEnum.IMAP, sCommand.TrimEnd(IMAP_COMMAND_EOL.ToCharArray())); byte [] data = System.Text.Encoding.ASCII.GetBytes(sCommand.ToCharArray()); try { m_oNetStrm.Write(data, 0, data.Length); bool bRead = true; while (bRead) { string sResult = m_oRdStrm.ReadLine(); sResultArray.Add(sResult); nLineCount++; if (sResult.StartsWith(IMAP_SERVER_RESPONSE_OK) || nLineCount == nNumLines) { bRead = false; Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE; } else if (sResult.StartsWith(IMAP_SERVER_RESPONSE_NO)) { bRead = false; Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_FAILURE_RESPONSE; } else if (sResult.StartsWith(IMAP_SERVER_RESPONSE_BAD)) { bRead = false; Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_FAILURE_RESPONSE; } else { Log(LogTypeEnum.IMAP, sResult); } } } catch (Exception e) { //LogOut(); throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_SERIOUS, e.Message); } Log(LogTypeEnum.IMAP, ""); return(eImapResponse); }
/// <summary> /// IMAP Capability command /// </summary> public void Capability() { ArrayList sResultArray = new ArrayList(); string capabilityCommand = IMAP_CAPABILITY_COMMAND; capabilityCommand += IMAP_COMMAND_EOL; ImapResponseEnum eImapResponse = SendAndReceive(capabilityCommand, ref sResultArray); if (eImapResponse != ImapResponseEnum.IMAP_SUCCESS_RESPONSE) { throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_CAPABILITY); } }
/// <summary> /// retrieve response /// </summary> /// <param name="command">Command to send Imap Server</param> /// <param name="sResultArray">Imap Server response</param> /// <returns>ImapResponseEnum type</returns> protected ImapResponseEnum Receive(ref ArrayList sResultArray) { ImapResponseEnum eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE; try { bool bRead = true; while (bRead) { string sResult = m_oRdStrm.ReadLine(); sResultArray.Add(sResult); if (sResult.StartsWith(IMAP_SERVER_RESPONSE_OK)) { bRead = false; Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE; } else if (sResult.StartsWith(IMAP_SERVER_RESPONSE_NO)) { bRead = false; Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_FAILURE_RESPONSE; } else if (sResult.StartsWith(IMAP_SERVER_RESPONSE_BAD)) { bRead = false; Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_FAILURE_RESPONSE; } else { Log(LogTypeEnum.IMAP, sResult); } } } catch (Exception e) { //LogOut(); throw new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_SERIOUS, e.Message); } Log(LogTypeEnum.IMAP, ""); return(eImapResponse); }
/// <summary> /// Connect to specified host and port /// </summary> /// <param name="sHost">Imap host</param> /// <param name="nPort">Imap port</param> /// <param name="sslEnabled"> </param> /// <returns>ImapResponseEnum type</returns> protected ImapResponseEnum Connect(string sHost, ushort nPort, bool sslEnabled = false) { IMAP_COMMAND_VAL = 0; ImapResponseEnum eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE; ImapException e_connect = new ImapException(ImapException.ImapErrorEnum.IMAP_ERR_CONNECT, sHost); try { m_oImapServ = new TcpClient(sHost, nPort); if (sslEnabled) { var sslStrm = new SslStream(m_oImapServ.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); //m_SSLStream.ReadTimeout = 30000; try { sslStrm.AuthenticateAsClient(sHost); } catch (AuthenticationException e) { Console.WriteLine("Exception: {0}", e.Message); if (e.InnerException != null) { Console.WriteLine("Inner exception: {0}", e.InnerException.Message); } Console.WriteLine("Authentication failed - closing the connection."); m_oImapServ.Close(); return(ImapResponseEnum.IMAP_FAILURE_RESPONSE); } m_oNetStrm = (Stream)sslStrm; m_oRdStrm = new StreamReader(sslStrm); } else { m_oNetStrm = m_oImapServ.GetStream(); m_oRdStrm = new StreamReader(m_oImapServ.GetStream()); } string sResult = m_oRdStrm.ReadLine(); if ((sResult.StartsWith(IMAP_OK_SERVER_RESPONSE) == true) || (sResult.StartsWith(IMAP_CAPABILITY_SERVER_RESPONSE) == true)) { Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_SUCCESS_RESPONSE; Capability(); } else { Log(LogTypeEnum.IMAP, sResult); eImapResponse = ImapResponseEnum.IMAP_FAILURE_RESPONSE; } } catch { throw e_connect; } m_sHost = sHost; m_nPort = nPort; m_bSSLEnabled = sslEnabled; return(eImapResponse); }