Exemplo n.º 1
0
        /// <summary>
        /// Given a byte array describing a full message.<br/>
        /// Parses the byte array into a <see cref="MessagePart"/>.
        /// </summary>
        /// <param name="rawMessageContent">The byte array containing both headers and body of a message</param>
        /// <returns>A <see cref="MessagePart"/> which was described by the <paramref name="rawMessageContent"/> byte array</returns>
        private static MessagePart GetMessagePart(byte[] rawMessageContent)
        {
            // Find the headers and the body parts of the byte array
            HeaderExtractor.ExtractHeadersAndBody(rawMessageContent, out var headers, out var body);

            // Create a new MessagePart from the headers and the body
            return(new MessagePart(body, headers));
        }
Exemplo 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)
        {
            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");
        }
Exemplo n.º 3
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
            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);
            }
        }
Exemplo n.º 4
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();
                }
            }
        }
Exemplo n.º 5
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>
        /// <param name="parsingErrorHandler">(Optional) It is notifified when an error occurs while parsing something in the message.
        /// If it is not null, the handler handles the error on the specific element without stopping the message parsing process</param>
        public Message(byte[] rawMessageContent, bool parseBody, IParsingErrorHandler parsingErrorHandler = null)
        {
            RawMessage = rawMessageContent;

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

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

            // 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, parsingErrorHandler);
            }
        }
Exemplo n.º 6
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 MimeMessage(Byte[] rawMessageContent, Boolean 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);
            }
        }