Ejemplo n.º 1
0
        public virtual void Load(Stream reader, bool headersOnly = false, int maxLength = 0, char?termChar = null)
        {
            HeadersOnly = headersOnly;
            Headers     = null;
            Body        = null;
            if (maxLength == 0)
            {
                return;
            }

            var    headers = new StringBuilder();
            string line;

            while ((line = reader.ReadLine(ref maxLength, _DefaultEncoding, termChar)) != null)
            {
                if (line.Length == 0)
                {
                    if (headers.Length == 0)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                headers.AppendLine(line);
            }

            RawHeaders = headers.ToString();

            if (!headersOnly)
            {
                var boundary = Headers.GetBoundary();
                if (!string.IsNullOrEmpty(boundary))
                {
                    var atts = new List <Attachment>();
                    var body = ParseMime(reader, boundary, ref maxLength, atts, Encoding, termChar);
                    if (!string.IsNullOrEmpty(body))
                    {
                        SetBody(body);
                    }

                    foreach (var att in atts)
                    {
                        (att.IsAttachment ? Attachments : AlternateViews).Add(att);
                    }

                    if (maxLength > 0)
                    {
                        reader.ReadToEnd(maxLength, Encoding);
                    }
                }
                else
                {
                    //  sometimes when email doesn't have a body, we get here with maxLength == 0 and we shouldn't read any further
                    var body = string.Empty;
                    if (maxLength > 0)
                    {
                        body = reader.ReadToEnd(maxLength, Encoding);
                    }

                    SetBody(body);
                }
            }
            else if (maxLength > 0)
            {
                reader.ReadToEnd(maxLength, Encoding);
            }

            if ((string.IsNullOrWhiteSpace(Body) || ContentType.StartsWith("multipart/")) && AlternateViews.Count > 0)
            {
                var att = AlternateViews.GetTextView() ?? AlternateViews.GetHtmlView();
                if (att != null)
                {
                    Body = att.Body;
                    ContentTransferEncoding = att.Headers["Content-Transfer-Encoding"].RawValue;
                    ContentType             = att.Headers["Content-Type"].RawValue;
                }
            }

            Date = Headers.GetDate();
            try { To = Headers.GetMailAddresses("To").ToList(); } catch { }
            try { Cc = Headers.GetMailAddresses("Cc").ToList(); } catch { }
            try { Bcc = Headers.GetMailAddresses("Bcc").ToList(); } catch { }
            try { Sender = Headers.GetMailAddresses("Sender").FirstOrDefault(); } catch { }
            try { ReplyTo = Headers.GetMailAddresses("Reply-To").ToList(); } catch { }
            try { From = Headers.GetMailAddresses("From").FirstOrDefault(); } catch { }
            MessageID = Headers["Message-ID"].RawValue;

            Importance = Headers.GetEnum <MailPriority>("Importance");
            Subject    = Headers["Subject"].RawValue;
        }
Ejemplo n.º 2
0
        public virtual void Load(Stream reader, bool headersOnly = false, int maxLength = 0, char?termChar = null)
        {
            _HeadersOnly = headersOnly;
            Headers      = null;
            Body         = null;


            var    headers = new StringBuilder();
            string line;

            while ((line = reader.ReadLine(ref maxLength, _DefaultEncoding, termChar)) != null)
            {
                if (line.Trim().Length == 0)
                {
                    if (headers.Length == 0)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                headers.AppendLine(line);
            }
            RawHeaders = headers.ToString();

            if (!headersOnly)
            {
                string boundary = Headers.GetBoundary();
                if (!string.IsNullOrEmpty(boundary))
                {
                    var atts = new List <Attachment>();
                    var body = ParseMime(reader, boundary, ref maxLength, atts, Encoding, termChar);
                    if (!string.IsNullOrEmpty(body))
                    {
                        SetBody(body);
                    }

                    foreach (var att in atts)
                    {
                        (att.IsAttachment ? Attachments : AlternateViews).Add(att);
                    }

                    if (maxLength > 0)
                    {
                        reader.ReadToEnd(maxLength, Encoding);
                    }
                }
                else
                {
                    SetBody(reader.ReadToEnd(maxLength, Encoding));
                }
            }

            if ((string.IsNullOrWhiteSpace(Body) || ContentType.MediaType.StartsWith("multipart/")) && AlternateViews.Count > 0)
            {
                var att = AlternateViews.GetTextView() ?? AlternateViews.GetHtmlView();
                if (att != null)
                {
                    Body = att.Body;
                    ContentTransferEncoding = att.Headers["Content-Transfer-Encoding"].RawValue;
                    SetContentType(att.Headers["Content-Type"].RawValue);
                }
            }

            Date      = Headers.GetDate();
            To        = Headers.GetMailAddresses("To").ToList();
            Cc        = Headers.GetMailAddresses("Cc").ToList();
            Bcc       = Headers.GetMailAddresses("Bcc").ToList();
            Sender    = Headers.GetMailAddresses("Sender").FirstOrDefault();
            ReplyTo   = Headers.GetMailAddresses("Reply-To").ToList();
            From      = Headers.GetMailAddresses("From").FirstOrDefault();
            MessageID = Headers["Message-ID"].RawValue;

            Importance = Headers.GetEnum <MailPriority>("Importance");
            Subject    = Headers["Subject"].RawValue;
        }