Example #1
0
        //解析邮件正文,附件
        private IList <HMailContentInfo> GetMailContent(string mailString, ref IList <HMailAttachmentInfo> mentInfo)
        {
            IList <HMailContentInfo> mailContents = new List <HMailContentInfo>();
            string mixedMark   = GetMailStringMark(mailString, "Content-Type: multipart/mixed;");
            string matchString = string.Empty;

            string[] strArr = null;
            if (!string.IsNullOrEmpty(mixedMark))
            {
                matchString = Regex.Match(mailString, string.Format("(?<=\r\n{0}[^\"])[\\s\\S]*(?=[^\"]{0}{1}{1})", mixedMark, "-")).Value;
                string[] matchStrArr = Regex.Split(matchString, mixedMark);

                foreach (string str in matchStrArr)
                {
                    matchString = Regex.Match(str, "[\\s\\S]*(?=\n)").Value;
                    mixedMark   = GetMailStringMark(matchString, "Content-Type: multipart/alternative;");
                    if (string.IsNullOrEmpty(mixedMark))
                    {
                        mixedMark = GetMailStringMark(matchString, "");
                    }
                    if (!string.IsNullOrEmpty(mixedMark))
                    {
                        strArr = Regex.Split(matchString, mixedMark);
                        HMailContentInfo tmpContent = null;
                        foreach (string tmpStr in strArr)
                        {
                            tmpContent = DecodeContent(tmpStr);
                            if (string.IsNullOrEmpty(tmpContent.ContentType) || string.IsNullOrEmpty(tmpContent.Encoding))
                            {
                                continue;
                            }
                            mailContents.Add(DecodeContent(tmpStr));
                        }
                    }
                    else if (matchString.Contains("Content-Disposition: attachment;"))
                    {
                        mentInfo.Add(GetAttachment(matchString));
                    }
                    else
                    {
                        mailContents.Add(DecodeContent(matchString));
                    }
                }
            }
            else
            {
                RecursiveContent(mailContents, mentInfo, mailString);
            }
            return(mailContents);
        }
Example #2
0
        private void RecursiveContent(IList <HMailContentInfo> mailContents, IList <HMailAttachmentInfo> mentInfo, string mailString)
        {
            string matchString = string.Empty;
            string mixedMark   = string.Empty;

            string[] strArr = null;
            matchString = Regex.Match(mailString, "[\\s\\S]*(?=\n)").Value;
            mixedMark   = GetMailStringMark(matchString, "Content-Type: multipart/alternative;");
            if (string.IsNullOrEmpty(mixedMark))
            {
                mixedMark = GetMailStringMark(matchString, "");
            }
            if (!string.IsNullOrEmpty(mixedMark))
            {
                strArr = Regex.Split(matchString, mixedMark);
                HMailContentInfo tmpContent = null;
                foreach (string tmpStr in strArr)
                {
                    tmpContent = DecodeContent(tmpStr);
                    if (string.IsNullOrEmpty(tmpContent.ContentType) || string.IsNullOrEmpty(tmpContent.Encoding))
                    {
                        continue;
                    }
                    mixedMark = GetMailStringMark(tmpStr, "Content-Type: multipart/alternative;");
                    if (string.IsNullOrEmpty(mixedMark))
                    {
                        mixedMark = GetMailStringMark(tmpStr, "");
                    }
                    if (!string.IsNullOrEmpty(mixedMark))
                    {
                        RecursiveContent(mailContents, mentInfo, tmpStr);
                    }
                    else
                    {
                        mailContents.Add(DecodeContent(tmpStr));
                    }
                }
            }
            else if (matchString.Contains("Content-Disposition: attachment;"))
            {
                mentInfo.Add(GetAttachment(matchString));
            }
            else
            {
                mailContents.Add(DecodeContent(matchString));
            }
        }
Example #3
0
        private HMailContentInfo DecodeContent(string mailString)
        {
            HMailContentInfo mailContentInfo = new HMailContentInfo();

            try
            {
                if (mailString.Contains("Content-Type: Message/Rfc822"))
                {
                    mailContentInfo.Content = "";
                    return(mailContentInfo);
                }
                mailContentInfo.ContentType = Regex.Match(mailString, "(?<=Content-Type:)[\\s\\S]*?(?=\r\n)").Value.Trim().Trim(new char[] { ';' }).ToLower();
                mailContentInfo.Charset     = Regex.Match(mailString, "(?<=charset)[\\s\\S]*?(?=\r\n)").Value.Replace('=', ' ').Trim().Trim(new char[] { '\"' }).ToLower();
                if (mailContentInfo.Charset.Contains("charset"))
                {
                    mailContentInfo.Charset = Regex.Split(mailContentInfo.Charset, "charset")[1].Trim().Trim(new char[] { '\"' }).ToLower();
                }
                if (mailContentInfo.Charset.ToUpper() == "SO-8859-1")
                {
                    mailContentInfo.Charset = "ISO-8859-1";
                }
                mailContentInfo.Encoding = Regex.Match(mailString, "(?<=Content-Transfer-Encoding:)[\\s\\S]*?(?=\r\n)").Value.Trim().ToLower();
                if (!string.IsNullOrEmpty(mailContentInfo.ContentType) && !string.IsNullOrEmpty(mailContentInfo.Charset) && (mailContentInfo.ContentType.Contains("text/plain") || mailContentInfo.ContentType.Contains("text/html")) && (mailContentInfo.Encoding == "quoted-printable" || mailContentInfo.Encoding == "base64"))
                {
                    //string sssss = Regex.Replace(mailString, "[\\s\\S]*(?=<html>)", "");
                    if (mailContentInfo.Encoding == "quoted-printable")
                    {
                        mailString = Regex.Replace(mailString, "Content-Type:[\\s\\S]*?\r\n", "");
                        mailString = Regex.Replace(mailString, "charset=[\\s\\S]*?\r\n", "");
                        mailString = Regex.Replace(mailString, "Content-Transfer-Encoding:[\\s\\S]*?\r\n", "");
                        if (Regex.IsMatch(mailString, "[\\s\\S]+\r\n\r\n[\\s\\S]*"))
                        {
                            mailString = Regex.Match(mailString, "\r\n\r\n[\\s\\S]*").Value;
                        }
                        mailContentInfo.Content = DecodeQP(Regex.Replace(mailString, "[\\s\\S]*(?=<html>)", ""), mailContentInfo.Charset);
                    }
                    else
                    {
                        string _partten   = "((?=X-CM-TRANSID)|(?=X-Coremail-Antispam)|(?=Message-Id)|(?=Date))";
                        string _tmpString = Regex.Match(mailString, "(?<=base64)[\\s\\S]*(?=\n)").Value;
                        if (_tmpString.Contains("X-CM-TRANSID"))
                        {
                            _tmpString = Regex.Replace(_tmpString, string.Format("X-CM-TRANSID[\\s\\S]*{0}", _partten), "");
                        }
                        if (_tmpString.Contains("X-Coremail-Antispam"))
                        {
                            _tmpString = Regex.Replace(_tmpString, string.Format("X-Coremail-Antispam[\\s\\S]*{0}", _partten), "");
                        }
                        if (_tmpString.Contains("Message-Id") || _tmpString.Contains("Message-ID"))
                        {
                            _tmpString = Regex.Replace(_tmpString, string.Format("((Message-Id)|(Message-ID))[\\s\\S]*{0}", _partten), "");
                        }
                        if (_tmpString.Contains("Date") || _tmpString.Contains("date"))
                        {
                            _tmpString = Regex.Replace(_tmpString, "((Date)|(date))[\\s\\S]*?\r\n", "");
                        }
                        if (_tmpString.Contains("X-CM-SenderInfo"))
                        {
                            _tmpString = Regex.Replace(_tmpString, "X-CM-SenderInfo[\\s\\S]*?\r\n", "");
                        }
                        if (_tmpString.Contains("Content-Description"))
                        {
                            _tmpString = Regex.Replace(_tmpString, "Content-Description[\\s\\S]*?\r\n", "");
                        }
                        if (_tmpString.Contains("X-Mailer"))
                        {
                            _tmpString = Regex.Replace(_tmpString, "X-Mailer[\\s\\S]*?\r\n", "");
                        }
                        if (_tmpString.Contains("X-IM-id"))
                        {
                            _tmpString = Regex.Replace(_tmpString, "X-IM-id[\\s\\S]*?\r\n", "");
                        }

                        if (Regex.IsMatch(_tmpString, "[\\s\\S]*\r\n\r\n[\\s\\S]*"))
                        {
                            _tmpString = Regex.Match(_tmpString, "\r\n\r\n[\\s\\S]*").Value;
                        }

                        //if (Regex.IsMatch(_tmpString, "[\\s\\S]+\r\n\r\n[\\s\\S]*"))
                        //_tmpString = Regex.Match(_tmpString, "\r\n\r\n[\\s\\S]*").Value;
                        mailContentInfo.Content = Encoding.GetEncoding(mailContentInfo.Charset).GetString(Convert.FromBase64String(_tmpString));
                    }
                    //mailContentInfo.Content = mailContentInfo.Encoding == "quoted-printable" ? DecodeQP(Regex.Replace(mailString, "[\\s\\S]*(?=<html>)", ""), mailContentInfo.Charset) : Encoding.GetEncoding(mailContentInfo.Charset).GetString(Convert.FromBase64String(Regex.Match(mailString, "(?<=base64)[\\s\\S]*(?=\n)").Value));
                }
                else
                {
                    mailString = Regex.Replace(mailString, "Content-Type[\\s\\S]*?\r\n", "");
                    mailString = Regex.Replace(mailString, "Content-Transfer-Encoding[\\s\\S]*?\r\n", "");
                    if (Regex.IsMatch(mailString, "[\\s\\S]+\r\n\r\n[\\s\\S]*"))
                    {
                        mailString = Regex.Match(mailString, "(?<=\r\n\r\n)[\\s\\S]*").Value;
                    }
                    mailContentInfo.Content = Regex.Match(mailString, "[\\s\\S]*(?=\n)").Value;
                }
                //if (mailContentInfo.Content.Contains("<body") && mailContentInfo.Content.Contains("</body>"))
                // mailContentInfo.Content = Regex.Match(mailContentInfo.Content, "(?<=<body [\\s\\S]*?>)[\\s\\S]*(?=</body>)").Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(mailContentInfo);
        }