示例#1
0
        private void SetMimeContentBody(MimeContent content, MimeContentByteArray bodyBuffer)
        {
            var mc = content;

            if (this.Filter.LoadContentRawData == true)
            {
                mc.RawData = bodyBuffer.GetEntireArray();
            }
            if (this.Filter.LoadContentBodyData == false)
            {
                return;
            }

            var bodyBytes = bodyBuffer.GetBodyArray();

            this.AddToRawDataWithNewline(bodyBytes);

            if (mc.ContentType.IsText == true)
            {
                mc.BodyRawText = this.Encoding.GetString(bodyBytes);
                mc.BodyData    = bodyBytes;

                var charSet = mc.ContentType.CharsetEncoding;
                //if charset is ommit - use ASCII
                if (charSet == null)
                {
                    charSet = Encoding.ASCII;
                }

                switch (mc.ContentTransferEncoding)
                {
                case TransferEncoding.None:
                case TransferEncoding.SevenBit: mc.BodyText = charSet.GetString(bodyBytes); break;

                case TransferEncoding.Base64: mc.BodyText = charSet.GetString(_Base64Converter.Decode(bodyBytes)); break;

                case TransferEncoding.QuotedPrintable: mc.BodyText = charSet.GetString(_QuotedPrintableBodyConverter.Decode(bodyBytes)); break;

                case TransferEncoding.EightBit: mc.BodyText = charSet.GetString(bodyBytes); break;

                default: throw new InvalidOperationException();
                }
            }
            mc.BodyData = bodyBytes;

            if (mc.ContentType.IsRfc822)
            {
                var mg = this.Parse <MailMessage>(new MemoryStream(mc.BodyData), new MailMessage());
                mc.MailMessage = mg;
            }
        }
示例#2
0
        private List <MimeContent> ReadMimeContent(MimeStreamBuffer context, Byte[] boundary)
        {
            List <MimeContent>        l             = new List <MimeContent>();
            MimeContent               mc            = null;
            MimeHeaderBufferByteArray headerPointer = _HeaderBuffer;
            var bodyBuffer     = new MimeContentByteArray();
            var state          = MimeContentParserState.Boundary;
            var boundaryResult = CheckBoundaryResult.None;
            var isEndOfBody    = false;

            Byte[]           childBoundary = null;
            Byte[]           line          = null;
            MimeStreamBuffer cx            = context;

            mc = new MimeContent();
            headerPointer.Clear();
            while (true)
            {
                if (context == null)
                {
                    this.ReadFromStream(cx);
                }
                context = null;

                while (true)
                {
                    switch (state)
                    {
                    case MimeContentParserState.Boundary:
                        #region
                    {
                        line = cx.ReadLine();
                        if (line.Length == 0)
                        {
                            continue;
                        }
                        this.AddToRawData(line);
                        if (line.Length == 2 && line[0] == 13 && line[1] == 10)
                        {
                            continue;
                        }

                        bodyBuffer.AddBoundaryLine(line);
                        if (IsEndByNewLine(line) == true)
                        {
                            boundaryResult = bodyBuffer.CheckBoundary(boundary);
                            switch (boundaryResult)
                            {
                            case CheckBoundaryResult.None:
                            {
                                var lineText = this.Encoding.GetString(line);
                                throw new InvalidMimeFormatException(lineText);
                            }

                            case CheckBoundaryResult.Boundary:
                            {
                                headerPointer.Clear();
                                state = MimeContentParserState.Header;
                            }
                            break;

                            case CheckBoundaryResult.EndBoundary: break;

                            default: throw new InvalidOperationException();
                            }
                        }
                    }
                    break;

                        #endregion
                    case MimeContentParserState.Header:
                        #region
                    {
                        headerPointer = cx.ReadHeader(headerPointer);
                        if (headerPointer.IsEmptyNewLine() == true)
                        {
                            var bb = headerPointer.ToArray();
                            this.AddToRawData(bb);
                            bodyBuffer.AddHeaderLine(bb);
                            bodyBuffer.HeaderLength = bodyBuffer.Length;
                            headerPointer.Clear();

                            if (mc.ContentType != null && String.IsNullOrEmpty(mc.ContentType.Boundary) == false)
                            {
                                childBoundary = CreateBoundary("--" + mc.ContentType.Boundary);
                                state         = MimeContentParserState.ChildContent;
                            }
                            else
                            {
                                state = MimeContentParserState.Body;
                            }
                        }
                        else if (headerPointer.IsEnd == true)
                        {
                            var bb = headerPointer.ToArray();
                            this.AddToRawData(bb);
                            //New MimeHeader
                            var header = ParseHeader(headerPointer);
                            mc.Headers.Add(header);
                            headerPointer.Clear();
                            bodyBuffer.AddHeaderLine(bb);
                        }
                    }
                    break;

                        #endregion
                    case MimeContentParserState.Body:
                        #region
                    {
                        var bodyLine = cx.ReadBody(boundary, out boundaryResult, out isEndOfBody);
                        bodyBuffer.AddBodyLine(bodyLine);
                        if (boundaryResult != CheckBoundaryResult.None || isEndOfBody == true)
                        {
                            this.SetMimeContentBody(mc, bodyBuffer);
                            l.Add(mc);

                            headerPointer.Clear();
                            bodyBuffer.Clear();
                            mc = new MimeContent();

                            state = MimeContentParserState.Boundary;
                        }
                    }
                    break;

                        #endregion
                    case MimeContentParserState.ChildContent:
                        #region
                    {
                        var bodyLine = cx.ReadBody(childBoundary, out boundaryResult, out isEndOfBody);
                        bodyBuffer.AddBodyLine(bodyLine);
                        if (boundaryResult != CheckBoundaryResult.None || isEndOfBody == true)
                        {
                            if (bodyBuffer.Length > 0)
                            {
                                this.AddToRawDataWithNewline(bodyBuffer.GetBodyArray());
                            }

                            mc.Contents.AddRange(ReadMimeContent(cx, childBoundary));

                            bodyBuffer.Clear();

                            state = MimeContentParserState.Body;
                        }
                    }
                    break;

                        #endregion
                    default: throw new InvalidOperationException();
                    }
                    if (cx.IsEnd() == true)
                    {
                        break;
                    }
                    if (isEndOfBody == true || boundaryResult == CheckBoundaryResult.EndBoundary)
                    {
                        break;
                    }
                }
                if (isEndOfBody == true || boundaryResult == CheckBoundaryResult.EndBoundary || cx.EndOfStream == true)
                {
                    break;
                }
            }
            if (boundaryResult == CheckBoundaryResult.EndBoundary)
            {
                this.AddToRawData(boundary);
                this.AddToRawData(new Byte[] { 45, 45 });
            }
            return(l);
        }