Esempio n. 1
0
        /// <summary>
        /// Start receiving the email message.
        /// </summary>
        /// <returns>The current state object.</returns>
        private Pop3StateAdapter StartReceive()
        {
            // The manual reset event handler user to
            // block the current thread until the operation
            // completes the task, and create a new state
            // object to hold all state data.
            ManualResetEvent waitObject = null;
            Pop3StateAdapter state      = new Pop3StateAdapter();

            // Assign the current client
            // socket object.
            state.Client    = _client;
            state.SslClient = _sslStream;

            // Get the event to wait on.
            waitObject = state.ReceiveComplete;

            // If a secure connection is required.
            if (_connection.UseSSLConnection)
            {
                // Start receiving data asynchrounusly.
                lock (_sslStream)
                    _sslStream.BeginRead(_buffer, 0, EmailMessageParse.MAX_BUFFER_READ_SIZE,
                                         new AsyncCallback(ReceiveCallback), state);
            }
            else
            {
                // Start receiving data asynchrounusly.
                lock (_client)
                    _client.BeginReceive(_buffer, 0, EmailMessageParse.MAX_BUFFER_READ_SIZE,
                                         SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
            }

            // Block the current thread until all
            // operations are complete.
            waitObject.WaitOne();

            // The operations either completed or threw an exception.
            if (state.OperationException != null)
            {
                throw state.OperationException;
            }

            // Return the state object.
            return(state);
        }
Esempio n. 2
0
        /// <summary>
        /// Start loading the current requested message.
        /// </summary>
        /// <param name="message">The message object that will contain the message data.</param>
        private void LoadEmail(Message message)
        {
            // If only message header data is required.
            if (_headersOnly)
            {
                // Send a command to the server indicating that
                // only header data is required. Send the message
                // position and the number of message lines to return.
                SendCommand("TOP " + _inboxPosition + " " + _headerLineCount);
            }
            else
            {
                // Send a command to return the entire
                // message. Send the message position.
                SendCommand("RETR " + _inboxPosition);
            }

            // Start the receive process
            // at this point, and return the state
            // object that contains the email message
            // built from the string builder.
            Pop3StateAdapter state = StartReceive();

            // Return the complete raw email message only
            if (_rawEmailMessageOnly)
            {
                // Assign the complete email message.
                message.RawEmailMessage = state.EmailMessage.ToString();
            }
            else
            {
                // Parse the email message for header processing
                // of the line within the email message.
                ParseEmail(state.EmailMessage.ToString().Split(new char[] { '\r' }));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Receive call back asynchrounus result.
        /// Handles the receiving of the email message.
        /// </summary>
        /// <param name="ar">The current asynchronus result.</param>
        private void ReceiveCallback(IAsyncResult ar)
        {
            // Get the state adapter from the
            // async result object.
            Pop3StateAdapter state = (Pop3StateAdapter)ar.AsyncState;

            try
            {
                // Get the socket from the
                // state adapter.
                Socket    client    = state.Client;
                SslStream sslStream = state.SslClient;

                // Read data from the remote device.
                int bytesRead = 0;

                // If a secure connection is required.
                if (_connection.UseSSLConnection)
                {
                    lock (sslStream)
                        // End the current asynchrounus
                        // read operation.
                        bytesRead = sslStream.EndRead(ar);
                }
                else
                {
                    lock (client)
                        // End the current asynchrounus
                        // read operation.
                        bytesRead = client.EndReceive(ar);
                }

                if (bytesRead > 0)
                {
                    // Decode the current buffer and add the new data
                    // to the message string builder.
                    state.EmailMessage.Append(Encoding.ASCII.GetString(_buffer, 0, bytesRead));

                    // If the progress call back handler
                    // is not null then send to the client
                    // the number of bytes read.
                    if (_callbackHandler != null)
                    {
                        _callbackHandler((long)bytesRead);
                    }
                }

                // Receive more data if we expect more.
                // note: a literal "." (or more) followed by
                // "\r\n" in an email is prefixed with "." ...
                if (!state.EmailMessage.ToString().EndsWith("\r\n.\r\n"))
                {
                    // If a secure connection is required.
                    if (_connection.UseSSLConnection)
                    {
                        lock (sslStream)
                            sslStream.BeginRead(_buffer, 0, EmailMessageParse.MAX_BUFFER_READ_SIZE,
                                                new AsyncCallback(ReceiveCallback), state);
                    }
                    else
                    {
                        lock (client)
                            client.BeginReceive(_buffer, 0, EmailMessageParse.MAX_BUFFER_READ_SIZE,
                                                SocketFlags.None, new AsyncCallback(ReceiveCallback), state);
                    }
                }
                else
                {
                    // The end of the message has been reached
                    // indicate by the ManualResetEvent thread
                    // handler the that operation is complete
                    // indicated to the ManualResetEvent handler
                    // to stop blocking.
                    state.ReceiveComplete.Set();
                }
            }
            catch (Exception e)
            {
                // An exception has occurred, assign the
                // current operation exception and set
                // the recieved ManualResetEvent thread
                // handler to set indicating that the
                // operation must stop.
                state.OperationException = e;
                state.ReceiveComplete.Set();
            }
        }