Example #1
0
 public MimePart()
 {
     ContentDisposition = new ContentDisposition();
     ContentType = new ContentType();
     BinaryContent = new byte[0];
     HeaderFields = new NameValueCollection();
     HeaderFieldNames = new NameValueCollection();
     SubParts = new MimePartCollection();
 }
Example #2
0
 /// <summary>
 /// Gets the type of the content.
 /// </summary>
 /// <param name="input">The input.</param>
 /// <returns></returns>
 private static ContentType GetContentType(string input)
 {
     ContentType field = new ContentType();
     field.MimeType = Regex.Match(input, @"(?<=: ?)\S+?(?=([;\s]|\Z))").Value;
     Match parammatch = Regex.Match(input, @"(?<=;\s+)[^;\s]*=[^;]*(?=(;|\Z))");
     while (parammatch.Success)
     {
         field.Parameters.Add(FormatFieldName(parammatch.Value.Substring(0, parammatch.Value.IndexOf('='))).ToLower(), parammatch.Value.Substring(parammatch.Value.IndexOf('=') + 1).Replace("\"", "").Trim('\r', '\n'));
         parammatch = parammatch.NextMatch();
     }
     return field;
 }
Example #3
0
        private static void DispatchPart(MimePart part, ref Message message)
        {
            if (part.SubParts.Count > 0) // This is a container part.
            {
                _parentContentType = part.ContentType; // saveing right content-type of message
                DispatchParts(part, ref message);
            }
            else // This is a leaf part.
            {
                // We will consider the highest-level text parts that are not attachments to be the intended for display.
                // We know the highest-level parts will be set, because the parser first goes to the deepest level and returns top-level parts last.
                if (part.ContentType.Type.ToLower().Equals("text") && !part.ContentDisposition.Disposition.ToLower().Equals("attachment"))
                {
                    if (part.ContentType.SubType.ToLower().Equals("plain"))
                    {
                        message.BodyText.Charset = part.Charset;
                        message.BodyText.Text = part.TextContent;
                    }
                    else if (part.ContentType.SubType.ToLower().Equals("html"))
                    {
                        message.IsHtml = true;
                        message.BodyHtml.Charset = part.Charset;
                        message.BodyHtml.Text = part.TextContent;
                    }

                    if (_parentContentType != null)
                        message.ContentType = _parentContentType;
                }
                // If this part has to be displayed has an attachment, add it to the appropriate collection.
                else if (part.ContentDisposition.Disposition.ToLower().Equals("attachment"))
                {
                    message.Attachments.Add(part);
                }
                // If this part has to be displayed at the same time as the main body, add it to the appropriate collection.
                else if (part.ContentDisposition.Disposition.ToLower().Equals("inline"))
                {
                    message.EmbeddedObjects.Add(part);
                }
                // If we have image isn't marked as "Content-Disposition: inline", we will add it to EmbededObjects
                else if (part.ContentType.Type.ToLower().Equals("image"))
                {
                    message.EmbeddedObjects.Add(part);
                }

                // Parse message/rfc822 parts as Message objects and place them in the appropriate collection.
                else if (part.ContentType.MimeType.ToLower().Equals("message/rfc822"))
                {
                    var msg = part.TextContent;
                    message.SubMessages.Add(ParseMessage(ref msg)); //.BinaryContent));
                }

                else if (part.ContentType.MimeType.ToLower().Equals("application/pkcs7-signature")
                    || part.ContentType.MimeType.ToLower().Equals("application/x-pkcs7-signature"))
                {
                    var to_digest = part.Container.TextContent;
                    to_digest = Regex.Split(to_digest, "\r\n--" + part.Container.ContentType.Parameters["boundary"])[1];
                    to_digest = to_digest.TrimStart('\r', '\n');
                    //Match endDelimiter = Regex.Match(toDigest, "(?<=[^\r\n]\r\n)\r\n", RegexOptions.RightToLeft);
                    //int lastNonNewLine = Regex.Match(toDigest, "[^\r\n]", RegexOptions.RightToLeft).Index;
                    //if (endDelimiter.Index != -1 && endDelimiter.Index > lastNonNewLine) toDigest = toDigest.Remove(endDelimiter.Index);

                    //TODO: What should be done in PPC ?
#if !PocketPC
                    message.Signatures.Smime = new SignedCms(new ContentInfo(Encoding.ASCII.GetBytes(to_digest)), true);
                    message.Signatures.Smime.Decode(part.BinaryContent);
#endif
                }
                else if (message.IsMultipartReport && part.ContentType.MimeType.ToLower().Equals("message/delivery-status"))
                {
                    message.BodyText.Text += String.Format("\r\nDelivery status:\r\n{0}", part.TextContent);
                }
                else
                {
                    message.UnknownDispositionMimeParts.Add(part);
                }

                // Anyway, this is a leaf part of the message.
                message.LeafMimeParts.Add(part);
            }
        }
Example #4
0
        /// <summary>
        /// Gets the type of the content.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns></returns>
        public static ContentType GetContentType(string input)
        {
            var field = new ContentType {MimeType = _regxMimeType.Match(input).Value};
            var parammatch = _regxMimeTypeParams.Match(input);

            while (parammatch.Success)
            {
                field.Parameters.Add(
                    FormatFieldName(
                        parammatch.Value.Substring(0, parammatch.Value.IndexOf('='))).ToLower(),
                    parammatch.Value.Substring(parammatch.Value.IndexOf('=') + 1).Replace("\"", "").Trim('\r', '\n'));

                parammatch = parammatch.NextMatch();
            }
            return field;
        }
Example #5
0
        /// <summary>
        /// The MIME representation of the header.
        /// </summary>
        /// <param name="removeBlindCopies">if set to <c>true</c> remove blind copies (BCC).</param>
        /// <param name="forceBase64Encoding">if set to <c>true</c> forces inner elements to be base64 encoded</param>
        /// <returns></returns>
        public string ToHeaderString(bool removeBlindCopies, bool forceBase64Encoding = false)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            /*foreach (TraceInfo trace in this.Trace) sb.AppendLine("Received: " + trace.ToString());
             * if (!this.From.Email.Equals(string.Empty)) sb.AppendLine("From: " + this.From.Merged);
             * if (!this.Sender.Email.Equals(string.Empty)) sb.AppendLine("Sender: " + this.Sender.Merged);
             * if (this.To.Count > 0) sb.AppendLine("To: " + this.To.Merged);
             * if (this.Cc.Count > 0) sb.AppendLine("Cc: " + this.Cc.Merged);
             * if (this.Bcc.Count > 0) sb.AppendLine("Bcc: " + this.Bcc.Merged);
             * if (!this.ReplyTo.Email.Equals(string.Empty)) sb.AppendLine("Reply-to: " + this.ReplyTo.Merged);*/

            foreach (TraceInfo trace in Trace)
            {
                AddHeaderField("Received", trace.ToString());
            }
            if (!From.Email.Equals(string.Empty))
            {
                AddHeaderField("From", From.Merged);
            }
            if (!Sender.Email.Equals(string.Empty))
            {
                AddHeaderField("Sender", Sender.Merged);
            }
            if (To.Count > 0)
            {
                AddHeaderField("To", To.Merged);
            }
            if (Cc.Count > 0)
            {
                AddHeaderField("Cc", Cc.Merged);
            }
            if (Bcc.Count > 0 && !removeBlindCopies)
            {
                AddHeaderField("Bcc", Bcc.Merged);
            }
            if (!ReplyTo.Email.Equals(string.Empty))
            {
                AddHeaderField("Reply-To", ReplyTo.Merged);
            }

            if (Date.Equals(DateTime.MinValue))
            {
                Date = DateTime.Now;
            }

            if (string.IsNullOrEmpty(MessageId))
            {
                MessageId = "<AU" + Codec.GetUniqueString() + "@" + System.Net.Dns.GetHostName() + ">";
            }

            if (ContentType.MimeType.Length > 0)
            {
                string contentType = ContentType.ToString();
                contentType = contentType.Substring(contentType.IndexOf(":") + 1).TrimStart(' ');
                AddHeaderField("Content-Type", contentType);
            }

            if (ContentDisposition.Disposition.Length > 0)
            {
                string contentDisposition = ContentDisposition.ToString();
                contentDisposition = contentDisposition.Substring(contentDisposition.IndexOf(":") + 1).TrimStart(' ');
                AddHeaderField("Content-Disposition", contentDisposition);
            }

            if (forceBase64Encoding)
            {
                AddHeaderField("Content-Transfer-Encoding", "base64");
            }
            else if (ContentType.Type.Equals("text"))
            {
                AddHeaderField("Content-Transfer-Encoding", "quoted-printable");
            }

            System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
            Version v = asm.GetName().Version;

            AddHeaderField("X-Mailer", "ActiveUp.MailSystem " + v.Major + "." + v.Minor + "." + v.Build + " www.activeup.com");

            foreach (string key in HeaderFields.AllKeys)
            {
                for (int i = 0; i < HeaderFields.GetValues(key).Length; i++)
                {
                    sb.Append(HeaderFieldNames.GetValues(key)[i] + ": " + HeaderFields.GetValues(key)[i] + "\r\n");
                }
            }
            return(sb.ToString().TrimEnd('\r', '\n'));
        }