Example #1
0
        public bool ContainsStatus(SmtpCode status)
        {
            if (this.Values.Count == 0)
            {
                return(false);
            }

            return(this.Values.Any(kvp => kvp.Key == status));
        }
Example #2
0
        /// <summary>
        /// Determines whether the SMTP response contains the specified status code.
        /// </summary>
        /// <param name="status">The SMTP code to check.</param>
        /// <returns>True if the response contains the code. False otherwise.</returns>
        public bool Contains(SmtpCode status)
        {
            if (Values.Count == 0)
            {
                return false;
            }

            return Values.Any(kvp => kvp.Key == status);
        }
Example #3
0
        private async Task <SmtpResponse> GetResponse(string from)
        {
            SmtpResponse  response       = new SmtpResponse();
            bool          isStartingLine = true;
            StringBuilder stringBuilder  = null;
            int           charLen        = 3;
            Boolean       endOfStream    = false;
            SmtpCode      code           = SmtpCode.None;
            string        codeStr        = string.Empty;

            try
            {
                stringBuilder = new StringBuilder();

                while (!endOfStream)
                {
                    // There is a Strange behavior when the bufferLength is exactly the same size as the inputStream
                    await reader.LoadAsync(bufferLength);

                    charLen = Math.Min((int)reader.UnconsumedBufferLength, bufferLength);

                    if (charLen == 0)
                    {
                        endOfStream = true;
                        break;
                    }

                    // If charLen < bufferLength, it's end of stream
                    if (charLen < bufferLength)
                    {
                        endOfStream = true;
                    }

                    // get the current position
                    int charPos = 0;

                    // Read the buffer
                    byte[] buffer = new byte[charLen];
                    reader.ReadBytes(buffer);

                    do
                    {
                        // get the character
                        char chr = (char)buffer[charPos];

                        // if it's starting point, we can read the first 3 chars.
                        if (isStartingLine)
                        {
                            codeStr += chr;

                            // Get the code
                            if (codeStr.Length == 3)
                            {
                                int codeInt;
                                if (int.TryParse(codeStr, out codeInt))
                                {
                                    code = (SmtpCode)codeInt;
                                }

                                // next
                                isStartingLine = false;
                            }
                        }
                        else if (chr == '\r' || chr == '\n')
                        {
                            // Advance 1 byte to get the '\n' if not at the end of the buffer
                            if (chr == '\r' && charPos < (charLen - 1))
                            {
                                charPos++;
                                chr = (char)buffer[charPos];
                            }
                            if (chr == '\n')
                            {
                                KeyValuePair <SmtpCode, String> r = new KeyValuePair <SmtpCode, string>
                                                                        (code, stringBuilder.ToString());

                                response.Values.Add(r);

                                _logHelper.Log(LogLevel.Trace, $"{((int)code).ToString()} {stringBuilder.ToString()}");

                                stringBuilder  = new StringBuilder();
                                code           = SmtpCode.None;
                                codeStr        = string.Empty;
                                isStartingLine = true;
                            }
                        }
                        else
                        {
                            stringBuilder.Append(chr);
                        }

                        charPos++;
                    } while (charPos < charLen);
                }
            }
            catch (Exception ex)
            {
                _logHelper.Log(LogLevel.Error, ex.Message);
                Debug.WriteLine(ex.Message);
                throw;
            }

            return(response);
        }