Exemplo n.º 1
0
        protected virtual void ParseBody()
        {
            string strFullConentType = _headers["Content-Type"];

            if (strFullConentType == null)
            {
                strFullConentType = "";
            }

            //string strContentTypeValue	= null;

            Hashtable parametrs;

            MimeEntry.ParseHeader(strFullConentType, out _contentType, out parametrs);

            // Step 2. Parse Messagy Body [1/23/2004]
            if (!_contentType.StartsWith("multipart/"))
            {
                _charSet = (string)parametrs["charset"];

                if (_charSet == null)
                {
                    _charSet = Encoding.Default.HeaderName;
                }

                string ContentEncoding = _headers["Content-Transfer-Encoding"];

                if (ContentEncoding == null)
                {
                    ContentEncoding = "8bit";
                }

                string strDisposition = _headers["Content-Disposition"];

                if (strDisposition != null)
                {
                    Hashtable DispositionParameters;
                    string    DispositionType;
                    MimeEntry.ParseHeader(strDisposition, out DispositionType, out DispositionParameters);

                    DispositionType = DispositionType.ToLower();

                    if (DispositionType == "attachment")
                    {
                        this._disposition = Disposition.Attachment;
                    }
                    else if (DispositionType == "inline")
                    {
                        this._disposition = Disposition.Inline;
                    }

                    _fileName = (string)DispositionParameters["filename"];
                    if (_fileName != null)
                    {
                        _fileName = Rfc822HeaderCollection.DeocodeHeaderValue(_fileName);
                    }
                }

                //string BodyString = Encoding.Default.GetString(this._BinaryData.GetBuffer(),this._BodyOffset,(int)(this._BinaryData.Length  - this._BodyOffset));
                Encoding encoding = null;
                try
                {
                    encoding = Encoding.GetEncoding(CharSet);
                }
                catch
                {
                    encoding = Encoding.Default;
                }


                string BodyString = encoding.GetString(this._BinaryData.GetBuffer(), this._BodyOffset, (int)(this._BinaryData.Length - this._BodyOffset));

                //string BodyString = Encoding.ASCII.GetString(this._BinaryData.GetBuffer(),this._BodyOffset,(int)(this._BinaryData.Length  - this._BodyOffset));
                //string BodyString2 = Encoding.UTF8.GetString(this._BinaryData.GetBuffer(),this._BodyOffset,(int)(this._BinaryData.Length  - this._BodyOffset));

                switch (ContentEncoding.ToLower())
                {
                case "quoted-printable":
                    _body = encoding.GetBytes(MimeEntry.QDecode(encoding, BodyString));
                    break;

                case "7bit":
                    //_body = Encoding.ASCII.GetBytes(BodyString);
                    _body = encoding.GetBytes(BodyString);
                    break;

                default:
                case "8bit":
                    _body = encoding.GetBytes(BodyString);
                    break;

                case "base64":
                    BodyString = BodyString.Trim();

                    if (BodyString.Length > 0)
                    {
                        int base64FixCount = 0;

                        // Fix If Base 64 is broken
                        while (true)
                        {
                            try
                            {
                                _body = Convert.FromBase64String(BodyString);
                                break;
                            }
                            catch (System.FormatException)
                            {
                                // Remove not supported chars
                                if (base64FixCount == 0)
                                {
                                    BodyString = Regex.Replace(BodyString, "[^a-zA-Z0-9+/=]+", string.Empty);
                                }
                                else if (base64FixCount == 1)
                                {
                                    BodyString += "=";
                                }
                                else
                                {
                                    BodyString = BodyString.Substring(0, BodyString.Length - 1);
                                }

                                if (BodyString.Length == 0 || base64FixCount == 25)                                         // Max 25 Attempts to fix chars
                                {
                                    _body = new byte[] { };
                                    break;
                                }

                                base64FixCount++;
                            }
                        }
                    }
                    else
                    {
                        _body = new byte[] {}
                    };
                    break;

                case "binary":
                    _body = encoding.GetBytes(BodyString);
                    break;
                    //default:
                    //    throw new Pop3ServerIncorectEMailFormatException("Not supported content-encoding " + ContentEncoding + " !");
                }
            }
            else
            {
                DataParseStatus parseStatus = _mimeEntries.ParseMimeEntries(_BinaryData.GetBuffer(), (int)_BinaryData.Length, ref _BodyOffset, this.Headers);
            }
        }
Exemplo n.º 2
0
        public static string DeocodeHeaderValue(string headerValue)
        {
            // Cool Encoding [2/13/2004]
            MatchCollection matchList = Regex.Matches(headerValue, @"(?<encodedword>=\?(?<charset>[^?]+)\?(?<encoding>[^?]+)\?(?<encodedtext>[^?]+)\?=)");

            if (matchList.Count > 0)
            {
                // Fix: White space between adjacent 'encoded-word's is not displayed
                // [2007-07-03] TODO (=?ISO-8859-1?Q?a?= =?ISO-8859-1?Q?b?=)   ->  (ab)
                // [2007-07-03] TODO (=?ISO-8859-1?Q?a?=  =?ISO-8859-1?Q?b?=)   ->  (ab)
                // [2007-07-03] TODO (=?ISO-8859-1?Q?a?=\r\n  =?ISO-8859-1?Q?b?=)   ->  (ab)
                headerValue = Regex.Replace(headerValue, @"\?=[ ]+=\?", @"?==?");

                System.Text.StringBuilder strBilder = new System.Text.StringBuilder(headerValue);

                foreach (Match matchItem in matchList)
                {
                    string strEncodedWord = matchItem.Groups["encodedword"].Value;

                    string strCharset     = matchItem.Groups["charset"].Value;
                    string strEncoding    = matchItem.Groups["encoding"].Value;
                    string strEncodedText = matchItem.Groups["encodedtext"].Value;


                    if (matchItem.Groups["spaceceprator"].Success)
                    {
                        strBilder.Replace(strEncodedWord + matchItem.Groups["spaceceprator"].Value, strEncodedWord);
                    }

                    System.Text.Encoding encoder = System.Text.Encoding.GetEncoding(strCharset);

                    string DecodedString = null;

                    if (strEncoding.ToUpper() == "Q")
                    {
                        // FIX: The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
                        // represented as "_" (underscore, ASCII 95.).  (This character may
                        // not pass through some internetwork mail gateways, but its use
                        // will greatly enhance readability of "Q" encoded data with mail
                        // readers that do not support this encoding.)

                        DecodedString = MimeEntry.QDecode(encoder, strEncodedText.Replace('_', ' '));
                    }
                    else if (strEncoding.ToUpper() == "B")
                    {
                        int base64FixCount = 0;

                        while (true)
                        {
                            try
                            {
                                byte[] FromBase64String = Convert.FromBase64String(strEncodedText);
                                DecodedString = encoder.GetString(FromBase64String);
                                break;
                            }
                            catch (System.FormatException)
                            {
                                // Remove not supported chars
                                if (base64FixCount == 0)
                                {
                                    strEncodedText = Regex.Replace(strEncodedText, "[^a-zA-Z0-9+/=]+", string.Empty);
                                }
                                else if (base64FixCount == 1)
                                {
                                    strEncodedText += "=";
                                }
                                else
                                {
                                    strEncodedText = strEncodedText.Substring(0, strEncodedText.Length - 1);
                                }

                                if (strEncodedText.Length == 0 || base64FixCount == 25)                                 // Max 25 Attempts to fix chars
                                {
                                    DecodedString = null;
                                    break;
                                }

                                base64FixCount++;
                            }
                        }
                    }

                    if (DecodedString != null)
                    {
                        strBilder.Replace(strEncodedWord, DecodedString);
                    }
                }

                return(strBilder.ToString());
            }

//			if(headerValue.StartsWith("=?"))
//			{
//				string[] parts = headerValue.Substring(2).Split(new char[]{'?'});
//
//				string encoderName	= parts[0];
//				string type			= parts[1];
//				string datax		= parts[2];
//
//				System.Text.Encoding encoder = System.Text.Encoding.GetEncoding(encoderName);
//				if(type.ToUpper() == "Q")
//				{
//					return MimeEntry.QDecode(encoder,datax);
//				}
//				else if(type.ToUpper() == "B")
//				{
//					return encoder.GetString(Convert.FromBase64String(datax));
//				}
//			}

            return(headerValue);
        }