Exemplo n.º 1
0
        public void HeaderDocument_ShouldBeSameAsReturnedWhenReading()
        {
            var reader = new HeaderExtractor(File.OpenRead(TestFilePath));
            var doc    = reader.ReadHeader();

            Assert.Same(doc, reader.HeaderDocument);
        }
Exemplo n.º 2
0
        public void NormalRead()
        {
            var reader = new HeaderExtractor(File.OpenRead(TestFilePath));
            var doc    = reader.ReadHeader();

            Assert.NotNull(doc);
            Assert.Equal("QvdTableHeader", doc.Root.Name);
        }
Exemplo n.º 3
0
        public void TestGetHeadersValueSplitLine()
        {
            const string expectedName            = "MessageID";
            KeyValuePair <string, string> header = HeaderExtractor.SeparateHeaderNameAndValue(string.Format("{0}:", expectedName));

            Assert.AreEqual(expectedName, header.Key, "Header Name");
            Assert.AreEqual(string.Empty, header.Value, "Header Value");
        }
Exemplo n.º 4
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.º 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>
        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.º 6
0
        public void IsRead()
        {
            var reader = new HeaderExtractor(File.OpenRead(TestFilePath));

            Assert.False(reader.IsRead);
            var doc = reader.ReadHeader();

            Assert.True(reader.IsRead);
        }
Exemplo n.º 7
0
        public void TestGetHeadersValueSingleLine()
        {
            const string expectedName            = "MessageID";
            const string expectedValue           = "<1234567890>";
            KeyValuePair <string, string> header = HeaderExtractor.SeparateHeaderNameAndValue(string.Format("{0}: {1}", expectedName, expectedValue));

            Assert.AreEqual(expectedName, header.Key, "Header Name");
            Assert.AreEqual(expectedValue, header.Value, "Header Value");
        }
Exemplo n.º 8
0
        public void TestInvalidParameterCharsetInContentDisposition()
        {
            string base64Header = "Content-Disposition: attachment; charset=windows-1251; filename=\"image.jpg\"";
            string expectedName = "image.jpg";

            NameValueCollection col = HeaderExtractor.ExtractHeaders(base64Header);

            Assert.AreEqual(1, col.Count);

            MessageHeader header = new MessageHeader(col);

            Assert.IsNotNull(header.ContentDisposition);
            Assert.AreEqual(expectedName, header.ContentDisposition.FileName);
        }
Exemplo n.º 9
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.º 10
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.º 11
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.º 12
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);
            }
        }
Exemplo n.º 13
0
        public void TestSpaceInBase64HeaderValue()
        {
            string base64Header = "Disposition-Notification-To: =?windows-1251?B?ZWFzdXJlLg\r\n"
                                  + " ==?=\r\n"
                                  + "\t<*****@*****.**>\r\n"
            ;

            string expectedName    = "easure.";
            string expectedAddress = "*****@*****.**";

            NameValueCollection col = HeaderExtractor.ExtractHeaders(base64Header);

            Assert.AreEqual(1, col.Count);

            MessageHeader header = new MessageHeader(col);

            Assert.AreEqual(1, header.DispositionNotificationTo.Count);

            RfcMailAddress address = header.DispositionNotificationTo[0];

            Assert.IsNotNull(address.MailAddress);
            Assert.AreEqual(expectedName, address.MailAddress.DisplayName);
            Assert.AreEqual(expectedAddress, address.MailAddress.Address);
        }
Exemplo n.º 14
0
        public void HeaderDocument_ShouldThrowIfNotRead()
        {
            var reader = new HeaderExtractor(File.OpenRead(TestFilePath));

            Assert.Throws <InvalidOperationException>(() => _ = reader.HeaderDocument);
        }