private static void FixEmailBody(EmailBody emailBody)
        {
            var htmlCharset = new Regex("\\<meta.*?charset=(?<charset>[a-zA-Z0-9\\-]*).*?\\>")
                                        .Match(emailBody.Content)
                                        .Groups["charset"]
                                        .Value;

            if (!string.IsNullOrEmpty(htmlCharset))
            {
                emailBody.Charset = htmlCharset;
                return;
            }

            var content = emailBody.Content;

            var meta = string.Format("<meta http-equiv=\"content-type\" content=\"text/html;charset={0}\"/>", emailBody.Charset);
            var bodyIndex = content.IndexOf("<body", 0, StringComparison.InvariantCultureIgnoreCase);
            var htmlIndex = content.IndexOf("<html", 0, StringComparison.InvariantCultureIgnoreCase);

            if (bodyIndex < 0)
            {
                content = string.Format("<body>\r\n{0}\r\n</body>", content);
            }

            var insertIndex = content.IndexOf("</head>", 0, StringComparison.InvariantCultureIgnoreCase);

            if (insertIndex < 0)
            {
                meta = string.Format("<head>\r\n{0}\r\n</head>", meta);
                insertIndex = content.IndexOf("<body", 0, StringComparison.InvariantCultureIgnoreCase);
            }
            content = content.Insert(insertIndex, meta);

            if (htmlIndex < 0)
            {
                content = string.Format("<html>\r\n{0}\r\n</html>", content);
            }

            emailBody.Content = content;
        }
        private static EmailBody ProcessMailPartsRecursively(string partBody, string boundary, int recursionLevel = 0)
        {
            if (string.IsNullOrEmpty(partBody) || string.IsNullOrEmpty(boundary)) return null;

            boundary = boundary.EscapeRegexpSpecialChars();
            var re = new Regex(string.Format(MailPartDividerPattern, boundary), RegexOptions.IgnoreCase | RegexOptions.Singleline);
            var parts = re.Matches(partBody);
            var count = parts.Count;
            EmailBody result = null;

            for (var i = count - 1; i >= 0; i--)
            {
                var part = parts[i];
                var contentType = part.Value.GetHeaderValue("content-type");
                var headerCharset = part.Value.GetHeaderValue("content-type", "charset");

                if (IsMultipartContent(contentType))
                {
                    var subBoundary = part.Value.GetHeaderValue("content-type", "boundary");
                    result = ProcessMailPartsRecursively(part.Value, subBoundary, ++recursionLevel);
                }

                if (result != null)
                {
                    break;
                }

                if (!contentType.Equals(HtmlContentType, StringComparison.InvariantCultureIgnoreCase) && (i > 0 || recursionLevel != 0)) continue;

                var match = new Regex(MailPartDetailsPattern, RegexOptions.Singleline).Match(part.Value);

                result = new EmailBody
                {
                    Content = match.Value,
                    ContentType = contentType,
                    ContentEncoding = part.Value.GetHeaderValue("content-transfer-encoding"),
                    Charset = headerCharset
                };

                break;
            }
            return result;
        }