Example #1
0
        protected virtual MemoryStream ReadMessageResponse()
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] bs            = null;
            byte[] bsTmpNextLine = null;
            int    iRealRead;

            string strPrevLine = string.Empty;

            while ((bs = ReadBytes(4096, out iRealRead)) != null)
            {
                if (bsTmpNextLine != null)
                {
                    MemoryStream tmpMemory = new MemoryStream();
                    tmpMemory.Write(bsTmpNextLine, 0, bsTmpNextLine.Length);
                    tmpMemory.Write(bs, 0, iRealRead);
                    bs            = tmpMemory.GetBuffer();
                    iRealRead     = (int)tmpMemory.Length;
                    bsTmpNextLine = null;
                }
                StreamLineReader lineReader = new StreamLineReader(bs, 0, iRealRead);

                byte[] bsNextLine = null;

                System.Diagnostics.Debug.WriteLine("-----------------------------------------------");

                while ((bsNextLine = lineReader.ReadLine()) != null)
                {
                    if (lineReader.CRLFWasFound)
                    {
                        string strLine = Encoding.ASCII.GetString(bsNextLine);

                        if (strLine.StartsWith(".") && strLine.Length == 1)
                        {
                            // we are found end.
                            memoryStream.Flush();
                            memoryStream.Seek(0, SeekOrigin.Begin);
                            return(memoryStream);
                        }
                        System.Diagnostics.Debug.WriteLine(strLine);

                        memoryStream.Write(bsNextLine, 0, bsNextLine.Length);
                        memoryStream.WriteByte((byte)'\r');
                        memoryStream.WriteByte((byte)'\n');

                        strPrevLine = strLine;
                    }
                    else
                    {
                        bsTmpNextLine = bsNextLine;
                    }
                }
            }

            throw new Pop3ServerIncorectAnswerException();
        }
Example #2
0
        protected virtual MemoryStream ReadMessageResponse()
        {
            MemoryStream memoryStream = new MemoryStream();

            byte[] bs = null;
            byte[] bsTmpNextLine = null;
            int iRealRead;

            string strPrevLine = string.Empty;

            while ((bs = ReadBytes(4096, out iRealRead)) != null)
            {
                if (bsTmpNextLine != null)
                {
                    MemoryStream tmpMemory = new MemoryStream();
                    tmpMemory.Write(bsTmpNextLine, 0, bsTmpNextLine.Length);
                    tmpMemory.Write(bs, 0, iRealRead);
                    bs = tmpMemory.GetBuffer();
                    iRealRead = (int)tmpMemory.Length;
                    bsTmpNextLine = null;
                }
                StreamLineReader lineReader = new StreamLineReader(bs, 0, iRealRead);

                byte[] bsNextLine = null;

                System.Diagnostics.Debug.WriteLine("-----------------------------------------------");

                while ((bsNextLine = lineReader.ReadLine()) != null)
                {
                    if (lineReader.CRLFWasFound)
                    {
                        string strLine = Encoding.ASCII.GetString(bsNextLine);

                        if (strLine.StartsWith(".") && strLine.Length == 1)
                        {
                            // we are found end.
                            memoryStream.Flush();
                            memoryStream.Seek(0, SeekOrigin.Begin);
                            return memoryStream;
                        }
                        System.Diagnostics.Debug.WriteLine(strLine);

                        memoryStream.Write(bsNextLine, 0, bsNextLine.Length);
                        memoryStream.WriteByte((byte)'\r');
                        memoryStream.WriteByte((byte)'\n');

                        strPrevLine = strLine;
                    }
                    else
                    {
                        bsTmpNextLine = bsNextLine;
                    }

                }
            }

            throw new Pop3ServerIncorectAnswerException();
        }
Example #3
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;
        }
Example #4
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);
        }