/// <summary>
 /// Constructor
 /// </summary>
 /// <param name="mp">MimeParser</param>
 /// <param name="parentMultipartBody">Multipart Body of parent messages</param>
 protected MimeMessage(ref MimeParser mp, MimeMultipartBody parentMultipartBody)
 {
     if (parentMultipartBody != null)
     {
         parentMultipartBody.Add(this);
     }
     Parse(ref mp, parentMultipartBody);
 }
        private void Parse(ref MimeParser mp, MimeMultipartBody parentMultipartBody)
        {
            try
            {
                string sLine = "";
                byte [] buffer = null;
                bool isEOC = false;
                bool readBinaryBody = false;

                while(sLine != null)
                {
                    //Check if the Binary encoding header is found
                    if (_headers["content-transfer-encoding"] != null &&
                        MimeBodyPart.GetMimeEncoding(_headers["content-transfer-encoding"].Value) == MimeBodyPart.MimeEncoding.Binary
                        && buffer == null)
                    {
                        readBinaryBody = true;
                    }
                    MimeParser.ChunkType chunkType = mp.ReadNextChunk(ref sLine, ref isEOC, ref readBinaryBody, ref buffer);

                    switch(chunkType)
                    {
                        case MimeParser.ChunkType.VersionHeader:
                        case MimeParser.ChunkType.Header:
                            MimeHeader mh = new MimeHeader(sLine);
                            InitializeMultipart(ref mp, mh);
                            _headers.Add(mh);
                            break;
                        case MimeParser.ChunkType.Body:
                            CreateBody(sLine, buffer);
                            break;
                        case MimeParser.ChunkType.StartBoundary:
                            if (_body == null)
                                CreateBody("", buffer);
                            MimeMessage firstMessage = new MimeMessage(ref mp, (MimeMultipartBody)_body);
                            break;
                        case MimeParser.ChunkType.Boundary:
                            MimeMessage nextMessage = new MimeMessage(ref mp, parentMultipartBody);
                            return;
                        case MimeParser.ChunkType.EndBoundary:
                            return;
                        case MimeParser.ChunkType.EOF:
                            break;
                        default:
                            break;
                    }
                }
            }
            catch(Exception err)
            {
                logger.Error(err);
                throw;
            }
        }
        private void CreateBody(string sLine, byte [] buffer)
        {
            MimeBodyPart mbp = null;
            if (_isMultiPart)
            {
                mbp = new MimeMultipartBody(_boundary);
            }
            else
            {
                mbp = new MimeBasicPart();
            }
            _body = mbp;
            //Set content type
            string charset = null;
            if (_headers["content-type"] != null)
            {
                _body.ContentType = _headers["content-type"].Value;
                if (_headers["content-type"].Parameters["charset"] != null)
                {
                    charset = _headers["content-type"].Parameters["charset"].Value;
                }
            }

            //Set content type
            if (_headers["content-transfer-encoding"] != null)
            {
                _body.Encoding = MimeBodyPart.GetMimeEncoding(_headers["content-transfer-encoding"].Value);
            }

            //Set text
            if (sLine != null)
            {
                if (_body.Encoding == MimeBodyPart.MimeEncoding.Binary)
                {
                    //Copy buffer
                    _body.Buffer = buffer;
                }
                else if (_body.Encoding == MimeBodyPart.MimeEncoding.Base64)
                {
                    _body.Buffer = Convert.FromBase64String(sLine);
                }
                else
                {
                    if (charset != null)
                    {
                        _body.SetText(sLine, MimeBodyPart.GetMimeCharset(charset));
                    }
                    else
                    {
                        _body.SetText(sLine);
                    }
                }
            }
        }