Beispiel #1
0
 private void BuildMultiPartMessage(MimeEntity entity, MailMessageEx message)
 {
     foreach (MimeEntity entity2 in entity.Children)
     {
         if (entity2 == null)
         {
             continue;
         }
         if (string.Equals(entity2.ContentType.MediaType, MediaTypes.MultipartAlternative, StringComparison.InvariantCultureIgnoreCase) || string.Equals(entity2.ContentType.MediaType, MediaTypes.MultipartMixed, StringComparison.InvariantCultureIgnoreCase))
         {
             this.BuildMultiPartMessage(entity2, message);
             continue;
         }
         if (!IsAttachment(entity2) && (string.Equals(entity2.ContentType.MediaType, MediaTypes.TextPlain) || string.Equals(entity2.ContentType.MediaType, MediaTypes.TextHtml)))
         {
             message.AlternateViews.Add(this.CreateAlternateView(entity2));
             this.SetMessageBody(message, entity2);
             continue;
         }
         if (string.Equals(entity2.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase) && string.Equals(entity2.ContentDisposition.DispositionType, "attachment", StringComparison.InvariantCultureIgnoreCase))
         {
             message.Children.Add(this.ToMailMessageEx(entity2));
         }
         else if (IsAttachment(entity2))
         {
             message.Attachments.Add(this.CreateAttachment(entity2));
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MimeEntity"/> class.
 /// </summary>
 public MimeEntity()
 {
     _children = new List<MimeEntity>();
     _headers = new NameValueCollection();
     _contentType = MimeReader.GetContentType(string.Empty);
     _parent = null;
     _encodedMessage = new StringBuilder();
 }
Beispiel #3
0
 public MimeEntity(MimeEntity parent) : this()
 {
     if (parent == null)
     {
         throw new ArgumentNullException("parent");
     }
     this._parent = parent;
     this._startBoundary = parent.StartBoundary;
 }
Beispiel #4
0
 private MimeReader(MimeEntity entity, Queue<string> lines) : this()
 {
     if (entity == null)
     {
         throw new ArgumentNullException("entity");
     }
     if (lines == null)
     {
         throw new ArgumentNullException("lines");
     }
     this._lines = lines;
     this._entity = new MimeEntity(entity);
 }
Beispiel #5
0
 /// <summary>
 /// Sets the message body.
 /// </summary>
 /// <param name="message">The message.</param>
 /// <param name="child">The child.</param>
 private void SetMessageBody(MailMessageEx message, MimeEntity child)
 {
     Encoding encoding = child.GetEncoding();
     message.Body = DecodeBytes(child.Content.ToArray(), encoding);
     message.BodyEncoding = encoding;
     message.IsBodyHtml = string.Equals(MediaTypes.TextHtml,
         child.ContentType.MediaType, StringComparison.InvariantCultureIgnoreCase);
 }
Beispiel #6
0
        /// <summary>
        /// Creates the attachment.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        private Attachment CreateAttachment(MimeEntity entity)
        {
            Attachment attachment = new Attachment(entity.Content, entity.ContentType);

            if (entity.ContentDisposition != null)
            {
                attachment.ContentDisposition.Parameters.Clear();
                foreach (string key in entity.ContentDisposition.Parameters.Keys)
                {
                    attachment.ContentDisposition.Parameters.Add(key, entity.ContentDisposition.Parameters[key]);
                }

                attachment.ContentDisposition.CreationDate = entity.ContentDisposition.CreationDate;
                attachment.ContentDisposition.DispositionType = entity.ContentDisposition.DispositionType;
                attachment.ContentDisposition.FileName = entity.ContentDisposition.FileName;
                attachment.ContentDisposition.Inline = entity.ContentDisposition.Inline;
                attachment.ContentDisposition.ModificationDate = entity.ContentDisposition.ModificationDate;
                attachment.ContentDisposition.ReadDate = entity.ContentDisposition.ReadDate;
                attachment.ContentDisposition.Size = entity.ContentDisposition.Size;
            }

            if (!string.IsNullOrEmpty(entity.ContentId))
            {
                attachment.ContentId = TrimBrackets(entity.ContentId);
            }

            attachment.TransferEncoding = entity.ContentTransferEncoding;

            return attachment;
        }
Beispiel #7
0
 /// <summary>
 /// Creates the alternate view.
 /// </summary>
 /// <param name="view">The view.</param>
 /// <returns></returns>
 private AlternateView CreateAlternateView(MimeEntity view)
 {
     AlternateView alternateView = new AlternateView(view.Content, view.ContentType);
     alternateView.TransferEncoding = view.ContentTransferEncoding;
     alternateView.ContentId = TrimBrackets(view.ContentId);
     return alternateView;
 }
Beispiel #8
0
 /// <summary>
 /// Builds the single part message.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <param name="message">The message.</param>
 private void BuildSinglePartMessage(MimeEntity entity, MailMessageEx message)
 {
     SetMessageBody(message, entity);
 }
Beispiel #9
0
 private static bool IsAttachment(MimeEntity child)
 {
     return(child.ContentDisposition != null && string.Equals(child.ContentDisposition.DispositionType, "attachment", StringComparison.InvariantCultureIgnoreCase));
 }
Beispiel #10
0
        /// <summary>
        /// Creates the mail message from entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        public static MailMessageEx CreateMailMessageFromEntity(MimeEntity entity)
        {
            MailMessageEx message = new MailMessageEx();
            string value;
            foreach (string key in entity.Headers.AllKeys)
            {
                value = entity.Headers[key];
                if (value.Equals(string.Empty))
                {
                    value = " ";
                }

                message.Headers.Add(key.ToLowerInvariant(), value);

                switch (key.ToLowerInvariant())
                {
                    case MailHeaders.Bcc:
                        MailMessageEx.PopulateAddressList(value, message.Bcc);
                        break;
                    case MailHeaders.Cc:
                        MailMessageEx.PopulateAddressList(value, message.CC);
                        break;
                    case MailHeaders.From:
                        message.From = MailMessageEx.CreateMailAddress(value);
                        break;
                    case MailHeaders.ReplyTo:
                        message.ReplyTo = MailMessageEx.CreateMailAddress(value);
                        break;
                    case MailHeaders.Subject:
                        message.Subject = value;
                        break;
                    case MailHeaders.To:
                        MailMessageEx.PopulateAddressList(value, message.To);
                        break;
                }
            }

            return message;
        }
Beispiel #11
0
        /// <summary>
        /// Adds the child entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        private void AddChildEntity(MimeEntity entity, Queue<string> lines)
        {
            /*if (entity == null)
            {
                return;
            }

            if (lines == null)
            {
                return;
            }*/

            MimeReader reader = new MimeReader(entity, lines);
            entity.Children.Add(reader.CreateMimeEntity());
        }
Beispiel #12
0
 private AlternateView CreateAlternateView(MimeEntity view)
 {
     return new AlternateView(view.Content, view.ContentType) { TransferEncoding = view.ContentTransferEncoding, ContentId = TrimBrackets(view.ContentId) };
 }
Beispiel #13
0
 private MailMessageEx ToMailMessageEx(MimeEntity entity)
 {
     if (entity == null)
     {
         return null;
     }
     MailMessageEx message = MailMessageEx.CreateMailMessageFromEntity(entity);
     if (!string.IsNullOrEmpty(entity.ContentType.Boundary))
     {
         message = MailMessageEx.CreateMailMessageFromEntity(entity);
         this.BuildMultiPartMessage(entity, message);
         return message;
     }
     if (string.Equals(entity.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase))
     {
         if (entity.Children.Count < 0)
         {
             throw new Pop3Exception("Invalid child count on message/rfc822 entity.");
         }
         message = MailMessageEx.CreateMailMessageFromEntity(entity.Children[0]);
         this.BuildMultiPartMessage(entity, message);
         return message;
     }
     message = MailMessageEx.CreateMailMessageFromEntity(entity);
     this.BuildSinglePartMessage(entity, message);
     return message;
 }
Beispiel #14
0
        /// <summary>
        /// Toes the mail message ex.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        private MailMessageEx ToMailMessageEx(MimeEntity entity)
        {
            if (entity == null)
            {
                //throw new ArgumentNullException("entity");
                return null;
            }

            //parse standard headers and create base email.
            MailMessageEx message = MailMessageEx.CreateMailMessageFromEntity(entity);

            if (!string.IsNullOrEmpty(entity.ContentType.Boundary))
            {
                message = MailMessageEx.CreateMailMessageFromEntity(entity);
                BuildMultiPartMessage(entity, message);
            }//parse multipart message into sub parts.
            else if (string.Equals(entity.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase))
            {
                //use the first child to create the multipart message.
                if (entity.Children.Count < 0)
                {
                    throw new Pop3Exception("Invalid child count on message/rfc822 entity.");
                }

                //create the mail message from the first child because it will
                //contain all of the mail headers.  The entity in this state
                //only contains simple content type headers indicating, disposition, type and description.
                //This means we can't create the mail message from this type as there is no
                //internet mail headers attached to this entity.
                message = MailMessageEx.CreateMailMessageFromEntity(entity.Children[0]);
                BuildMultiPartMessage(entity, message);
            } //parse nested message.
            else
            {
                message = MailMessageEx.CreateMailMessageFromEntity(entity);
                BuildSinglePartMessage(entity, message);
            } //Create single part message.

            return message;
        }
Beispiel #15
0
 private static bool IsAttachment(MimeEntity child)
 {
     return (child.ContentDisposition != null)
         && (string.Equals(child.ContentDisposition.DispositionType, DispositionTypeNames.Attachment, StringComparison.InvariantCultureIgnoreCase));
 }
Beispiel #16
0
 private MimeReader()
 {
     this._entity = new MimeEntity();
 }
Beispiel #17
0
        /// <summary>
        /// Builds the multi part message.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="message">The message.</param>
        private void BuildMultiPartMessage(MimeEntity entity, MailMessageEx message)
        {
            foreach (MimeEntity child in entity.Children)
            {
                if (child == null)
                {
                    continue;
                }
                if (string.Equals(child.ContentType.MediaType, MediaTypes.MultipartAlternative, StringComparison.InvariantCultureIgnoreCase)
                    || string.Equals(child.ContentType.MediaType, MediaTypes.MultipartMixed, StringComparison.InvariantCultureIgnoreCase))
                {
                    BuildMultiPartMessage(child, message);
                }  //if the message is mulitpart/alternative or multipart/mixed then the entity will have children needing parsed.
                else if (!IsAttachment(child) &&
                    (string.Equals(child.ContentType.MediaType, MediaTypes.TextPlain)
                    || string.Equals(child.ContentType.MediaType, MediaTypes.TextHtml)))
                {
                    message.AlternateViews.Add(CreateAlternateView(child));
                    SetMessageBody(message, child);

                } //add the alternative views.
                else if (string.Equals(child.ContentType.MediaType, MediaTypes.MessageRfc822, StringComparison.InvariantCultureIgnoreCase)
                    && string.Equals(child.ContentDisposition.DispositionType, DispositionTypeNames.Attachment, StringComparison.InvariantCultureIgnoreCase))
                {
                    message.Children.Add(ToMailMessageEx(child));
                } //create a child message and
                else if (IsAttachment(child))
                {
                    message.Attachments.Add(CreateAttachment(child));

                }
            }
        }
Beispiel #18
0
 private void AddChildEntity(MimeEntity entity, Queue<string> lines)
 {
     MimeReader reader = new MimeReader(entity, lines);
     entity.Children.Add(reader.CreateMimeEntity());
 }
Beispiel #19
0
 private void BuildSinglePartMessage(MimeEntity entity, MailMessageEx message)
 {
     this.SetMessageBody(message, entity);
 }