public void TestIsAttachmentApplicationPdf() { const string messagePartContent = "Content-Type: application/pdf\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.IsTrue(messagePart.IsAttachment); }
public void TestIsTextMessageRfc822() { const string messagePartContent = "Content-Type: message/rfc822\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.IsTrue(messagePart.IsText); }
public void TestLineEndingsNotStrippedAwayAtStart() { const string input = "Content-Type: text/plain; charset=iso-8859-1\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + // Headers end "\r\nHello"; // This is where the first \r\n should not be removed const string expectedOutput = "\r\nHello"; string output = new OPMessage(Encoding.ASCII.GetBytes(input)).MessagePart.GetBodyAsText(); Assert.AreEqual(expectedOutput, output); }
public void TestLineEndingsNotStrippedAwayAtEndUsingQuotedPrintable() { const string input = "Content-Type: text/plain; charset=iso-8859-1\r\n" + "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + // Headers end "Hello=\r\n"; // This is where the last \r\n should not be removed // The QP encoding would have decoded Hello=\r\n into Hello, since =\r\n is a soft line break const string expectedOutput = "Hello"; string output = new OPMessage(Encoding.ASCII.GetBytes(input)).MessagePart.GetBodyAsText(); Assert.AreEqual(expectedOutput, output); }
public TreeNode VisitMessage(Message message) { if (message == null) { throw new ArgumentNullException("message"); } // First build up the child TreeNode TreeNode child = VisitMessagePart(message.MessagePart); // Then create the topmost root node with the subject as text TreeNode topNode = new TreeNode(message.Headers.Subject, new[] { child }); return(topNode); }
public void TestISO88599CharacterSet() { const string Iso88599SpecialChars = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏĞÑÒÓÔÕÖ×ØÙÚÛÜİŞßàáâãäåæçèéêëìíîïğñòóôõö÷øùúûüışÿ"; const string input = "Content-Type: text/plain; charset=iso-8859-9\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + // Headers end Iso88599SpecialChars; const string expectedOutput = Iso88599SpecialChars; string output = new OPMessage(Encoding.GetEncoding("ISO-8859-9").GetBytes(input)).MessagePart.GetBodyAsText(); Assert.AreEqual(expectedOutput, output); }
public void TestGetAllMessagesWithMediaTypeMultiPartFindMultiPartMediaTypeWithMultipleMultiParts() { const string rfcExample = "From: Nathaniel Borenstein <*****@*****.**>\r\n" + "To: Ned Freed <*****@*****.**>\r\n" + "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\r\n" + "Subject: Sample message\r\n" + "MIME-Version: 1.0\r\n" + "Content-type: multipart/mixed; boundary=\"simple boundary\"\r\n" + "\r\n" + "--simple boundary\r\n" + "Content-type: multipart/mixed; boundary=\"anotherBoundary\"\r\n" + "\r\n" + "--anotherBoundary\r\n" + "\r\n" + "TEXT\r\n" + "--anotherBoundary\r\n" + "Content-Type: text/html; charset=us-ascii\r\n" + "\r\n" + "HTML\r\n" + "--anotherBoundary--\r\n" + "--simple boundary\r\n" + "Content-type: text/html; charset=ISO-8859-1\r\n" + "\r\n" + "MORE HTML\r\n" + "--simple boundary--"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); System.Collections.Generic.List<MessagePart> parts = message.FindAllMessagePartsWithMediaType("multipart/mixed"); Assert.NotNull(parts); Assert.IsNotEmpty(parts); Assert.AreEqual(2, parts.Count); MessagePart firstPart = parts[0]; Assert.IsTrue(firstPart.IsMultiPart); Assert.AreEqual("multipart/mixed", firstPart.ContentType.MediaType); Assert.AreEqual("simple boundary", firstPart.ContentType.Boundary); MessagePart secondPart = parts[1]; Assert.IsTrue(secondPart.IsMultiPart); Assert.AreEqual("multipart/mixed", secondPart.ContentType.MediaType); Assert.AreEqual("anotherBoundary", secondPart.ContentType.Boundary); }
public void TestAllFirstMessagesWithMediaTypeSimpleNotFound() { const string rfcExample = "Content-type: text/plain; charset=us-ascii\r\n" + "\r\n" + "This is explicitly typed plain US-ASCII text"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); System.Collections.Generic.List<MessagePart> parts = message.FindAllMessagePartsWithMediaType("text/html"); // We should not be able to find such a MessagePart Assert.NotNull(parts); Assert.IsEmpty(parts); }
public void TestGetAllMessagesWithMediaTypeSimpleFound() { const string rfcExample = "Content-type: text/plain; charset=us-ascii\r\n" + "\r\n" + "This is explicitly typed plain US-ASCII text"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); System.Collections.Generic.List<MessagePart> parts = message.FindAllMessagePartsWithMediaType("text/plain"); Assert.NotNull(parts); Assert.IsNotEmpty(parts); Assert.AreEqual(1, parts.Count); MessagePart foundMessagePart = parts[0]; Assert.AreEqual("text/plain", foundMessagePart.ContentType.MediaType); Assert.AreEqual("us-ascii", foundMessagePart.ContentType.CharSet); Assert.AreEqual(Encoding.ASCII, foundMessagePart.BodyEncoding); Assert.AreEqual("This is explicitly typed plain US-ASCII text", foundMessagePart.GetBodyAsText()); }
public void TestContentTypeWithLargeCharactersCanStillBeFound() { const string messagePartContent = "Content-Type: TEXT/PLAIN\r\n" + "\r\n" + // End of message headers "foo"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)); // Cna be found MessagePart textHtml = message.FindFirstPlainTextVersion(); Assert.NotNull(textHtml); Assert.AreEqual("foo", textHtml.GetBodyAsText()); // Can still be found System.Collections.Generic.List<MessagePart> messageParts = message.FindAllTextVersions(); Assert.IsNotEmpty(messageParts); Assert.AreEqual(1, messageParts.Count); Assert.AreEqual(textHtml, messageParts[0]); }
public void TestContentTypeWithMissingSemicolonAndTabs() { const string messagePartContent = "Content-Type: text/plain;\r\n\tcharset=\"Windows-1252\"\r\n\tname=\"ALERTA_1.txt\"\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.AreEqual(Encoding.GetEncoding(1252), messagePart.BodyEncoding); }
public void TestHeaderWhiteSpace() { const string messagePartContent = "Content-Disposition: attachment; filename=Attach2.txt; modification-date=\"21\r\n" + " Feb 2011 17:09:47 +0000\"; read-date=\"21 Feb 2011 17:09:47 +0000\";\r\n" + " creation-date=\"21 Feb 2011 12:59:07 +0000\"\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.IsFalse(messagePart.ContentDisposition.Inline); DateTime modificationDate = new DateTime(2011, 2, 21, 17, 09, 47, DateTimeKind.Utc); Assert.AreEqual(modificationDate, messagePart.ContentDisposition.ModificationDate); DateTime readDate = modificationDate; Assert.AreEqual(readDate, messagePart.ContentDisposition.ReadDate); DateTime creationDate = new DateTime(2011, 2, 21, 12, 59, 07, DateTimeKind.Utc); Assert.AreEqual(creationDate, messagePart.ContentDisposition.CreationDate); Assert.AreEqual("Attach2.txt", messagePart.ContentDisposition.FileName); }
public void TestContentDescriptionTwo() { const string messagePartContent = "Content-Description: This is some OTHER human readable text\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; const string expectedDescription = "This is some OTHER human readable text"; string actualDescription = messagePart.ContentDescription; Assert.AreEqual(expectedDescription, actualDescription); }
public void TestMultiPartRFCExample() { const string rfcExample = "From: Nathaniel Borenstein <*****@*****.**>\r\n" + "To: Ned Freed <*****@*****.**>\r\n" + "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\r\n" + "Subject: Sample message\r\n" + "MIME-Version: 1.0\r\n" + "Content-type: multipart/mixed; boundary=\"simple boundary\"\r\n" + "\r\n" + "This is the preamble. It is to be ignored, though it\r\n" + " is a handy place for composition agents to include an\r\n" + " explanatory note to non-MIME conformant readers.\r\n" + "\r\n" + "--simple boundary\r\n" + "\r\n" + "This is implicitly typed plain US-ASCII text.\r\n" + "It does NOT end with a linebreak.\r\n" + "--simple boundary\r\n" + "Content-type: text/plain; charset=us-ascii\r\n" + "\r\n" + "This is explicitly typed plain US-ASCII text.\r\n" + "It DOES end with a linebreak.\r\n" + "\r\n" + "--simple boundary--\r\n" + "\r\n" + "This is the epilogue. It is also to be ignored."; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); Assert.NotNull(message); Assert.AreEqual("Nathaniel Borenstein", message.Headers.From.DisplayName); Assert.AreEqual("*****@*****.**", message.Headers.From.Address); Assert.AreEqual(1, message.Headers.To.Count); Assert.AreEqual("Ned Freed", message.Headers.To[0].DisplayName); Assert.AreEqual("*****@*****.**", message.Headers.To[0].Address); // -0800 is the same as adding 8 hours to UTC Assert.AreEqual(new DateTime(1993, 3, 22, 7, 56, 48, DateTimeKind.Utc), message.Headers.DateSent); Assert.AreEqual("Sample message", message.Headers.Subject); Assert.AreEqual("1.0", message.Headers.MimeVersion); Assert.AreEqual("multipart/mixed", message.Headers.ContentType.MediaType); Assert.AreEqual("simple boundary", message.Headers.ContentType.Boundary); MessagePart multiPart = message.MessagePart; Assert.IsTrue(multiPart.IsMultiPart); Assert.AreEqual("multipart/mixed", multiPart.ContentType.MediaType); Assert.AreEqual("simple boundary", multiPart.ContentType.Boundary); Assert.NotNull(multiPart.MessageParts); Assert.AreEqual(2, multiPart.MessageParts.Count); MessagePart firstPlainText = multiPart.MessageParts[0]; Assert.AreEqual("This is implicitly typed plain US-ASCII text.\r\n" + "It does NOT end with a linebreak.", firstPlainText.GetBodyAsText()); Assert.AreEqual(Encoding.ASCII, firstPlainText.BodyEncoding); Assert.AreEqual("us-ascii", firstPlainText.ContentType.CharSet); Assert.AreEqual("text/plain", firstPlainText.ContentType.MediaType); MessagePart secondPlainText = multiPart.MessageParts[1]; Assert.AreEqual("This is explicitly typed plain US-ASCII text.\r\n" + "It DOES end with a linebreak.\r\n", secondPlainText.GetBodyAsText()); Assert.AreEqual(Encoding.ASCII, secondPlainText.BodyEncoding); Assert.AreEqual("us-ascii", secondPlainText.ContentType.CharSet); Assert.AreEqual("text/plain", secondPlainText.ContentType.MediaType); }
public void TestSenderNotIncluded() { const string messageString = "Content-Type: text/plain; \r\n" + "\r\n" + "Testing"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)); Assert.IsNull(message.Headers.Sender); }
public void TestMultiPartMessageWithNoBoundaryEnd() { const string multipartMessage = "MIME-Version: 1.0\r\n" + "Content-Type: multipart/alternative; boundary=\"boundary\"\r\n" + "\r\n" + "This is a message with multiple parts in MIME format.\r\n" + "--boundary\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "This is the body of the message.\r\n" + "--boundary\r\n" + "Content-Type: text/html\r\n" + "\r\n" + "<html></html>\r\n"; // Here there should have been a boundary "--boundary--" to delimit last ending OPMessage message = null; Assert.DoesNotThrow(delegate { message = new OPMessage(Encoding.ASCII.GetBytes(multipartMessage)); }); Assert.NotNull(message); Assert.AreEqual(2, message.MessagePart.MessageParts.Count); Assert.AreEqual("This is the body of the message.", message.MessagePart.MessageParts[0].GetBodyAsText()); Assert.AreEqual("<html></html>", message.MessagePart.MessageParts[1].GetBodyAsText()); }
public void TestComplexMultiPartMessage() { const string multiPartMessage = "MIME-Version: 1.0\r\n" + "From: Nathaniel Borenstein <*****@*****.**>\r\n" + "To: Ned Freed <*****@*****.**>\r\n" + "Date: Fri, 07 Oct 1994 16:15:05 -0700 (PDT)\r\n" + "Subject: A multipart example\r\n" + "Content-Type: multipart/mixed;\r\n" + "\t\t\t\t\t\t boundary=unique-boundary-1\r\n" + "\r\n" + "This is the preamble area of a multipart message.\r\n" + "Mail readers that understand multipart format\r\n" + "should ignore this preamble.\r\n" + "\r\n" + "If you are reading this text, you might want to\r\n" + "consider changing to a mail reader that understands\r\n" + "how to properly display multipart messages.\r\n" + "\r\n" + "--unique-boundary-1\r\n" + "\r\n" + " ... Some text appears here ...\r\n" + "--unique-boundary-1\r\n" + "Content-type: text/plain; charset=US-ASCII\r\n" + "\r\n" + "This could have been part of the previous part, but\r\n" + "illustrates explicit versus implicit typing of body\r\n" + "parts.\r\n" + "--unique-boundary-1\r\n" + "Content-Type: multipart/parallel; boundary=unique-boundary-2\r\n" + "\r\n" + "--unique-boundary-2\r\n" + "Content-Type: audio/basic\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + "dGVzdCBhdWRpbw==\r\n" + // "test audio" in base64 "--unique-boundary-2\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + "dGVzdCBpbWFnZQ==\r\n" + // "test image" in base64 "--unique-boundary-2--\r\n" + "\r\n" + "--unique-boundary-1\r\n" + "Content-type: text/enriched\r\n" + "\r\n" + "This is <bold><italic>enriched.</italic></bold>\r\n" + "<smaller>as defined in RFC 1896</smaller>\r\n" + "\r\n" + "Isn\'t it\r\n" + "<bigger><bigger>cool?</bigger></bigger>\r\n" + "--unique-boundary-1\r\n" + "Content-Type: message/rfc822\r\n" + "\r\n" + "From: Test <*****@*****.**>\r\n" + "To: Test <*****@*****.**>\r\n" + "Subject: Test subject\r\n" + "Content-Type: Text/plain; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: Quoted-printable\r\n" + "\r\n" + "... Additional text in ISO-8859-1 goes here ... 3 + 5 =3D 8\r\n" + "--unique-boundary-1--"; // No special characters used - we can use ASCII to get the bytes OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(multiPartMessage)); Assert.AreEqual("1.0", message.Headers.MimeVersion); // From Assert.AreEqual("Nathaniel Borenstein", message.Headers.From.DisplayName); Assert.AreEqual("*****@*****.**", message.Headers.From.Address); // To Assert.NotNull(message.Headers.To); Assert.AreEqual(1, message.Headers.To.Count); Assert.AreEqual("Ned Freed", message.Headers.To[0].DisplayName); Assert.AreEqual("*****@*****.**", message.Headers.To[0].Address); // Date Assert.AreEqual("Fri, 07 Oct 1994 16:15:05 -0700 (PDT)", message.Headers.Date); // -0700 is the same as adding 7 hours in the UTC DateTime Assert.AreEqual(new DateTime(1994, 10, 7, 23, 15, 05, DateTimeKind.Utc), message.Headers.DateSent); // Subject Assert.AreEqual("A multipart example", message.Headers.Subject); MessagePart part1 = message.MessagePart; Assert.AreEqual("multipart/mixed", part1.ContentType.MediaType); Assert.IsTrue(part1.IsMultiPart); Assert.NotNull(part1.MessageParts); Assert.IsNull(part1.Body); // There is a total of 5 multiparts in the first message (unique-boundary-1) Assert.AreEqual(5, part1.MessageParts.Count); // Fetch out the parts, which are checked against later System.Collections.Generic.List<MessagePart> attachments = message.FindAllAttachments(); System.Collections.Generic.List<MessagePart> textVersions = message.FindAllTextVersions(); // We are now going one level deeper into the message tree { MessagePart part1Part1 = part1.MessageParts[0]; Assert.NotNull(part1Part1); Assert.IsFalse(part1Part1.IsMultiPart); Assert.NotNull(part1Part1.Body); Assert.AreEqual("text/plain", part1Part1.ContentType.MediaType); Assert.AreEqual(" ... Some text appears here ...", part1Part1.GetBodyAsText()); // Check that the fetching algoritm for finding a plain-text version is working Assert.AreEqual(part1Part1, message.FindFirstPlainTextVersion()); // Check this message is included in the text version Assert.Contains(part1Part1, textVersions); // But not included in the attachments Assert.IsFalse(attachments.Contains(part1Part1)); // We are now going one level deeper into the message tree { MessagePart part1Part2 = part1.MessageParts[1]; Assert.NotNull(part1Part2); Assert.IsFalse(part1Part2.IsMultiPart); Assert.NotNull(part1Part2.Body); Assert.AreEqual("text/plain", part1Part2.ContentType.MediaType); Assert.AreEqual("US-ASCII", part1Part2.ContentType.CharSet); Assert.AreEqual("This could have been part of the previous part, but\r\n" + "illustrates explicit versus implicit typing of body\r\n" + "parts.", part1Part2.GetBodyAsText()); // Check this message is included in the text version Assert.Contains(part1Part2, textVersions); // But not included in the attachments Assert.IsFalse(attachments.Contains(part1Part2)); } // We are now going one level deeper into the message tree { MessagePart part1Part3 = part1.MessageParts[2]; Assert.NotNull(part1Part3); Assert.IsTrue(part1Part3.IsMultiPart); Assert.IsNotNull(part1Part3.MessageParts); Assert.IsNull(part1Part3.Body); // There is a total of message parts in part1Part3 Assert.AreEqual(2, part1Part3.MessageParts.Count); Assert.AreEqual("multipart/parallel", part1Part3.ContentType.MediaType); // Check this message is not in the text versions Assert.IsFalse(textVersions.Contains(part1Part3)); // Check this message is not included in the attachments Assert.IsFalse(attachments.Contains(part1Part3)); // We are now diving into part1Part3 multiparts - therefore going one level deeper in the message tree { MessagePart part1Part3Part1 = part1Part3.MessageParts[0]; Assert.NotNull(part1Part3Part1); Assert.IsFalse(part1Part3Part1.IsMultiPart); Assert.NotNull(part1Part3Part1.Body); Assert.AreEqual("audio/basic", part1Part3Part1.ContentType.MediaType); Assert.AreEqual(ContentTransferEncoding.Base64, part1Part3Part1.ContentTransferEncoding); Assert.AreEqual("test audio", part1Part3Part1.GetBodyAsText()); // Check this message is not in the text versions Assert.IsFalse(textVersions.Contains(part1Part3Part1)); // Check this message is included in the attachments Assert.Contains(part1Part3Part1, attachments); MessagePart part1Part3Part2 = part1Part3.MessageParts[1]; Assert.NotNull(part1Part3Part2); Assert.IsFalse(part1Part3Part2.IsMultiPart); Assert.NotNull(part1Part3Part2.Body); Assert.AreEqual("image/jpeg", part1Part3Part2.ContentType.MediaType); Assert.AreEqual(ContentTransferEncoding.Base64, part1Part3Part2.ContentTransferEncoding); Assert.AreEqual("test image", part1Part3Part2.GetBodyAsText()); // Check this message is not in the text versions Assert.IsFalse(textVersions.Contains(part1Part3Part2)); // Check this message is included in the attachments Assert.Contains(part1Part3Part2, attachments); } } // We are now going one level deeper into the message tree { MessagePart part1Part4 = part1.MessageParts[3]; Assert.NotNull(part1Part4); Assert.IsFalse(part1Part4.IsMultiPart); Assert.NotNull(part1Part4.Body); Assert.AreEqual("text/enriched", part1Part4.ContentType.MediaType); Assert.AreEqual("This is <bold><italic>enriched.</italic></bold>\r\n" + "<smaller>as defined in RFC 1896</smaller>\r\n" + "\r\n" + "Isn\'t it\r\n" + "<bigger><bigger>cool?</bigger></bigger>", part1Part4.GetBodyAsText()); // Check this message is in the text versions Assert.Contains(part1Part4, textVersions); // Check this message is not included in the attachments Assert.IsFalse(attachments.Contains(part1Part4)); } // We are now going one level deeper into the message tree { MessagePart part1Part5 = part1.MessageParts[4]; Assert.NotNull(part1Part5); Assert.IsFalse(part1Part5.IsMultiPart); Assert.NotNull(part1Part5.Body); Assert.AreEqual("message/rfc822", part1Part5.ContentType.MediaType); Assert.AreEqual("From: Test <*****@*****.**>\r\n" + "To: Test <*****@*****.**>\r\n" + "Subject: Test subject\r\n" + "Content-Type: Text/plain; charset=ISO-8859-1\r\n" + "Content-Transfer-Encoding: Quoted-printable\r\n" + "\r\n" + "... Additional text in ISO-8859-1 goes here ... 3 + 5 =3D 8", part1Part5.GetBodyAsText()); // Check this message is in the text versions Assert.Contains(part1Part5, textVersions); // Check this message is not included in the attachments Assert.IsFalse(attachments.Contains(part1Part5)); // This last part is actually a message. Lets try to parse it OPMessage lastMessage = new OPMessage(part1Part5.Body); // From Assert.AreEqual("Test", lastMessage.Headers.From.DisplayName); Assert.AreEqual("*****@*****.**", lastMessage.Headers.From.Address); // To Assert.NotNull(lastMessage.Headers.To); Assert.AreEqual(1, lastMessage.Headers.To.Count); Assert.AreEqual("Test", lastMessage.Headers.To[0].DisplayName); Assert.AreEqual("*****@*****.**", lastMessage.Headers.To[0].Address); // Subject Assert.AreEqual("Test subject", lastMessage.Headers.Subject); // We are now going one level deeper into the message tree { MessagePart lastPart = lastMessage.MessagePart; Assert.IsFalse(lastPart.IsMultiPart); Assert.IsNull(lastPart.MessageParts); Assert.NotNull(lastPart.Body); Assert.AreEqual(ContentTransferEncoding.QuotedPrintable, lastPart.ContentTransferEncoding); Assert.AreEqual("Text/plain", lastPart.ContentType.MediaType); Assert.AreEqual("ISO-8859-1", lastPart.ContentType.CharSet); // Notice that =3D has been decoded to = because it was QuotedPrintable encoded Assert.AreEqual("... Additional text in ISO-8859-1 goes here ... 3 + 5 = 8", lastPart.GetBodyAsText()); } } } }
public void TestSender() { const string messageString = "Content-Type: text/plain; \r\n" + "Sender: Secretary <*****@*****.**>\r\n" + "\r\n" + "Testing"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)); Assert.IsNotNull(message.Headers.Sender); Assert.IsTrue(message.Headers.Sender.HasValidMailAddress); Assert.AreEqual("Secretary", message.Headers.Sender.DisplayName); Assert.AreEqual("*****@*****.**", message.Headers.Sender.Address); }
public void TestMultiPartMessage() { const string multipartMessage = "MIME-Version: 1.0\r\n" + "Content-Type: multipart/mixed; boundary=\"frontier\"\r\n" + "\r\n" + "This is a message with multiple parts in MIME format.\r\n" + "--frontier\r\n" + "Content-Type: text/plain\r\n" + "\r\n" + "This is the body of the message.\r\n" + "--frontier\r\n" + "Content-Type: application/octet-stream\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + "PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n" + "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\r\n" + "--frontier--"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(multipartMessage)); MessagePart topPart = message.MessagePart; Assert.IsTrue(topPart.IsMultiPart); Assert.NotNull(topPart.MessageParts); Assert.AreEqual(2, topPart.MessageParts.Count); MessagePart part1 = topPart.MessageParts[0]; Assert.AreEqual("text/plain", part1.ContentType.MediaType); Assert.AreEqual("This is the body of the message.", part1.GetBodyAsText()); MessagePart part2 = topPart.MessageParts[1]; Assert.AreEqual("application/octet-stream", part2.ContentType.MediaType); Assert.AreEqual(ContentTransferEncoding.Base64, part2.ContentTransferEncoding); Assert.AreEqual( Convert.FromBase64String("PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n" + "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==\r\n"), part2.Body); }
public void TestSaveToFile() { const string base64 = "JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIv\r\n" + "TGFuZyhkYS1ESykgL1N0cnVjdFRyZWVSb290IDE1IDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0\r\n" + "cnVlPj4+Pg0KZW5kb2JqDQoyIDAgb2JqDQo8PC9UeXBlL1BhZ2VzL0NvdW50IDEvS2lkc1sg\r\n" + "MyAwIFJdID4+DQplbmRvYmoNCjMgMCBvYmoNCjw8L1R5cGUvUGFnZS9QYXJlbnQgMiAwIFIv\r\n" + "UmVzb3VyY2VzPDwvRm9udDw8L0YxIDUgMCBSL0YyIDcgMCBSL0YzIDkgMCBSPj4vUHJvY1Nl\r\n" + "dFsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldID4+L01lZGlhQm94WyAwIDAgNTk0\r\n" + "Ljk2IDg0Mi4wNF0gL0NvbnRlbnRzIDQgMCBSL0dyb3VwPDwvVHlwZS9Hcm91cC9TL1RyYW5z\r\n" + "cGFyZW5jeS9DUy9EZXZpY2VSR0I+Pi9UYWJzL1MvU3RydWN0UGFyZW50cyAwPj4NCmVuZG9i"; const string partPdf = "Content-Type: application/pdf;\r\n" + " name=\"=?ISO-8859-1?Q?=D8nskeliste=2Epdf?=\"\r\n" + "Content-Transfer-Encoding: base64\r\n" + "\r\n" + base64; // Base 64 is only in ASCII OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(partPdf)); MessagePart messagePart = message.MessagePart; // Check the headers Assert.AreEqual("application/pdf", messagePart.ContentType.MediaType); Assert.AreEqual(ContentTransferEncoding.Base64, messagePart.ContentTransferEncoding); Assert.AreEqual("Ønskeliste.pdf", messagePart.ContentType.Name); byte[] correctBytes = Convert.FromBase64String(base64); // This will fail if US-ASCII is assumed on the bytes when decoded from base64 to bytes Assert.AreEqual(correctBytes, messagePart.Body); FileInfo testFile = new FileInfo("test_message_save_.testFile"); messagePart.Save(testFile); byte[] fileBytes = File.ReadAllBytes(testFile.ToString()); testFile.Delete(); Assert.AreEqual(correctBytes, fileBytes); }
public void TestSaveAndLoad() { const string messageString = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)); FileInfo testFile = new FileInfo("test_message_save_.testFile"); message.Save(testFile); OPMessage message2 = OPMessage.Load(testFile); Assert.AreEqual("Testing", message.MessagePart.GetBodyAsText()); Assert.AreEqual("Testing", message2.MessagePart.GetBodyAsText()); Assert.AreEqual("text/plain", message.Headers.ContentType.MediaType); Assert.AreEqual("text/plain", message2.Headers.ContentType.MediaType); Assert.IsFalse(message.Headers.ContentDisposition.Inline); Assert.IsFalse(message2.Headers.ContentDisposition.Inline); testFile.Delete(); }
public void TestQuotedPrintableDoesNotDecodeUnderscoresInBody() { const string messagePartContent = "Content-Transfer-Encoding: quoted-printable\r\n" + "\r\n" + // End of message headers "a_a"; MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; // QuotedPrintable, when used as Content-Transfer-Encoding does not decode _ to spaces const string expectedBody = "a_a"; string actualBody = messagePart.GetBodyAsText(); Assert.AreEqual(expectedBody, actualBody); }
public void TestIsAttachmentIsAnAttachment() { const string messageString = "Content-Type: text/plain\r\n" + "Content-Disposition: attachment\r\n" + "\r\n" + "Testing"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)); Assert.IsTrue(message.MessagePart.IsAttachment); }
public void TestContentDisposition() { const string messagePartContent = "Content-Disposition: attachment\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.IsFalse(messagePart.ContentDisposition.Inline); }
public void TestWindows1254CharacterSet() { const string windows1254SpecialChars = "€‚ƒ„…†‡ˆ‰Š‹Œ‘’“”•–—˜™š›œŸ"; // Windows 1254 is compatible with ISO-8859-9 in ranges [A0-FF]. const string Iso88599SpecialChars = "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏĞÑÒÓÔÕÖ×ØÙÚÛÜİŞßàáâãäåæçèéêëìíîïğñòóôõö÷øùúûüışÿ"; const string specialChars = windows1254SpecialChars + Iso88599SpecialChars; const string input = "Content-Type: text/plain; charset=windows-1254\r\n" + "Content-Transfer-Encoding: 7bit\r\n" + "\r\n" + // Headers end specialChars; const string expectedOutput = specialChars; string output = new OPMessage(Encoding.GetEncoding(1254).GetBytes(input)).MessagePart.GetBodyAsText(); Assert.AreEqual(expectedOutput, output); }
public void TestContentTypeCharsetWithLargeFirstChar() { const string messagePartContent = "Content-Type: TEXT/PLAIN; Charset=\"US-ASCII\"\r\n" + "\r\n" + // End of message headers "foo"; MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.AreEqual(Encoding.ASCII, messagePart.BodyEncoding); Assert.AreEqual("foo", messagePart.GetBodyAsText()); }
public void TestGetFirstMessageWithMediaTypeMultiPartFindMultiPartMediaTypeWithMultipleHTML() { const string rfcExample = "From: Nathaniel Borenstein <*****@*****.**>\r\n" + "To: Ned Freed <*****@*****.**>\r\n" + "Date: Sun, 21 Mar 1993 23:56:48 -0800 (PST)\r\n" + "Subject: Sample message\r\n" + "MIME-Version: 1.0\r\n" + "Content-type: multipart/mixed; boundary=\"simple boundary\"\r\n" + "\r\n" + "--simple boundary\r\n" + "Content-type: multipart/mixed; boundary=\"anotherBoundary\"\r\n" + "\r\n" + "--anotherBoundary\r\n" + "\r\n" + "TEXT\r\n" + "--anotherBoundary\r\n" + "Content-Type: text/html; charset=us-ascii\r\n" + "\r\n" + "HTML\r\n" + "--anotherBoundary--\r\n" + "--simple boundary\r\n" + "Content-type: text/html; charset=ISO-8859-1\r\n" + "\r\n" + "MORE HTML\r\n" + "--simple boundary--"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); MessagePart part = message.FindFirstMessagePartWithMediaType("text/html"); Assert.NotNull(part); Assert.IsFalse(part.IsMultiPart); Assert.AreEqual("text/html", part.ContentType.MediaType); Assert.AreEqual("HTML", part.GetBodyAsText()); }
public void TestGetFirstMessageWithMediaTypeSimpleNotFound() { const string rfcExample = "Content-type: text/plain; charset=us-ascii\r\n" + "\r\n" + "This is explicitly typed plain US-ASCII text"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); MessagePart part = message.FindFirstMessagePartWithMediaType("text/html"); // We should not be able to find such a MessagePart Assert.Null(part); }
public void TestMessageWithonlyHeaders() { const string messageString = "Content-Type: text/plain"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)); Assert.NotNull(message); Assert.AreEqual("text/plain", message.Headers.ContentType.MediaType); Assert.NotNull(message.MessagePart); Assert.IsFalse(message.MessagePart.IsMultiPart); Assert.IsEmpty(message.MessagePart.Body); Assert.IsEmpty(message.MessagePart.GetBodyAsText()); }
public void TestIsAttachmentImageJpeg() { const string messagePartContent = "Content-Type: image/jpeg\r\n" + "\r\n"; // End of message headers MessagePart messagePart = new OPMessage(Encoding.ASCII.GetBytes(messagePartContent)).MessagePart; Assert.IsTrue(messagePart.IsAttachment); }
public void TestGetFirstMessageWithMediaTypeSimpleFoundHTML() { const string rfcExample = "Content-type: text/html; charset=us-ascii\r\n" + "\r\n" + "This is explicitly typed plain US-ASCII HTML text"; OPMessage message = new OPMessage(Encoding.ASCII.GetBytes(rfcExample)); MessagePart part = message.FindFirstMessagePartWithMediaType("text/html"); Assert.NotNull(part); Assert.AreEqual("text/html", part.ContentType.MediaType); Assert.AreEqual("us-ascii", part.ContentType.CharSet); Assert.AreEqual(Encoding.ASCII, part.BodyEncoding); Assert.AreEqual("This is explicitly typed plain US-ASCII HTML text", part.GetBodyAsText()); }