Esempio n. 1
0
        /// <summary>
        /// Constructs a message from a byte array.<br/>
        /// <br/>
        /// The headers are always parsed, but if <paramref name="parseBody"/> is <see langword="false"/>, the body is not parsed.
        /// </summary>
        /// <param name="rawMessageContent">The byte array which is the message contents to parse</param>
        /// <param name="parseBody">
        /// <see langword="true"/> if the body should be parsed,
        /// <see langword="false"/> if only headers should be parsed out of the <paramref name="rawMessageContent"/> byte array
        /// </param>
        public Message(byte[] rawMessageContent, bool parseBody)
        {
            Logger.WriteToLog("Processing raw EML message content");

            RawMessage = rawMessageContent;

            // Find the headers and the body parts of the byte array
            HeaderExtractor.ExtractHeadersAndBody(rawMessageContent, out var headersTemp, out var body);

            // Set the Headers property
            Headers = headersTemp;

            // Should we also parse the body?
            if (parseBody)
            {
                // Parse the body into a MessagePart
                MessagePart = new MessagePart(body, Headers);

                var findBodyMessagePartWithMediaType = new FindBodyMessagePartWithMediaType();

                // Searches for the first HTML body and mark this one as the HTML body of the E-mail
                HtmlBody = findBodyMessagePartWithMediaType.VisitMessage(this, "text/html");
                if (HtmlBody != null)
                {
                    HtmlBody.IsHtmlBody = true;
                }

                // Searches for the first TEXT body and mark this one as the TEXT body of the E-mail
                TextBody = findBodyMessagePartWithMediaType.VisitMessage(this, "text/plain");
                if (TextBody != null)
                {
                    TextBody.IsTextBody = true;
                }

                var attachments = new AttachmentFinder().VisitMessage(this);

                if (HtmlBody != null)
                {
                    foreach (var attachment in attachments)
                    {
                        if (attachment.IsInline || attachment.ContentId == null)
                        {
                            continue;
                        }
                        var htmlBody = HtmlBody.BodyEncoding.GetString(HtmlBody.Body);
                        attachment.IsInline = htmlBody.Contains($"cid:{attachment.ContentId}");
                    }
                }

                if (attachments != null)
                {
                    Attachments = attachments.AsReadOnly();
                }
            }

            Logger.WriteToLog("Raw EML message content processed");
        }
Esempio n. 2
0
        /// <summary>
        /// Constructs a message from a byte array.<br/>
        /// <br/>
        /// The headers are always parsed, but if <paramref name="parseBody"/> is <see langword="false"/>, the body is not parsed.
        /// </summary>
        /// <param name="rawMessageContent">The byte array which is the message contents to parse</param>
        /// <param name="parseBody">
        /// <see langword="true"/> if the body should be parsed,
        /// <see langword="false"/> if only headers should be parsed out of the <paramref name="rawMessageContent"/> byte array
        /// </param>
        public Message(byte[] rawMessageContent, bool parseBody)
        {
            RawMessage = rawMessageContent;

            // Find the headers and the body parts of the byte array
            MessageHeader headersTemp;

            byte[] body;
            HeaderExtractor.ExtractHeadersAndBody(rawMessageContent, out headersTemp, out body);

            // Set the Headers property
            Headers = headersTemp;

            // Should we also parse the body?
            if (parseBody)
            {
                // Parse the body into a MessagePart
                MessagePart = new MessagePart(body, Headers);

                var findBodyMessagePartWithMediaType = new FindBodyMessagePartWithMediaType();

                // Searches for the first HTML body and mark this one as the HTML body of the E-mail
                HtmlBody = findBodyMessagePartWithMediaType.VisitMessage(this, "text/html");
                if (HtmlBody != null)
                {
                    HtmlBody.IsHtmlBody = true;
                }

                // Searches for the first TEXT body and mark this one as the TEXT body of the E-mail
                TextBody = findBodyMessagePartWithMediaType.VisitMessage(this, "text/plain");
                if (TextBody != null)
                {
                    TextBody.IsTextBody = true;
                }

                var attachments = new AttachmentFinder().VisitMessage(this);
                if (attachments != null)
                {
                    Attachments = attachments.AsReadOnly();
                }
            }
        }