예제 #1
0
        public ContentType ParseContentType(string text)
        {
            ContentType type;

            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            if (!this.IsValidCharacters(text))
            {
                type = new ContentType();
                type.Attributes.Add("charset", "us-ascii");
                return(type);
            }
            Match match = regContentType.Match(text);

            if (match.Success && (match.Groups.Count > 2))
            {
                type = new ContentType(match.Groups[1].Value, match.Groups[2].Value);
                MatchCollection matchs = regKeyValue.Matches(match.Value);
                for (int i = 0; i < matchs.Count; i++)
                {
                    DictionaryExtenders.SmartAdd(type.Attributes, matchs[i].Groups[1].Value.ToLower().Trim(), matchs[i].Groups[2].Value.Trim());
                }
                if ((type.Type == "text") && !type.Attributes.ContainsKey("charset"))
                {
                    type.Attributes.Add("charset", "us-ascii");
                }
                return(type);
            }
            type = new ContentType();
            type.Attributes.Add("charset", "us-ascii");
            return(type);
        }
예제 #2
0
        public ContentDisposition ParseContentDisposition(string text)
        {
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }
            ContentDisposition disposition = new ContentDisposition();
            Match match = regContentDisposition.Match(text);

            if (!match.Success || (match.Groups.Count <= 0))
            {
                return(null);
            }
            MatchCollection matchs = regKeyValue.Matches(match.Value);

            for (int i = 0; i < matchs.Count; i++)
            {
                string key    = matchs[i].Groups[1].Value.ToLower();
                string input  = matchs[i].Groups[2].Value;
                Match  match2 = regDifArgumentValue.Match(input);
                if (match2.Success && (match2.Groups.Count > 1))
                {
                    input = match2.Groups[1].Value;
                }
                DictionaryExtenders.SmartAdd(disposition.Attributes, key, input);
            }
            disposition.Disposition = match.Groups[1].Value;
            return(disposition);
        }
예제 #3
0
        public MIMEHeader Parse(string source)
        {
            string       str;
            MIMEHeader   header = new MIMEHeader();
            Regex        regex  = new Regex("\r\n[ \\t]+");
            StringReader reader = new StringReader(regex.Replace(source, " "));

            header.ContentType = new ContentType();
            while ((str = reader.ReadLine()) != null)
            {
                Match match = MailMessageRFCDecoder.regArgument.Match(str);
                if (match.Success)
                {
                    switch (match.Groups[1].Value.ToLower())
                    {
                    case "mime-version":
                    {
                        Version version = this.ParseMimeVersion(str);
                        if ((version.Major != 1) || (version.Minor != 0))
                        {
                            throw new Exception("MIME version is not 1.0");
                        }
                        continue;
                    }

                    case "date":
                    {
                        header.Date = this.ParseDate(str).ToLocalTime();
                        continue;
                    }

                    case "from":
                    {
                        header.From = this.ParseFrom(str);
                        continue;
                    }

                    case "sender":
                    {
                        header.Sender = this.ParseSender(str);
                        continue;
                    }

                    case "reply-to":
                    {
                        header.ReplyTo = this.ParseReplyTo(str);
                        continue;
                    }

                    case "to":
                    {
                        header.To = this.ParseTo(str);
                        continue;
                    }

                    case "cc":
                    {
                        header.CarbonCopies = this.ParseCarbonCopies(str);
                        continue;
                    }

                    case "bcc":
                    {
                        header.BlindedCarbonCopies = this.ParseBlindCarbonCopies(str);
                        continue;
                    }

                    case "message-id":
                    {
                        header.MessageID = this.ParseMessageID(str);
                        continue;
                    }

                    case "in-reply-to":
                    {
                        header.InReplyTo = this.ParseInReplyTo(str);
                        continue;
                    }

                    case "references":
                    {
                        header.References = this.ParseReferences(str);
                        continue;
                    }

                    case "subject":
                    {
                        header.Subject = this.ParseSubject(str);
                        continue;
                    }

                    case "comments":
                    {
                        header.Comments = this.ParseComments(str);
                        continue;
                    }

                    case "keywords":
                    {
                        header.Keywords = this.ParseKeywords(str);
                        continue;
                    }

                    case "content-disposition":
                    {
                        header.ContentDisposition = this.ParseContentDisposition(str);
                        continue;
                    }

                    case "content-id":
                    {
                        header.ContentID = this.ParseContentID(str);
                        continue;
                    }

                    case "resent-date":
                    {
                        header.ResentDate = this.ParseDate(str);
                        continue;
                    }

                    case "resent-from":
                    {
                        header.ResentFrom = this.ParseFrom(str);
                        continue;
                    }

                    case "resent-sender":
                    {
                        header.ResentSender = this.ParseSender(str);
                        continue;
                    }

                    case "resent-to":
                    {
                        header.ResentTo = this.ParseTo(str);
                        continue;
                    }

                    case "resent-cc":
                    {
                        header.ResentCarbonCopies = this.ParseCarbonCopies(str);
                        continue;
                    }

                    case "resent-bcc":
                    {
                        header.ResentBlindedCarbonCopies = this.ParseBlindCarbonCopies(str);
                        continue;
                    }

                    case "resent-message-id":
                    {
                        header.ResentMessageID = this.ParseMessageID(str);
                        continue;
                    }

                    case "return-path":
                    {
                        header.ReturnPath = this.ParseReturnPatch(str);
                        continue;
                    }

                    case "received":
                    {
                        if (string.IsNullOrEmpty(header.Received))
                        {
                            header.Received = str.Substring(9).Trim();
                        }
                        continue;
                    }

                    case "content-type":
                    {
                        header.ContentType = this.ParseContentType(str);
                        continue;
                    }

                    case "content-transfer-encoding":
                    {
                        header.ContentTransferEncoding = this.ParseContentTransferEncoding(str);
                        continue;
                    }

                    case "content-description":
                    {
                        header.ContentDescription = this.ParseContentDescription(str);
                        continue;
                    }

                    case "importance":
                    {
                        header.Importance = this.ParseImportance(str);
                        continue;
                    }
                    }
                    DictionaryExtenders.SmartAdd(header.ExtraHeaders, match.Groups[1].Value, match.Groups[2].Value);
                }
            }
            return(header);
        }