// --------------------------- constructor ----------------------- public Pop3Message( long position, long size, Socket client, SocketExchange InEx) { m_inboxPosition = position; m_messageSize = size; mSocket = client; mSockEx = InEx; // object used to receive the mail message in a background thread. mSockThreadState = new Pop3StateObject( ); mSockThreadState.SockEx = InEx; mSockThreadState.sb = new StringBuilder( ); // load email ... LoadEmail(InEx); // get body (if it exists) ... IEnumerator multipartEnumerator = MultipartEnumerator; while (multipartEnumerator.MoveNext()) { Pop3Component multipart = (Pop3Component) multipartEnumerator.Current; if (multipart.IsBody) { m_body = multipart.Data; break; } } }
// ---------------------- LoadEmail --------------------------- private void LoadEmail(SocketExchange InEx) { // tell the server we want to read the message. InEx.Send("retr " + m_inboxPosition + PopConstants.CrLf); // receive in a background thread. StartReceive(InEx); InEx.LoadResponseMessage(mSockThreadState.sb.ToString( )); // parse email ... string[] lines = InEx.ResponseMessage.Split(Chars.CharArrayChars('\r')); for (int ix = 0; ix < lines.Length; ++ix) { InEx.Logger.AddMessage(NetworkRole.Server, lines[ix]); } ParseEmail(lines); // remove reading pop3State ... mSockThreadState = null; }
// ------------------------- ReceiveCallback ------------------------- // receive MailMessage in background thread. private void ReceiveCallback(IAsyncResult InResult) { try { // Retrieve the state object and the client socket // from the asynchronous state object. Pop3StateObject sockThreadState = (Pop3StateObject)InResult.AsyncState; SocketExchange sockEx = sockThreadState.SockEx; Socket client = sockEx.ConnectedSocket; SecureSocket secureSock = sockEx.ConnectedSecureSocket; bool InlRcv = true; while (true) { // receive from the socket. int ReadCx; if (InlRcv == true) { if (client != null) { ReadCx = client.EndReceive(InResult); } else { ReadCx = secureSock.EndReceive(InResult); } } else { if (client != null) { ReadCx = client.Receive( sockThreadState.buffer, 0, Pop3StateObject.BufferSize, SocketFlags.None); } else { ReadCx = secureSock.Receive( sockThreadState.buffer, 0, Pop3StateObject.BufferSize, SocketFlags.None); } } InlRcv = false; // did not receive anything. probably should leave. if (ReadCx == 0) { break; } // There might be more data, // so store the data received so far. string block = Encoding.ASCII.GetString(sockThreadState.buffer, 0, ReadCx); sockThreadState.sb.Append(block); // is this the end of the message if (Stringer.Tail(sockThreadState.sb, 5) == "\r\n.\r\n") { break; } } } catch (Exception e) { throw new Pop3ReceiveException( "ReceiveCallback error" + e.ToString( )); } finally { m_manualEvent.Set( ); } }