This class adds a few internet mail headers not already exposed by the System.Net.MailMessage. It also provides support to encapsulate the nested mail attachments in the Children collection.
Inheritance: System.Net.Mail.MailMessage
Esempio n. 1
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         = this.DecodeBytes(child.Content.ToArray(), encoding);
            message.BodyEncoding = encoding;
            message.IsBodyHtml   = string.Equals(
                MediaTypes.TextHtml, child.ContentType.MediaType, StringComparison.InvariantCultureIgnoreCase);
        }
Esempio n. 2
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 (string.Equals(
                        child.ContentType.MediaType, MediaTypes.MultipartAlternative, StringComparison.InvariantCultureIgnoreCase)
                    ||
                    string.Equals(
                        child.ContentType.MediaType, MediaTypes.MultipartMixed, StringComparison.InvariantCultureIgnoreCase)
                    ||
                    string.Equals(
                        child.ContentType.MediaType, MediaTypes.MultipartRelated, StringComparison.InvariantCultureIgnoreCase))
                {
                    this.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(this.CreateAlternateView(child));
                    this.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(this.ToMailMessageEx(child));
                }

                // create a child message and
                else if (IsAttachment(child))
                {
                    message.Attachments.Add(this.CreateAttachment(child));
                }
                else if (string.Equals(
                             entity.ContentType.MediaType, MediaTypes.MultipartRelated, StringComparison.InvariantCultureIgnoreCase)
                         ||
                         string.Equals(
                             entity.ContentType.MediaType, MediaTypes.MultipartMixed, StringComparison.InvariantCultureIgnoreCase))
                {
                    message.Attachments.Add(this.CreateAttachment(child));
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Creates the mail message from entity.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <returns>
        /// </returns>
        public static MailMessageEx CreateMailMessageFromEntity(MimeEntity entity)
        {
            var message = new MailMessageEx();

            foreach (string key in entity.Headers.AllKeys)
            {
                string value = entity.Headers[key];
                if (value.Equals(string.Empty))
                {
                    value = " ";
                }

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

                switch (key.ToLowerInvariant())
                {
                case MailHeaders.Bcc:
                    PopulateAddressList(value, message.Bcc);
                    break;

                case MailHeaders.Cc:
                    PopulateAddressList(value, message.CC);
                    break;

                case MailHeaders.From:
                    MailAddress fromAddress;
                    if (TryParseMailAddress(value, out fromAddress))
                    {
                        message.From = fromAddress;
                    }
                    break;

                case MailHeaders.ReplyTo:
                    PopulateAddressList(value, message.ReplyToList);
                    break;

                case MailHeaders.Subject:
                    message.Subject = DecodeIfEncoded(value);
                    break;

                case MailHeaders.To:
                    PopulateAddressList(value, message.To);
                    break;
                }
            }

            return(message);
        }
Esempio n. 4
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");
            }

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

            if (!string.IsNullOrEmpty(entity.ContentType.Boundary))
            {
                message = MailMessageEx.CreateMailMessageFromEntity(entity);
                this.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 Exception("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]);
                this.BuildMultiPartMessage(entity, message);
            }

            // parse nested message.
            else
            {
                message = MailMessageEx.CreateMailMessageFromEntity(entity);
                this.BuildSinglePartMessage(entity, message);
            }

            // Create single part message.
            return(message);
        }
Esempio n. 5
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)
 {
     this.SetMessageBody(message, entity);
 }
Esempio n. 6
0
        /// <summary>
        ///     Write the HTML to a temporary file and render it to the HTML view
        /// </summary>
        /// <param name="mailMessageEx">
        ///     The mail Message Ex.
        /// </param>
        private void SetBrowserDocument(MailMessageEx mailMessageEx)
        {
            const int Length = 256;

            // double d = new Random().NextDouble();
            string tempPath = Path.GetTempPath();
            string htmlFile = Path.Combine(tempPath, "papercut.htm");

            string htmlText = mailMessageEx.Body;

            foreach (var attachment in mailMessageEx.Attachments)
            {
                if ((!string.IsNullOrEmpty(attachment.ContentId)) && (attachment.ContentStream != null))
                {
                    string fileName = Path.Combine(tempPath, attachment.ContentId);

                    using (var fs = File.OpenWrite(fileName))
                    {
                        var buffer = new byte[Length];
                        int bytesRead = attachment.ContentStream.Read(buffer, 0, Length);

                        // write the required bytes
                        while (bytesRead > 0)
                        {
                            fs.Write(buffer, 0, bytesRead);
                            bytesRead = attachment.ContentStream.Read(buffer, 0, Length);
                        }

                        fs.Close();
                    }

                    htmlText =
                        htmlText.Replace(string.Format(@"cid:{0}", attachment.ContentId), attachment.ContentId)
                            .Replace(string.Format(@"cid:'{0}'", attachment.ContentId), attachment.ContentId)
                            .Replace(string.Format(@"cid:""{0}""", attachment.ContentId), attachment.ContentId);
                }
            }

            File.WriteAllText(htmlFile, htmlText, Encoding.Unicode);

            this.htmlView.Navigate(new Uri(htmlFile));
            this.htmlView.Refresh();

            this.defaultHtmlView.Navigate(new Uri(htmlFile));
            this.defaultHtmlView.Refresh();
        }