Example #1
0
        internal static Bodypart BodypartFromMIME(MIMEPart mimePart)
        {
            NameValueCollection contentType = ParseMIMEField(mimePart.Header["Content-Type"]);
            Bodypart p = new Bodypart(null);
            Match m = Regex.Match(contentType["value"], "(.+)/(.+)");
            if (m.Success)
            {
                p.ContentType = ContentTypeMap.FromString(m.Groups[1].Value);
                p.Subtype = m.Groups[2].Value;
            }

            p.Encoding = ContentTransferEncodingMap.FromString(mimePart.Header["Content-Transfer-Encoding"]);
            p.Id = mimePart.Header["Content-Id"];
            foreach (string k in contentType.AllKeys)
            {
                p.Parameters.Add(k, contentType[k]);
            }

            p.Size = mimePart.Body.Length;
            if (mimePart.Header["Content-Disposition"] != null)
            {
                NameValueCollection disposition = ParseMIMEField(mimePart.Header["Content-Disposition"]);
                p.Disposition.ContentType = ContentDispositionTypeMap.FromString(disposition["value"]);
                p.Disposition.FileName = disposition["FileName"];
                foreach (string k in disposition.AllKeys)
                {
                    p.Disposition.Attributes.Add(k, disposition[k]);
                }
            }

            return p;
        }
Example #2
0
        private Bodypart ParseBodypart(string partNumber, bool parenthesis = true)
        {
            Bodypart part = new Bodypart(partNumber);

            part.ContentType = ContentTypeMap.FromString(reader.ReadWord());
            part.Subtype = reader.ReadWord();
            part.Parameters = reader.ReadList();
            part.Id = reader.ReadWord();
            part.Description = reader.ReadWord();
            part.Encoding = ContentTransferEncodingMap.FromString(reader.ReadWord());
            part.Size = reader.ReadInteger();
            if (part.ContentType == ContentType.Text)
            {
                part.Lines = reader.ReadInteger();
            }

            if (part.ContentType == ContentType.Message && part.Subtype.ToUpper() == "RFC822")
            {
                ParseMessage822Fields(part);
            }

            try
            {
                ParseOptionalFields(part, parenthesis);
            }
            catch (EndOfStringException)
            {
            }

            return part;
        }
Example #3
0
        internal static void AddBodypart(this MailMessage message, Bodypart part, string content)
        {
            Encoding encoding = part.Parameters.ContainsKey("Charset") ? Util.GetEncoding(part.Parameters["Charset"]) : Encoding.ASCII;

            byte[] bytes;
            try
            {
                switch (part.Encoding)
                {
                    case ContentTransferEncoding.QuotedPrintable:
                        bytes = encoding.GetBytes(Util.QPDecode(content, encoding));
                        break;
                    case ContentTransferEncoding.Base64:
                        bytes = Util.Base64Decode(content);
                        break;
                    default:
                        bytes = Encoding.ASCII.GetBytes(content);
                        break;
                }
            }
            catch
            {
                bytes = Encoding.ASCII.GetBytes(content);
            }

            bool hasName = part.Parameters.ContainsKey("name");

            if (string.IsNullOrEmpty(message.Body) && part.Disposition.ContentType != ContentDispositionType.Attachment)
            {
                message.Body = encoding.GetString(bytes);
                message.BodyEncoding = encoding;
                message.IsBodyHtml = part.Subtype.ToLower() == "html";
                return;
            }

            string contentType = ParseMIMEField(message.Headers["Content-Type"])["value"];
            bool preferAlternative = string.Compare(contentType, "multipart/alternative", true) == 0;

            if (part.Disposition.ContentType == ContentDispositionType.Attachment || (part.Disposition.ContentType == ContentDispositionType.Unknown && preferAlternative == false && hasName))
            {
                message.Attachments.Add(CreateAttachment(part, bytes));
            }
            else
            {
                message.AlternateViews.Add(CreateAlternateView(part, bytes));
            }
        }
Example #4
0
        internal static AlternateView CreateAlternateView(Bodypart part, byte[] bytes)
        {
            MemoryStream stream = new MemoryStream(bytes);
            System.Net.Mime.ContentType contentType;
            try
            {
                contentType = new System.Net.Mime.ContentType(part.ContentType.ToString().ToLower() + "/" + part.Subtype.ToLower());
            }
            catch
            {
                contentType = new System.Net.Mime.ContentType();
            }

            AlternateView view = new AlternateView(stream, contentType);
            try
            {
                view.ContentId = ParseMessageId(part.Id);
            }
            catch
            {
            }

            return view;
        }
Example #5
0
        internal static Attachment CreateAttachment(Bodypart part, byte[] bytes)
        {
            MemoryStream stream = new MemoryStream(bytes);
            string name = part.Disposition.FileName;

            if (string.IsNullOrEmpty(name) && part.Parameters.ContainsKey("name"))
            {
                name = part.Parameters["name"];
            }

            if (string.IsNullOrEmpty(name))
            {
                name = Path.GetRandomFileName();
            }

            Attachment attachment = new Attachment(stream, name);
            try
            {
                attachment.ContentId = ParseMessageId(part.Id);
            }
            catch
            {
            }

            try
            {
                attachment.ContentType = new System.Net.Mime.ContentType(part.ContentType.ToString().ToLower() + "/" + part.Subtype.ToLower());
            }
            catch
            {
                attachment.ContentType = new System.Net.Mime.ContentType();
            }

            attachment.Name = name;
            attachment.ContentDisposition.FileName = name;
            return attachment;
        }
Example #6
0
        private void ParseOptionalFields(Bodypart part, bool parenthesis = true)
        {
            if (parenthesis && reader.Peek(true) == ')')
            {
                reader.Read();
                return;
            }

            part.MD5 = reader.ReadWord();
            if (parenthesis && reader.Peek(true) == ')')
            {
                reader.Read();
                return;
            }

            part.Disposition = reader.ReadDisposition();
            if (parenthesis && reader.Peek(true) == ')')
            {
                reader.Read();
                return;
            }

            part.Language = reader.ReadWord();
            if (parenthesis && reader.Peek(true) == ')')
            {
                reader.Read();
                return;
            }

            part.Location = reader.ReadWord();
            if (parenthesis)
            {
                reader.SkipUntil(')');
            }
        }
Example #7
0
 private void ParseMessage822Fields(Bodypart part)
 {
     SkipParenthesizedExpression();
     SkipParenthesizedExpression();
     part.Lines = reader.ReadInteger();
 }