示例#1
0
        public MailMessageEx Top(int messageId, int lineCount)
        {
            if (messageId < 1)
            {
                throw new ArgumentOutOfRangeException("messageId");
            }

            if (lineCount < 0)
            {
                throw new ArgumentOutOfRangeException("lineCount");
            }

            RetrResponse response;

            using (TopCommand command = new TopCommand(_clientStream, messageId, lineCount))
            {
                response = ExecuteCommand <RetrResponse, TopCommand>(command);
            }

            MimeReader    reader  = new MimeReader(response.MessageLines);
            MimeEntity    entity  = reader.CreateMimeEntity();
            MailMessageEx message = entity.ToMailMessageEx();

            message.Octets        = response.Octets;
            message.MessageNumber = messageId;
            return(entity.ToMailMessageEx());
        }
 /// <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));
         }
     }
 }
        /// <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);
        }
示例#4
0
        /// <summary>
        /// Retrs the mail message ex.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <returns></returns>
        public MailMessageEx RetrMailMessageEx(Pop3ListItem item)
        {
            MailMessageEx message = RetrMimeEntity(item).ToMailMessageEx();

            if (message != null)
            {
                message.MessageNumber = item.MessageId;
            }
            return(message);
        }
示例#5
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);
        }
        /// <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);
        }
 public static List <MailMessageEx> Receive(string hostname, int port, bool useSsl, string username, string password)
 {
     using (Pop3Client client = new Pop3Client(hostname, port, useSsl, username, password))
     {
         client.Trace += new Action <string>(Console.WriteLine);
         client.Authenticate();
         client.Stat();
         List <MailMessageEx> maillist = new List <MailMessageEx>();
         MailMessageEx        message  = null;
         foreach (Pop3ListItem item in client.List())
         {
             message = client.RetrMailMessageEx(item);
             if (message != null)
             {
                 client.Dele(item);
                 maillist.Add(message);
             }
         }
         client.Noop();
         client.Rset();
         client.Quit();
         return(maillist);
     }
 }
 /// <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);
 }