Example #1
0
        /// <summary>
        /// Method that takes a full message and extract the headers from it.
        /// </summary>
        /// <param name="messageContent">The message to extract headers from. Does not need the body part. Needs the empty headers end line.</param>
        /// <returns>A collection of Name and Value pairs of headers</returns>
        /// <exception cref="T:System.ArgumentNullException">If <paramref name="messageContent" /> is <see langword="null" /></exception>
        private static NameValueCollection ExtractHeaders(string messageContent)
        {
            if (messageContent == null)
            {
                throw new ArgumentNullException("messageContent");
            }
            NameValueCollection headers = new NameValueCollection();

            using (StringReader messageReader = new StringReader(messageContent))
            {
                string line;
                while (!string.IsNullOrEmpty(line = messageReader.ReadLine()))
                {
                    KeyValuePair <string, string> header = HeaderExtractor.SeparateHeaderNameAndValue(line);
                    string        headerName             = header.Key;
                    StringBuilder headerValue            = new StringBuilder(header.Value);
                    while (HeaderExtractor.IsMoreLinesInHeaderValue(messageReader))
                    {
                        string moreHeaderValue = messageReader.ReadLine();
                        if (moreHeaderValue == null)
                        {
                            throw new ArgumentException("This will never happen");
                        }
                        headerValue.Append(moreHeaderValue);
                    }
                    headers.Add(headerName, headerValue.ToString());
                }
            }
            return(headers);
        }
Example #2
0
        private static MessagePart GetMessagePart(byte[] rawMessageContent)
        {
            MessageHeader headers;

            byte[] body;
            HeaderExtractor.ExtractHeadersAndBody(rawMessageContent, out headers, out body);
            return(new MessagePart(body, headers));
        }
Example #3
0
        public PopMessage(byte[] rawMessageContent, bool getBody)
        {
            this.RawMessage = rawMessageContent;
            MessageHeader headersTemp;

            byte[] body;
            HeaderExtractor.ExtractHeadersAndBody(rawMessageContent, out headersTemp, out body);
            this.Headers = headersTemp;
            if (getBody)
            {
                this.MessagePart = new MessagePart(body, this.Headers);
            }
        }
Example #4
0
        /// <summary>
        /// Extract the header part and body part of a message.<br />
        /// The headers are then parsed to a strongly typed <see cref="T:OpenPop.Mime.Header.MessageHeader" /> object.
        /// </summary>
        /// <param name="fullRawMessage">The full message in bytes where header and body needs to be extracted from</param>
        /// <param name="headers">The extracted header parts of the message</param>
        /// <param name="body">The body part of the message</param>
        /// <exception cref="T:System.ArgumentNullException">If <paramref name="fullRawMessage" /> is <see langword="null" /></exception>
        public static void ExtractHeadersAndBody(byte[] fullRawMessage, out MessageHeader headers, out byte[] body)
        {
            if (fullRawMessage == null)
            {
                throw new ArgumentNullException("fullRawMessage");
            }
            int    endOfHeaderLocation = HeaderExtractor.FindHeaderEndPosition(fullRawMessage);
            string headersString       = Encoding.ASCII.GetString(fullRawMessage, 0, endOfHeaderLocation);
            NameValueCollection headersUnparsedCollection = HeaderExtractor.ExtractHeaders(headersString);

            headers = new MessageHeader(headersUnparsedCollection);
            body    = new byte[fullRawMessage.Length - endOfHeaderLocation];
            Array.Copy(fullRawMessage, endOfHeaderLocation, body, 0, body.Length);
        }