/// <summary> /// Scans invalid CR or LF combination in stream. Returns true if contains invalid CR or LF combination. /// </summary> /// <param name="strm">Stream which to check.</param> /// <returns>Returns true if contains invalid CR or LF combination.</returns> public static bool ScanInvalid_CR_or_LF(Stream strm) { StreamHelper lineReader = new StreamHelper(strm, null); byte[] line = lineReader.ReadLine(); while(line != null){ foreach(byte b in line){ // Contains CR or LF. It cannot conatian such sumbols, because CR must be paired with LF // and we currently reading lines with CRLF combination. if(b == 10 || b == 13){ return true; } } line = lineReader.ReadLine(); } return false; }
/// <summary> /// Does period handling. /// </summary> /// <param name="strm">Input stream.</param> /// <param name="add_Remove">If true add periods, else removes periods.</param> /// <param name="setStrmPosTo0">If true sets stream position to 0.</param> /// <returns></returns> public static MemoryStream DoPeriodHandling(Stream strm,bool add_Remove,bool setStrmPosTo0) { MemoryStream replyData = new MemoryStream(); byte[] crlf = new byte[]{(byte)'\r',(byte)'\n'}; if(setStrmPosTo0){ strm.Position = 0; } StreamHelper r = new StreamHelper(strm, null); byte[] line = r.ReadLine(); // Loop through all lines while(line != null){ if(line.Length > 0){ if(line[0] == (byte)'.'){ /* Add period Rfc 2821 4.5.2 - Before sending a line of mail text, the SMTP client checks the first character of the line. If it is a period, one additional period is inserted at the beginning of the line. */ if(add_Remove){ replyData.WriteByte((byte)'.'); replyData.Write(line,0,line.Length); } /* Remove period Rfc 2821 4.5.2 If the first character is a period , the first characteris deleted. */ else{ replyData.Write(line,1,line.Length-1); } break; } else{ replyData.Write(line,0,line.Length); } } replyData.Write(crlf,0,crlf.Length); // Read next line line = r.ReadLine(); } replyData.Position = 0; return replyData; }
/// <summary> /// Connects to specified host. /// </summary> /// <param name="host">Host name.</param> /// <param name="port">Port number.</param> public void Connect(string host, int port, bool sslEnabled) { if (!m_Connected) { TcpClient client = new TcpClient(host, port); if (sslEnabled) { socketStream = new SslStream(client.GetStream(), false, null, null); (socketStream as SslStream).AuthenticateAsClient(host); } else { socketStream = client.GetStream(); } streamHelper = new StreamHelper(socketStream, Encoding.ASCII); m_Connected = true; string reply = streamHelper.ReadToEnd(); if (reply.StartsWith("+OK")) { // Try to read APOP hash key, if supports APOP if (reply.IndexOf("<") > -1 && reply.IndexOf(">") > -1) { m_ApopHashKey = reply.Substring(reply.LastIndexOf("<"), reply.LastIndexOf(">") - reply.LastIndexOf("<") + 1); } } } }