private void Parse(byte[] buffer)
        {
            int headerIndex = Util.IndexOfBuffer(buffer, Util.CrlfBuffer);

            if (headerIndex > -1)
            {
                byte[] headerBuffer = new byte[headerIndex];
                System.Array.Copy(buffer, 0, headerBuffer, 0, headerBuffer.Length);

                ParseHeader(headerBuffer);
            }

            /////////////// This part of code is same in Message and BodyPart classes

            if (buffer.Length > headerIndex + 4)
            {
                byte[] bodyBuffer = new byte[buffer.Length - headerIndex - 4];
                System.Array.Copy(buffer, headerIndex + 4, bodyBuffer, 0, bodyBuffer.Length);

                ContentType             contentType             = this.ContentType;
                ContentDisposition      contentDisposition      = this.ContentDisposition;
                ContentTransferEncoding contentTransferEncoding = this.ContentTransferEncoding;

                if (contentType == null)
                {
                    body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);
                }
                else if ((contentType.Type != null && contentType.Type.ToLower() == "text") ||
                         (contentType.Type != null && contentType.Type.ToLower() == "text" && contentDisposition != null && contentDisposition.Type == ContentDispositionType.Inline))
                {
                    Parameter charsetParameter = contentType.Parameters["charset"];

                    if (charsetParameter != null && charsetParameter.Value != null)
                    {
                        Encoding encoding = Util.GetEncoding(charsetParameter.Value);
                        body = encoding.GetString(bodyBuffer, 0, bodyBuffer.Length);

                        if (contentTransferEncoding == ContentTransferEncoding.QuotedPrintable)
                        {
                            body = Util.DecodeQuotedPrintable(body, encoding);
                        }
                        else if (contentTransferEncoding == ContentTransferEncoding.Base64)
                        {
                            body = Util.DecodeBase64(body, encoding);
                        }
                        else
                        {
                            body = encoding.GetString(bodyBuffer, 0, bodyBuffer.Length);
                        }
                    }
                    else
                    {
                        body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);

                        if (contentTransferEncoding == ContentTransferEncoding.QuotedPrintable)
                        {
                            body = Util.DecodeQuotedPrintable(body, System.Text.Encoding.Default);
                        }
                        else if (contentTransferEncoding == ContentTransferEncoding.Base64)
                        {
                            body = Util.DecodeBase64(body, System.Text.Encoding.Default);
                        }
                    }
                }
                else if (contentType.Type != null && contentType.Type.ToLower() == "multipart")
                {
                    Parameter boundaryParameter = contentType.Parameters["boundary"];

                    if (boundaryParameter != null && boundaryParameter.Value != null)
                    {
                        string boundary = boundaryParameter.Value;

                        IList <byte[]> bodyPartsBufferList = Util.GetBodyPartsBuffers(bodyBuffer, boundary);

                        for (int i = 0; i < bodyPartsBufferList.Count; i++)
                        {
                            byte[] bodyPartBuffer = bodyPartsBufferList[i];

                            BodyPart bodyPart = new BodyPart(bodyPartBuffer);
                            bodyParts.Add(bodyPart);
                        }
                    }
                }
                else if (contentType.Type != null && contentType.Type.ToLower() == "message")
                {
                    if (contentType.SubType != null && contentType.SubType.ToLower() == "rfc822")
                    {
                        embeddedMessage = new Message(bodyBuffer);
                    }
                    else if (contentType.SubType != null && contentType.SubType.ToLower() == "delivery-status")
                    {
                        byte[] encodedBodyBuffer = new byte[0];

                        if (contentTransferEncoding == ContentTransferEncoding.Base64)
                        {
                            String base64String = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);

                            if (base64String != null && base64String.Length > 0)
                            {
                                encodedBodyBuffer = Convert.FromBase64String(base64String);
                            }
                        }

                        if (encodedBodyBuffer.Length > 0)
                        {
                            try
                            {
                                embeddedMessage         = new Message(encodedBodyBuffer);
                                embeddedMessage.Subject = contentType.Parameters["name"] != null ? contentType.Parameters["name"].Value : "ATT";
                            }
                            catch (MessageFormatException)
                            {
                                body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);
                            }
                        }
                    }
                    else if (contentType.SubType != null && contentType.SubType == "partial")
                    {
                        body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);
                    }
                }
                else //contentType = image, application, video, audio, ...
                {
                    if (contentTransferEncoding == ContentTransferEncoding.QuotedPrintable)
                    {
                        body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);
                        body = body.Replace("=09", "\t");
                        body = body.Replace("=0D", "\r");
                        body = body.Replace("=0A", "\n");
                        body = body.Replace("=20", " ");
                        body = body.Replace("=\r\n", String.Empty);
                    }
                    else if (contentTransferEncoding == ContentTransferEncoding.Base64)
                    {
                        bodyBuffer = Util.RemoveCRLF(bodyBuffer);

                        body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);
                    }
                    else
                    {
                        body = System.Text.Encoding.Default.GetString(bodyBuffer, 0, bodyBuffer.Length);
                    }
                }
            }

            ///////////////////////////////////////////////////////////////////////////////////////
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BodyPart"/> class.
        /// </summary>
        /// <param name="attachment">The attachment.</param>
        public BodyPart(Attachment attachment)
        {
            if (attachment != null)
            {
                if (attachment.ContentType != null)
                {
                    this.ContentType = attachment.ContentType;
                }

                this.ContentID               = attachment.ContentID;
                this.ContentLocation         = attachment.ContentLocation;
                this.ContentTransferEncoding = ContentTransferEncoding.Base64;

                if (attachment.ContentDisposition != null)
                {
                    ContentDisposition contentDisposition = attachment.ContentDisposition;

                    if (attachment.ContentID != null)
                    {
                        contentDisposition = new ContentDisposition(ContentDispositionType.Inline);
                    }

                    if (attachment.Name != null)
                    {
                        Parameter nameParameter = new Parameter("filename", "\"" + attachment.Name + "\"");
                        contentDisposition.Parameters.Add(nameParameter);
                    }

                    this.ContentDisposition = contentDisposition;
                }
                else
                {
                    ContentDisposition contentDisposition = new ContentDisposition(ContentDispositionType.Attachment);

                    if (attachment.ContentID != null)
                    {
                        contentDisposition = new ContentDisposition(ContentDispositionType.Inline);
                    }

                    if (attachment.Name != null)
                    {
                        Parameter nameParameter = new Parameter("filename", "\"" + attachment.Name + "\"");
                        contentDisposition.Parameters.Add(nameParameter);
                    }

                    this.ContentDisposition = contentDisposition;
                }

                byte[] attachmentBuffer = attachment.GetBytes();

                if (attachmentBuffer != null && this.ContentType != null && this.ContentType.Type == "text")
                {
                    System.Text.Encoding encoding = Util.GetEncoding(this.ContentType.CharSet);

                    body = encoding.GetString(attachmentBuffer, 0, attachmentBuffer.Length);
                }
                else if (attachmentBuffer != null)
                {
                    body = Convert.ToBase64String(attachmentBuffer, 0, attachmentBuffer.Length);
                    body = Util.SplitBase64Body(body);
                }
            }
        }
        internal Attachment[] GetAttachments(bool includeEmbedded)
        {
            IList <Attachment> attachmentList = new List <Attachment>();

            if (embeddedMessage != null)
            {
                string attachmentName = null;
                string charset        = null;

                if (embeddedMessage.ContentType != null)
                {
                    Parameter nameParameter = embeddedMessage.ContentType.Parameters["name"];

                    if (nameParameter != null && nameParameter.Value != null)
                    {
                        attachmentName = nameParameter.Value;
                    }

                    Parameter charsetParameter = embeddedMessage.ContentType.Parameters["charset"];

                    if (charsetParameter != null)
                    {
                        charset = charsetParameter.Value;
                    }
                }

                if (attachmentName == null && embeddedMessage.ContentDisposition != null)
                {
                    Parameter filenameParameter = embeddedMessage.ContentDisposition.Parameters["filename"];

                    if (filenameParameter != null && filenameParameter.Value != null)
                    {
                        attachmentName = filenameParameter.Value;
                    }
                }

                if (attachmentName == null)
                {
                    attachmentName = embeddedMessage.Subject + ".eml";
                }

                Encoding encoding = Util.GetEncoding(charset);

                byte[] attachmentBuffer = encoding.GetBytes(embeddedMessage.ToString());

                Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                attachment.ContentID          = this.ContentID;
                attachment.ContentLocation    = this.ContentLocation;
                attachment.ContentDisposition = this.ContentDisposition;
                attachment.ContentDescription = this.ContentDescription;
                attachmentList.Add(attachment);
            }
            else if (ContentType != null && ContentType.Type != null && ContentType.Type.ToLower() == "message" && ContentType.SubType != null && ContentType.SubType.ToLower() == "rfc822")
            {
                string attachmentName = null;
                string charset        = null;

                if (this.ContentType != null)
                {
                    Parameter nameParameter = this.ContentType.Parameters["name"];

                    if (nameParameter != null && nameParameter.Value != null)
                    {
                        attachmentName = nameParameter.Value;
                    }

                    Parameter charsetParameter = this.ContentType.Parameters["charset"];

                    if (charsetParameter != null)
                    {
                        charset = charsetParameter.Value;
                    }
                }

                if (attachmentName == null && this.ContentDisposition != null)
                {
                    Parameter filenameParameter = this.ContentDisposition.Parameters["filename"];

                    if (filenameParameter != null && filenameParameter.Value != null)
                    {
                        attachmentName = filenameParameter.Value;
                    }
                }

                Encoding encoding = Util.GetEncoding(charset);

                byte[] attachmentBuffer = encoding.GetBytes(this.ToString());

                Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                attachment.ContentID          = this.ContentID;
                attachment.ContentLocation    = this.ContentLocation;
                attachment.ContentDisposition = this.ContentDisposition;
                attachment.ContentDescription = this.ContentDescription;
                attachmentList.Add(attachment);
            }
            else if (body != null && ContentType != null && ContentType.Type != null && ContentType.Type.ToLower() != "text")
            {
                string attachmentName = null;

                if (this.ContentType != null)
                {
                    Parameter nameParameter = this.ContentType.Parameters["name"];

                    if (nameParameter != null && nameParameter.Value != null)
                    {
                        attachmentName = nameParameter.Value;
                    }
                }

                if (attachmentName == null && this.ContentDisposition != null)
                {
                    Parameter filenameParameter = this.ContentDisposition.Parameters["filename"];

                    if (filenameParameter != null && filenameParameter.Value != null)
                    {
                        attachmentName = filenameParameter.Value;
                    }
                }

                if (this.ContentTransferEncoding == ContentTransferEncoding.Base64)
                {
                    byte[] attachmentBuffer = Util.DecodeBase64Attachment(body);

                    Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                    attachment.ContentType        = new ContentType(ContentType.Type, ContentType.SubType);
                    attachment.ContentID          = this.ContentID;
                    attachment.ContentLocation    = this.ContentLocation;
                    attachment.ContentDisposition = this.ContentDisposition;
                    attachment.ContentDescription = this.ContentDescription;
                    attachmentList.Add(attachment);
                }
                else
                {
                    byte[] attachmentBuffer = System.Text.Encoding.Default.GetBytes(body);

                    Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                    attachment.ContentType        = new ContentType(ContentType.Type, ContentType.SubType);
                    attachment.ContentID          = this.ContentID;
                    attachment.ContentLocation    = this.ContentLocation;
                    attachment.ContentDisposition = this.ContentDisposition;
                    attachment.ContentDescription = this.ContentDescription;
                    attachmentList.Add(attachment);
                }
            }
            else if (body != null && ContentType != null && ContentType.Type != null && ContentType.Type.ToLower() == "text" && ContentType.Parameters["name"] != null)
            {
                string attachmentName = null;

                if (this.ContentType != null)
                {
                    Parameter nameParameter = this.ContentType.Parameters["name"];

                    if (nameParameter != null && nameParameter.Value != null)
                    {
                        attachmentName = nameParameter.Value;
                    }
                }

                if (attachmentName == null && this.ContentDisposition != null)
                {
                    Parameter filenameParameter = this.ContentDisposition.Parameters["filename"];

                    if (filenameParameter != null && filenameParameter.Value != null)
                    {
                        attachmentName = filenameParameter.Value;
                    }
                }

                byte[] attachmentBuffer = System.Text.Encoding.Default.GetBytes(body);

                Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                attachment.ContentType        = new ContentType(ContentType.Type, ContentType.SubType);
                attachment.ContentID          = this.ContentID;
                attachment.ContentLocation    = this.ContentLocation;
                attachment.ContentDisposition = this.ContentDisposition;
                attachment.ContentDescription = this.ContentDescription;
                attachmentList.Add(attachment);
            }
            else if (body != null && ContentDisposition != null && ContentDisposition.ToString().ToLower() != "inline")
            {
                string attachmentName = null;

                if (this.ContentType != null)
                {
                    Parameter nameParameter = this.ContentType.Parameters["name"];

                    if (nameParameter != null && nameParameter.Value != null)
                    {
                        attachmentName = nameParameter.Value;
                    }
                }

                if (attachmentName == null && this.ContentDisposition != null)
                {
                    Parameter filenameParameter = this.ContentDisposition.Parameters["filename"];

                    if (filenameParameter != null && filenameParameter.Value != null)
                    {
                        attachmentName = filenameParameter.Value;
                    }
                }

                if (this.ContentTransferEncoding == ContentTransferEncoding.Base64)
                {
                    byte[] attachmentBuffer = Util.DecodeBase64Attachment(body);

                    Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                    attachment.ContentID          = this.ContentID;
                    attachment.ContentLocation    = this.ContentLocation;
                    attachment.ContentDisposition = this.ContentDisposition;
                    attachment.ContentDescription = this.ContentDescription;
                    attachmentList.Add(attachment);
                }
                else
                {
                    byte[] attachmentBuffer = System.Text.Encoding.Default.GetBytes(body);

                    Attachment attachment = new Attachment(attachmentBuffer, attachmentName);
                    attachment.ContentID          = this.ContentID;
                    attachment.ContentLocation    = this.ContentLocation;
                    attachment.ContentDisposition = this.ContentDisposition;
                    attachment.ContentDescription = this.ContentDescription;
                    attachmentList.Add(attachment);
                }
            }
            else if (bodyParts.Count > 0)
            {
                for (int i = 0; i < bodyParts.Count; i++)
                {
                    if (bodyParts[i] != null && (includeEmbedded || bodyParts[i].ContentID == null) && bodyParts[i].ContentType != null && ContentType.Type.ToLower() != "text")
                    {
                        Attachment[] bodyPartAttachments = bodyParts[i].GetAttachments(includeEmbedded);

                        for (int j = 0; j < bodyPartAttachments.Length; j++)
                        {
                            attachmentList.Add(bodyPartAttachments[j]);
                        }
                    }
                }
            }

            Attachment[] attachments = new Attachment[attachmentList.Count];

            for (int i = 0; i < attachmentList.Count; i++)
            {
                attachments[i] = attachmentList[i];
            }

            return(attachments);
        }
        private void ParseHeader(byte[] headerBuffer)
        {
            string headerAsciiString = System.Text.Encoding.Default.GetString(headerBuffer, 0, headerBuffer.Length);

            headerAsciiString = Util.Unfolding(headerAsciiString);

            string[] lines = headerAsciiString.Split(Util.CrlfSeparator);

            string currentHeaderName  = null;
            string currentHeaderValue = null;

            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i].Length > 0)
                {
                    if (!lines[i].StartsWith(" ") && !lines[i].StartsWith("\t"))
                    {
                        if (currentHeaderName != null && currentHeaderValue != null)
                        {
                            currentHeaderValue = currentHeaderValue.TrimStart();
                            currentHeaderValue = Util.DecodeHeader(currentHeaderValue, ref headerEncoding);

                            if (Util.IsStandardKey(currentHeaderName))
                            {
                                currentHeaderName = Util.GetCorrectedStandardKey(currentHeaderName);
                            }

                            Header header = new Header(currentHeaderName, currentHeaderValue);
                            headers.Add(header);

                            currentHeaderName  = null;
                            currentHeaderValue = null;
                        }

                        int colonIndex = lines[i].IndexOf(":");

                        if (colonIndex > -1)
                        {
                            currentHeaderName = lines[i].Substring(0, colonIndex);

                            if (currentHeaderName.ToLower() == "content-disposition")
                            {
                                currentHeaderValue = lines[i].Substring(colonIndex + 1);

                                //parse Content-Disposition
                                ContentDisposition cd = new ContentDisposition(currentHeaderValue);
                                currentHeaderValue = cd.ToString();
                            }
                            else
                            {
                                currentHeaderValue = lines[i].Substring(colonIndex + 1);
                            }
                        }
                    }
                    else
                    {
                        lines[i]            = lines[i].Replace("\t", " ");
                        currentHeaderValue += lines[i];
                    }
                }
            }

            if (currentHeaderName != null && currentHeaderValue != null)
            {
                currentHeaderValue = currentHeaderValue.TrimStart();
                currentHeaderValue = Util.DecodeHeader(currentHeaderValue, ref headerEncoding);

                if (Util.IsStandardKey(currentHeaderName))
                {
                    currentHeaderName = Util.GetCorrectedStandardKey(currentHeaderName);
                }

                Header header = new Header(currentHeaderName, currentHeaderValue);
                headers.Add(header);
            }
        }