Esempio n. 1
0
        private string CreateEntity(ref Stream dataStream, ref IMultipartEntity parent, out IEntity entity)
        {
            entity = null;
            IList <RFC822.Field> fields;
            int cause = ParseFields(ref dataStream, out fields);

            if (cause > 0)
            {
                foreach (RFC822.Field contentField in fields)
                {
                    if (contentField is ContentTypeField)
                    {
                        ContentTypeField contentTypeField = contentField as ContentTypeField;

                        if (m_FieldParser.CompositeType.IsMatch(contentTypeField.Type))
                        {
                            MultipartEntity mEntity = new MultipartEntity();
                            mEntity.Fields = fields;
                            entity         = mEntity;
                            entity.Parent  = parent;
                            parent.BodyParts.Add(entity);

                            if (Regex.IsMatch(contentTypeField.Type, "(?i)message") &&
                                Regex.IsMatch(contentTypeField.SubType, "(?i)rfc822"))
                            {
                                Message message = new Message();
                                IList <RFC822.Field> messageFields;
                                cause          = ParseFields(ref dataStream, out messageFields);
                                message.Fields = messageFields;
                                mEntity.BodyParts.Add(message);
                                message.Parent = mEntity;
                                if (cause > 0)
                                {
                                    return(ParseMessage(ref dataStream, ref message, messageFields));
                                }
                                break;
                            }
                            else
                            {
                                mEntity.Delimiter = ReadDelimiter(ref contentTypeField);
                                return(parent.Delimiter);
                            }
                        }
                        else if (m_FieldParser.DescriteType.IsMatch(contentTypeField.Type))
                        {
                            entity        = new Entity();
                            entity.Fields = fields;
                            entity.Parent = parent;
                            parent.BodyParts.Add(entity);
                            return(parent.Delimiter);
                        }
                    }
                }
            }
            return(string.Empty);
        }
Esempio n. 2
0
 private string ReadDelimiter(ref ContentTypeField contentTypeField)
 {
     if (contentTypeField.Parameters["boundary"] != null)
     {
         return(contentTypeField.Parameters["boundary"]);
     }
     else
     {
         return(string.Empty);
     }
 }
Esempio n. 3
0
        private void LoadAttachments(IEntity parent)
        {
            if (parent is MultipartEntity)
            {
                MultipartEntity mpe = parent as MultipartEntity;
                foreach (Entity entity in mpe.BodyParts)
                {
                    if (entity is MultipartEntity && !(entity is Message))
                    {
                        LoadAttachments(entity);
                    }
                    else if (!(entity is MultipartEntity) && !(entity is Message))
                    {
                        ContentDispositionField dispositionField = null;
                        ContentTypeField        contentTypeField = null;

                        foreach (RFC822.Field field in entity.Fields)
                        {
                            if (field is RFC2183.ContentDispositionField)
                            {
                                dispositionField = field as ContentDispositionField;
                            }

                            if (field is ContentTypeField)
                            {
                                contentTypeField = field as ContentTypeField;
                            }
                        }

                        if (dispositionField != null && contentTypeField != null)
                        {
                            IAttachment attachment = new Attachment();
                            attachment.Disposition = dispositionField.Disposition;
                            attachment.Name        = dispositionField.Parameters["filename"];
                            attachment.Data        = entity.Body;
                            attachment.Type        = contentTypeField.Type;
                            attachment.SubType     = contentTypeField.SubType;
                            m_Attachments.Add(attachment);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        private void LoadBody(Entity parent)
        {
            if (parent is MultipartEntity)
            {
                MultipartEntity mpe = parent as MultipartEntity;
                foreach (Entity child in mpe.BodyParts)
                {
                    if (child != parent && !(child is Message))
                    {
                        LoadBody(child);
                    }
                }
            }

            ContentTypeField        contentTypeField        = null;
            ContentDispositionField contentDispositionField = null;

            foreach (RFC822.Field field in parent.Fields)
            {
                if (field is ContentTypeField)
                {
                    ContentTypeField contentField = field as ContentTypeField;
                    if (contentField.Type.ToLower().Equals("text"))
                    {
                        contentTypeField = contentField;
                    }
                }

                if (field is ContentDispositionField)
                {
                    contentDispositionField = field as ContentDispositionField;
                }
            }

            if (contentTypeField != null &&
                (contentDispositionField == null ||
                 contentDispositionField.Disposition.ToLower().Equals("inline")))
            {
                string text = ToString(parent.Body);
                m_Body.Add(contentTypeField.Type + "/" + contentTypeField.SubType,
                           text);
            }
        }
Esempio n. 5
0
        public override void Parse(ref IList <RFC822.Field> fields, ref string fieldString)
        {
            if (!IsPatternCompiled())
            {
                CompilePattern();
            }

            if (fields.Count == 0)
            {
                base.Parse(ref fields, ref fieldString);
            }

            MatchCollection matches = m_Content.Matches(fieldString);

            foreach (Match match in matches)
            {
                ContentTypeField tmpContent = new ContentTypeField();
                tmpContent.Name = "Content-Type";
                MatchCollection parameters;
                string          key, val;

                Match tmpMatch = Regex.Match(match.Value, ":.+");

                tmpContent.Body    = tmpMatch.Value.TrimStart(new char[] { ':' });
                tmpMatch           = m_Type.Match(match.Value);
                tmpContent.Type    = tmpMatch.Value;
                tmpMatch           = m_SubType.Match(match.Value);
                tmpContent.SubType = tmpMatch.Value;
                parameters         = m_Parameters.Matches(match.Value);
                foreach (Match m in parameters)
                {
                    tmpMatch = Regex.Match(m.Value, m_TokenPattern + "=");
                    key      = tmpMatch.Value.TrimEnd(new char[] { '=' });
                    tmpMatch = Regex.Match(m.Value, "(?<==)" + m_ValuePattern);
                    val      = tmpMatch.Value.Trim(new char[] { '\\', '"' });
                    tmpContent.Parameters.Add(key, val);
                }

                fields.Add(tmpContent);
            }
        }
Esempio n. 6
0
        private string ParseMessage(ref Stream dataStream, ref Message message, IList <RFC822.Field> fields)
        {
            foreach (RFC822.Field contentField in fields)
            {
                if (contentField is ContentTypeField)
                {
                    ContentTypeField contentTypeField = contentField as ContentTypeField;

                    if (m_FieldParser.CompositeType.IsMatch(contentTypeField.Type))
                    {//是复合邮件
                        IMultipartEntity e = message as IMultipartEntity;
                        e.Delimiter = ReadDelimiter(ref contentTypeField);
                        return(ReadCompositeEntity(ref dataStream, ref e));
                    }
                    else if (m_FieldParser.DescriteType.IsMatch(contentTypeField.Type))
                    {
                        IEntity e = message as IEntity;
                        message.BodyParts.Add(e);// This is a message witch body lies within its own entity
                        return(ReadDiscreteEntity(ref dataStream, ref e));
                    }
                }
            }
            return(string.Empty);
        }