public void BasicWithParametersWithComment()
 {
     MimeHeader header = new MimeHeader("name:value(comment indeed);foo=(comment i..*)foo;fi=fi(comment)");
     AssertionHelper.IsTrue("Name is 'name'", header.Name == "name");
     AssertionHelper.IsTrue("Value is 'value'", header.Value == "value");
     AssertionHelper.IsTrue("There are two parameters", header.Parameters.Count == 2);
     AssertionHelper.IsTrue("The fi parameter has value fi", header.Parameters["fi"].Value == "fi");
 }
 /// <summary>
 /// Add a mime header.
 /// </summary>
 /// <param name="header">The header to add.</param>
 /// <returns>The newly added header.</returns>
 public MimeHeader Add(MimeHeader header)
 {
     string name = header.Name.ToLower();
     if (_headers.Contains(name))
     {
         _headers.Remove(name);
         RemoveFromList(name);
     }
     _listHeaders.Add(header);
     _headers.Add(header.Name.ToLower(), header);
     return header;
 }
        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 InitializeMultipart(ref MimeParser mp, MimeHeader mh)
        {
            //Check Content-Type
            if (mh.Name.ToLower().CompareTo("content-type") == 0
                && mh.Value.StartsWith("multipart"))
            {
                _isMultiPart = true;
                mp.IsMultiPart = _isMultiPart;

                //Check boundary
                if (mh.Parameters["boundary"] != null)
                {
                    _boundary = mh.Parameters["boundary"].Value;
                    mp.Boundary = _boundary;
                }
            }
        }
        /// <summary>
        /// This method checks and adds additional required header fields.  
        /// 1. If it is a text type, add the character set
        /// 2. Check and add Content-Transfer-Encoding
        /// 3. Calculates the content length.  For base64 encoding, this
        /// requires encoding the buffer.  Therefore, the encoded data is stored in
        /// _encodedString for use during the body right when base64 encoding is used.
        /// </summary>
        public void AddAdditionalHeader(MimeHeaderCollection headers)
        {
            //Add Content-Type
            if (!headers.Contains("Content-Type") && _contentType != null)
            {
                MimeHeader mh = new MimeHeader("Content-Type", _contentType);
                //if it is a text type, add the character set
                if (_contentType.StartsWith("text/"))
                {
                    mh.Parameters.Add(new MimeHeaderParam("charset", MimeCharsetToString(_charset)));
                }
                headers.Add(mh);
            }

            //Add Content-Transfer-Encoding
            if (!headers.Contains("Content-Transfer-Encoding") && _encoding != MimeEncoding.E7Bit)
            {
                headers.Add(new MimeHeader("Content-Transfer-Encoding", MimeEncodingToString(_encoding)));
            }

            //Add Content-Disposition: attachment; filename="Job_10235.jdf"
            if (!headers.Contains("Content-Disposition") && _fileName != null)
            {
                headers.Add(new MimeHeader("Content-Disposition", @"attachment; filename=""" + _fileName + @""""));
            }

            //Add Content-Length
            if (_buffer != null)
            {
                int contentLength = 0;
                if (_encoding == MimeEncoding.Base64)
                {
                    _encodedString = Convert.ToBase64String(_buffer, 0, _buffer.Length);
                    contentLength = _encodedString.Length;
                }
                else
                {
                    _encodedString = null;
                    contentLength = _buffer.Length;
                }

                if (contentLength != 0)
                {
                    if (!headers.Contains("Content-Length"))
                    {
                        headers.Add(new MimeHeader("Content-Length", contentLength.ToString()));
                    }
                    else
                    {
                        headers["Content-Length"].Value = contentLength.ToString();
                    }
                }
            }
        }
 public void BasicWithComment()
 {
     MimeHeader header = new MimeHeader("name:val(comment)ue");
     AssertionHelper.IsTrue("Name is 'name'", header.Name == "name");
     AssertionHelper.IsTrue("Value is 'value'", header.Value == "value");
 }
 public void BadLineOnParameter()
 {
     MimeHeader header = new MimeHeader("name:value;foo:fi");
 }
 public void BadLine()
 {
     MimeHeader header = new MimeHeader("name=value");
 }
        public void ParameterEnumerator()
        {
            MimeHeader header = new MimeHeader("name:value;p1=p1;p2=p2;p3=p3");

            int x = 0;
            foreach (MimeHeaderParam param in header.Parameters)
            {
                switch (x)
                {
                    case 0:
                        AssertionHelper.IsTrue("The param for p1=p1", param.Name == "p1" && param.Value == "p1");
                        break;
                    case 1:
                        AssertionHelper.IsTrue("The param for p2=p2", param.Name == "p2" && param.Value == "p2");
                        break;
                    case 2:
                        AssertionHelper.IsTrue("The param for p3=p3", param.Name == "p3" && param.Value == "p3");
                        break;
                    default:
                        AssertionHelper.IsTrue("There are only 3 items in the parameters collection", false);
                        break;
                }
                x++;

            }
        }