Esempio n. 1
0
        /// <summary>
        /// Authenticates using the CRAM-MD5 authentication method
        /// </summary>
        /// <param name="username">The username</param>
        /// <param name="password">The user password</param>
        /// <exception cref="NotSupportedException">Thrown when the server does not support AUTH CRAM-MD5</exception>
        /// <exception cref="InvalidLoginException">If the user credentials was not accepted</exception>
        /// <exception cref="PopServerLockedException">If the server said the the mailbox was locked</exception>
        /// <exception cref="LoginDelayException">If the server rejects the login because of too recent logins</exception>
        private void AuthenticateUsingCramMd5(string username, string password)
        {
            // Example of communication:
            // C: AUTH CRAM-MD5
            // S: + PDE4OTYuNjk3MTcwOTUyQHBvc3RvZmZpY2UucmVzdG9uLm1jaS5uZXQ+
            // C: dGltIGI5MTNhNjAyYzdlZGE3YTQ5NWI0ZTZlNzMzNGQzODkw
            // S: +OK CRAM authentication successful

            // Other example, where AUTH CRAM-MD5 is not supported
            // C: AUTH CRAM-MD5
            // S: -ERR Authentication method CRAM-MD5 not supported

            try
            {
                SendCommand("AUTH CRAM-MD5");
            }
            catch (PopServerException e)
            {
                // A PopServerException will be thrown if the server responds with a -ERR not supported
                throw new NotSupportedException("CRAM-MD5 authentication not supported", e);
            }

            // Fetch out the challenge from the server response
            string challenge = LastServerResponse.Substring(2);

            // Compute the challenge response
            string response = CramMd5.ComputeDigest(username, password, challenge);

            // Send the response to the server
            SendCommand(response);

            // Authentication was successful if no exceptions thrown before getting here
        }
Esempio n. 2
0
        /// <summary>
        /// Get a unique ID for a single message.<br/>
        /// </summary>
        /// <param name="messageNumber">
        /// Message number, which may not be marked as deleted.<br/>
        /// The <paramref name="messageNumber"/> must be inside the range [1, messageCount]
        /// </param>
        /// <returns>The unique ID for the message</returns>
        /// <exception cref="PopServerException">If the server did not accept the UIDL command. This could happen if the <paramref name="messageNumber"/> does not exist</exception>
        public string GetMessageUid(int messageNumber)
        {
            AssertDisposed();

            ValidateMessageNumber(messageNumber);

            if (State != ConnectionState.Transaction)
            {
                throw new InvalidUseException("Cannot get message ID, when the user has not been authenticated yet");
            }

            // Example from RFC:
            //C: UIDL 2
            //S: +OK 2 QhdPYR:00WBw1Ph7x7

            SendCommand("UIDL " + messageNumber);

            // Parse out the unique ID
            return(LastServerResponse.Split(' ')[2]);
        }
Esempio n. 3
0
        /// <summary>
        /// Sends a command to the POP server, expects an integer reply in the response
        /// </summary>
        /// <param name="command">command to send to server</param>
        /// <param name="location">
        /// The location of the int to return.<br/>
        /// Example:<br/>
        /// <c>S: +OK 2 200</c><br/>
        /// Set <paramref name="location"/>=1 to get 2<br/>
        /// Set <paramref name="location"/>=2 to get 200<br/>
        /// </param>
        /// <returns>Integer value in the reply</returns>
        /// <exception cref="PopServerException">If the server did not accept the command</exception>
        private int SendCommandIntResponse(string command, int location)
        {
            SendCommand(command);

            return(int.Parse(LastServerResponse.Split(' ')[location], CultureInfo.InvariantCulture));
        }