Ejemplo n.º 1
0
        public static void ParseContentType(ContentType contentType, String line)
        {
            Match m = null;


            foreach (Regex rx in MailParser.RegexList.ContentTypeName)
            {
                m = rx.Match(line);
                if (String.IsNullOrEmpty(m.Groups["Value"].Value) == false)
                {
                    contentType.Name = m.Groups["Value"].Value;
                    break;
                }
            }
            if (String.IsNullOrEmpty(contentType.Name) == true)
            {
                contentType.Name = MailParser.ParseHeaderParameterValue("name", line);
            }


            foreach (Regex rx in MailParser.RegexList.ContentTypeBoundary)
            {
                m = rx.Match(line);
                if (String.IsNullOrEmpty(m.Groups["Value"].Value) == false)
                {
                    contentType.Boundary = m.Groups["Value"].Value;
                    break;
                }
            }
            if (String.IsNullOrEmpty(contentType.Boundary) == true)
            {
                contentType.Boundary = MailParser.ParseHeaderParameterValue("boundary", line);
            }
        }
Ejemplo n.º 2
0
        public static String DecodeFromMailHeaderLine(String line)
        {
            Regex           rg = RegexList.DecodeByRfc2047;
            MatchCollection mc = null;
            Match           m  = null;

            Byte[]        bb         = null;
            Encoding      en         = null;
            Int32         StartIndex = 0;
            StringBuilder sb         = new StringBuilder();

            if (String.IsNullOrEmpty(line) == true)
            {
                return("");
            }

            m  = RegexList.DecodeByRfc2231.Match(line);
            mc = rg.Matches(line);
            if (m.Success == true && mc.Count == 0)
            {
                en = MailParser.GetEncoding(m.Groups["Encoding"].Value);
                sb.Append(MailParser.DecodeFromMailHeaderLineByRfc2231(m.Groups["Value"].Value, en));
            }
            else
            {
                for (int i = 0; i < mc.Count; i++)
                {
                    m = mc[i];
                    sb.Append(line.Substring(StartIndex, m.Index - StartIndex));
                    StartIndex = m.Index + m.Length;

                    if (m.Groups.Count < 3)
                    {
                        throw new InvalidDataException();
                    }
                    if (m.Groups["BorQ"].Value.ToUpper() == "B")
                    {
                        bb = Convert.FromBase64String(m.Groups["Value"].Value);
                    }
                    else if (m.Groups["BorQ"].Value.ToUpper() == "Q")
                    {
                        bb = MailParser.FromQuotedPrintableTextOnHeader(m.Groups["Value"].Value);
                    }
                    else
                    {
                        throw new InvalidDataException();
                    }
                    en = MailParser.GetEncoding(m.Groups["Encoding"].Value);
                    sb.Append(en.GetString(bb));
                }
                sb.Append(line.Substring(StartIndex, line.Length - StartIndex));
            }
            return(sb.ToString());
        }
Ejemplo n.º 3
0
 public static String EncodeToMailBody(String text, TransferEncoding encodeType, Encoding encoding)
 {
     Byte[] bb = encoding.GetBytes(text);
     if (encodeType == TransferEncoding.Base64)
     {
         return(Convert.ToBase64String(bb));
     }
     else if (encodeType == TransferEncoding.QuotedPrintable)
     {
         return(MailParser.ToQuotedPrintable(encoding.GetString(bb)));
     }
     return(encoding.GetString(bb));
 }
Ejemplo n.º 4
0
        public static void ParseContentDisposition(ContentDisposition contentDisposition, String line)
        {
            Match m = null;


            foreach (Regex rx in MailParser.RegexList.ContentDispositionFileName)
            {
                m = rx.Match(line);
                if (String.IsNullOrEmpty(m.Groups["Value"].Value) == false)
                {
                    contentDisposition.FileName = m.Groups["Value"].Value;
                    return;
                }
            }
            contentDisposition.FileName = MailParser.ParseHeaderParameterValue("filename", line);
        }
Ejemplo n.º 5
0
        public static String DecodeFromMailBody(String text, TransferEncoding encodeType, Encoding encoding)
        {
            Byte[] b = null;

            if (encodeType == TransferEncoding.Base64)
            {
                b = Convert.FromBase64String(text);
            }
            else if (encodeType == TransferEncoding.QuotedPrintable)
            {
                b = MailParser.FromQuotedPrintableText(text);
            }
            else
            {
                b = encoding.GetBytes(text);
            }
            return(encoding.GetString(b));
        }
Ejemplo n.º 6
0
 public static String ToMailAddressText(Encoding encoding, TransferEncoding transferEncoding
                                        , String mailAddress, String displayName, Boolean doubleQuote)
 {
     if (String.IsNullOrEmpty(displayName) == true)
     {
         return(mailAddress);
     }
     else
     {
         if (doubleQuote == true)
         {
             return(String.Format("\"{0}\" <{1}>", displayName, mailAddress));
         }
         else
         {
             return(String.Format("{0} <{1}>"
                                  , MailParser.EncodeToMailHeaderLine(displayName, transferEncoding, encoding, MailParser.MaxCharCountPerRow - mailAddress.Length - 3)
                                  , mailAddress));
         }
     }
 }
Ejemplo n.º 7
0
 static MailParser()
 {
     MailParser.SetDateTimeFormatString();
 }
Ejemplo n.º 8
0
        public static String EncodeToMailHeaderLine(String text, TransferEncoding encodeType, Encoding encoding, Int32 maxCharCount)
        {
            Byte[]        bb              = null;
            StringBuilder sb              = new StringBuilder();
            Int32         StartIndex      = 0;
            Int32         CharCountPerRow = 0;
            Int32         ByteCount       = 0;

            if (maxCharCount > MailParser.MaxCharCountPerRow)
            {
                throw new ArgumentException("maxCharCount must less than MailParser.MaxCharCountPerRow.");
            }

            if (String.IsNullOrEmpty(text) == true)
            {
                return("");
            }

            if (MailParser.AsciiCharOnly(text) == true)
            {
                StartIndex      = 0;
                CharCountPerRow = maxCharCount;
                for (int i = 0; i < text.Length; i++)
                {
                    sb.Append(text[i]);
                    if (StartIndex == CharCountPerRow)
                    {
                        sb.Append(MailParser.NewLine);
                        StartIndex      = 0;
                        CharCountPerRow = MailParser.MaxCharCountPerRow;
                        if (i < text.Length - 1)
                        {
                            sb.Append("\t");
                        }
                    }
                    else
                    {
                        StartIndex += 1;
                    }
                }
                return(sb.ToString());
            }
            if (encodeType == TransferEncoding.Base64)
            {
                CharCountPerRow = (Int32)Math.Floor((maxCharCount - (encoding.WebName.Length + 10)) * 0.75);
                for (int i = 0; i < text.Length; i++)
                {
                    ByteCount = encoding.GetByteCount(text.Substring(StartIndex, (i + 1) - StartIndex));
                    if (ByteCount > CharCountPerRow)
                    {
                        bb = encoding.GetBytes(text.Substring(StartIndex, i - StartIndex));
                        sb.AppendFormat("=?{0}?B?{1}?={2}\t", encoding.WebName, Convert.ToBase64String(bb), MailParser.NewLine);
                        StartIndex      = i;
                        CharCountPerRow = (Int32)Math.Floor((MailParser.MaxCharCountPerRow - (encoding.WebName.Length + 10)) * 0.75);
                    }
                }
                bb = encoding.GetBytes(text.Substring(StartIndex));
                sb.AppendFormat("=?{0}?B?{1}?=", encoding.WebName, Convert.ToBase64String(bb));

                return(sb.ToString());
            }
            else if (encodeType == TransferEncoding.QuotedPrintable)
            {
                CharCountPerRow = (Int32)Math.Floor((maxCharCount - (Double)(encoding.WebName.Length + 10)) / 3);
                for (int i = 0; i < text.Length; i++)
                {
                    ByteCount = encoding.GetByteCount(text.Substring(StartIndex, (i + 1) - StartIndex));
                    if (ByteCount > CharCountPerRow)
                    {
                        bb = encoding.GetBytes(text.Substring(StartIndex, i - StartIndex));
                        sb.AppendFormat("=?{0}?Q?{1}?={2}\t", encoding.WebName, MailParser.ToQuotedPrintableOnHeader(Encoding.ASCII.GetString(bb)), MailParser.NewLine);
                        StartIndex      = i;
                        CharCountPerRow = (Int32)Math.Floor((MailParser.MaxCharCountPerRow - (encoding.WebName.Length + 10)) * 0.75);
                    }
                }
                bb = encoding.GetBytes(text.Substring(StartIndex));
                sb.AppendFormat("=?{0}?Q?{1}?=", encoding.WebName, MailParser.ToQuotedPrintable(Encoding.ASCII.GetString(bb)));

                return(sb.ToString());
            }
            else
            {
                return(text);
            }
        }
Ejemplo n.º 9
0
 public static String EncodeToMailHeaderLine(String text, TransferEncoding encodeType, Encoding encoding)
 {
     return(MailParser.EncodeToMailHeaderLine(text, encodeType, encoding, MailParser.MaxCharCountPerRow));
 }