Exemple #1
0
        internal MimeEntry(MemoryStream BinaryData, Rfc822HeaderCollection headers)
        {
            _BinaryData = BinaryData;
            _headers    = headers;

            ParseBody();

            _BinaryData = null;
        }
Exemple #2
0
        internal MimeEntry(MemoryStream	BinaryData, Rfc822HeaderCollection	headers)
        {
            _BinaryData = BinaryData;
            _headers = headers;

            ParseBody();

            _BinaryData = null;
        }
Exemple #3
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);
            }
        }
Exemple #4
0
        protected void SaveHeaders(System.IO.StreamWriter	writer, Rfc822HeaderCollection	headers)
        {
            StringBuilder stringBuilder = new StringBuilder(30 * headers.Count);
            //			for (int i = 0; i < headers.Count; i++)
            //			{
            //				string str1 = headers.GetKey(i);
            //				string str2 = headers.Get(i);
            //
            //				if(str1=="Content-Transfer-Encoding")
            //					continue;
            //
            //				str2 = str2.Replace("text/plain","text/html");
            //
            //				if(str2.IndexOf("charset")!=-1)
            //				{
            //					int iCharSetIndex = str2.IndexOf("charset");
            //					int iStartIndex = str2.IndexOf("=",iCharSetIndex);
            //					int iEndIndex = str2.IndexOf(";",iStartIndex);
            //					if(iEndIndex==-1)
            //						iEndIndex = str2.Length;
            //
            //					string CharSet = str2.Substring(iStartIndex+1,iEndIndex-iStartIndex-1);
            //					str2 = str2.Replace(CharSet,"\"utf-8\"");
            //				}
            //
            //				stringBuilder.Append(str1).Append(": ");
            //				stringBuilder.Append(str2).Append("\r\n");
            //			}

            if(headers["Content-Type"]==null)
            {
                stringBuilder.Append("Content-Type").Append(": ");
                stringBuilder.Append("text/html; charset=\"utf-8\"").Append("\r\n");
            }
            else
            {
                stringBuilder.Append("Content-Type").Append(": ");

                string strContentTypeValue = headers["Content-Type"];

                if(!_HasHtmlMessage)
                    strContentTypeValue = strContentTypeValue.Replace("text/plain","text/html");

                ///strContentTypeValue = ChangeCharset(strContentTypeValue);

                stringBuilder.Append(strContentTypeValue).Append("\r\n");
            }

            stringBuilder.Append("Content-Transfer-Encoding").Append(": ");
            stringBuilder.Append("base64").Append("\r\n");

            if(headers["Content-Location"]!=null)
            {
                stringBuilder.Append("Content-Location").Append(": ");
                stringBuilder.Append(headers["Content-Location"]).Append("\r\n");
            }

            if(headers["Content-ID"]!=null)
            {
                stringBuilder.Append("Content-ID").Append(": ");
                stringBuilder.Append(headers["Content-ID"]).Append("\r\n");
            }

            stringBuilder.Append("\r\n");

            writer.Write(stringBuilder.ToString());
        }
Exemple #5
0
        internal DataParseStatus ParseMimeEntries(byte[] buffer, int size, ref int unparsed, Rfc822HeaderCollection headers)
        {
            string strFullConentType	= headers["Content-Type"];
            if(strFullConentType==null)
                strFullConentType="";

            string strContentTypeValue	= null;

            Hashtable	parametrs;
            MimeEntry.ParseHeader(strFullConentType, out strContentTypeValue, out parametrs);

            if(!strContentTypeValue.StartsWith("multipart/", true, CultureInfo.InvariantCulture))
            {
                // Simple Email [1/27/2004]
                MemoryStream strmEntry = new MemoryStream();
                strmEntry.Write(buffer, unparsed, size - unparsed);

                this.Add(new MimeEntry(strmEntry,headers));
            }
            else
            {
                // Parse Multipart Value
                string boundaryID = (string)parametrs["boundary"];

                // Step 1. Unpack multipart [2/10/2004]
                StreamLineReader	lineReader = new StreamLineReader (buffer, unparsed, size);
                byte[] lineData = lineReader.ReadLine();

                // Step 1.1 Search first entry
                while(lineData!=null)
                {
                    string strLine = System.Text.Encoding.Default.GetString(lineData);
                    if(strLine.StartsWith("--" + boundaryID))
                    {
                        lineData = lineReader.ReadLine();
                        break;
                    }
                    lineData = lineReader.ReadLine();
                }

                // Step 1.2 Start reading entries
                MemoryStream strmEntry = new MemoryStream();

                while(lineData!=null)
                {
                    string strLine = System.Text.Encoding.Default.GetString(lineData);
                    if(strLine.StartsWith("--" + boundaryID) && strmEntry.Length > 0)
                    {
                        // Step 1.3 Add Entry
                        try
                        {
                            MimeEntry newEntry = new MimeEntry(strmEntry);
                            this.Add(newEntry);
                        }
                        catch (Pop3ServerIncorectEMailFormatException ex)
                        {
                            // Skeep Broken Entry
                            if (ex.InnerException != null)
                                throw;
                        }

                        strmEntry.SetLength(0);
                    }
                    else
                    {
                        strmEntry.Write(lineData,0,lineData.Length);
                        strmEntry.Write(new byte[]{(byte)'\r',(byte)'\n'},0,2);
                    }

                    lineData = lineReader.ReadLine();
                }
            }

            return DataParseStatus.Done;
        }
Exemple #6
0
        internal DataParseStatus ParseMimeEntries(byte[] buffer, int size, ref int unparsed, Rfc822HeaderCollection headers)
        {
            string strFullConentType = headers["Content-Type"];

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

            string strContentTypeValue = null;

            Hashtable parametrs;

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

            if (!strContentTypeValue.StartsWith("multipart/", true, CultureInfo.InvariantCulture))
            {
                // Simple Email [1/27/2004]
                MemoryStream strmEntry = new MemoryStream();
                strmEntry.Write(buffer, unparsed, size - unparsed);

                this.Add(new MimeEntry(strmEntry, headers));
            }
            else
            {
                // Parse Multipart Value
                string boundaryID = (string)parametrs["boundary"];

                // Step 1. Unpack multipart [2/10/2004]
                StreamLineReader lineReader = new StreamLineReader(buffer, unparsed, size);
                byte[]           lineData   = lineReader.ReadLine();

                // Step 1.1 Search first entry
                while (lineData != null)
                {
                    string strLine = System.Text.Encoding.Default.GetString(lineData);
                    if (strLine.StartsWith("--" + boundaryID))
                    {
                        lineData = lineReader.ReadLine();
                        break;
                    }
                    lineData = lineReader.ReadLine();
                }

                // Step 1.2 Start reading entries
                MemoryStream strmEntry = new MemoryStream();

                while (lineData != null)
                {
                    string strLine = System.Text.Encoding.Default.GetString(lineData);
                    if (strLine.StartsWith("--" + boundaryID) && strmEntry.Length > 0)
                    {
                        // Step 1.3 Add Entry
                        try
                        {
                            MimeEntry newEntry = new MimeEntry(strmEntry);
                            this.Add(newEntry);
                        }
                        catch (Pop3ServerIncorectEMailFormatException ex)
                        {
                            // Skeep Broken Entry
                            if (ex.InnerException != null)
                            {
                                throw;
                            }
                        }

                        strmEntry.SetLength(0);
                    }
                    else
                    {
                        strmEntry.Write(lineData, 0, lineData.Length);
                        strmEntry.Write(new byte[] { (byte)'\r', (byte)'\n' }, 0, 2);
                    }

                    lineData = lineReader.ReadLine();
                }
            }


            return(DataParseStatus.Done);
        }