Esempio n. 1
0
        public void ToMailMessageHasCorrectContentType()
        {
            const string multipartMessage =
                "MIME-Version: 1.0\r\n" +
                "Content-ID: test\r\n" +
                "Content-Type: application/pdf\r\n" +
                "\r\n" +
                "Test if attachment has same ID in MailMessage";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(multipartMessage)).ToMailMessage();

            Assert.NotNull(mailMessage);

            // Check no body or alternative views
            Assert.IsNullOrEmpty(mailMessage.Body);
            Assert.IsEmpty(mailMessage.AlternateViews);

            // Check attachment
            Assert.AreEqual(1, mailMessage.Attachments.Count);

            Attachment firstAttachment = mailMessage.Attachments[0];

            Assert.NotNull(firstAttachment.ContentType);
            Assert.NotNull(firstAttachment.ContentType.MediaType);
            Assert.AreEqual("application/pdf", firstAttachment.ContentType.MediaType);
        }
Esempio n. 2
0
        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);
        }
        /// <summary>
        /// Finds the first <see cref="MessagePart"/> with the given MediaType
        /// </summary>
        /// <param name="message">The <see cref="OPMessage"/> to start looking in</param>
        /// <param name="question">The MediaType to look for. Case is ignored.</param>
        /// <returns>A <see cref="MessagePart"/> with the given MediaType or <see langword="null"/> if no such <see cref="MessagePart"/> was found</returns>
        public MessagePart VisitMessage(OPMessage message, string question)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            return(VisitMessagePart(message.MessagePart, question));
        }
        /// <summary>
        /// Call this when you want an answer for a full message.
        /// </summary>
        /// <param name="message">The message you want to traverse</param>
        /// <returns>An answer</returns>
        /// <exception cref="ArgumentNullException">if <paramref name="message"/> is <see langword="null"/></exception>
        public TAnswer VisitMessage(OPMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            return(VisitMessagePart(message.MessagePart));
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        /// <summary>
        /// Example showing:
        ///  - how to a find plain text version in a Message
        ///  - how to save MessageParts to file
        /// </summary>
        /// <param name="message">The message to examine for plain text</param>
        public static void FindPlainTextInMessage(OPMessage message)
        {
            MessagePart plainText = message.FindFirstPlainTextVersion();

            if (plainText != null)
            {
                // Save the plain text to a file, database or anything you like
                plainText.Save(new FileInfo("plainText.txt"));
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Example showing:
        ///  - how to find a html version in a Message
        ///  - how to save MessageParts to file
        /// </summary>
        /// <param name="message">The message to examine for html</param>
        public static void FindHtmlInMessage(OPMessage message)
        {
            MessagePart html = message.FindFirstHtmlVersion();

            if (html != null)
            {
                // Save the plain text to a file, database or anything you like
                html.Save(new FileInfo("html.txt"));
            }
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        public void ToMailMessageDoesNotReturnNull()
        {
            const string message =
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
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);
        }
Esempio n. 13
0
        public void TestSenderNotIncluded()
        {
            const string messageString =
                "Content-Type: text/plain; \r\n" +
                "\r\n" +
                "Testing";

            MailMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)).ToMailMessage();

            Assert.IsNull(message.Sender);
        }
Esempio n. 14
0
        public void ToMailMessageSimpleHasTextBody()
        {
            const string message =
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);
            Assert.AreEqual("Test", mailMessage.Body);
        }
Esempio n. 15
0
        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());
        }
Esempio n. 16
0
        public void ToMailMessageSimpleHasTextBodyWithCorrectEncodingISO88599()
        {
            const string message =
                "Content-Type: text/plain; charset=ISO-8859-9\r\n" +
                "\r\n" +                 // Headers end
                "Test øùúûüışÿ";

            MailMessage mailMessage = new OPMessage(Encoding.GetEncoding("ISO-8859-9").GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);
            Assert.AreEqual("Test øùúûüışÿ", mailMessage.Body);
            Assert.AreEqual(Encoding.GetEncoding("ISO-8859-9"), mailMessage.BodyEncoding);
        }
Esempio n. 17
0
        public void ToMailMessageSimpleHasCorrectFromWhenARFCMailAddressDoesNotHaveValidMailAddress()
        {
            const string message =
                "From: Test \r\n" +
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);

            Assert.IsNull(mailMessage.From);
        }
Esempio n. 18
0
        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);
        }
Esempio n. 19
0
        public void TestSender()
        {
            const string messageString =
                "Content-Type: text/plain; \r\n" +
                "Sender: Secretary <*****@*****.**>\r\n" +
                "\r\n" +
                "Testing";

            MailMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)).ToMailMessage();

            Assert.IsNotNull(message.Sender);
            Assert.AreEqual("Secretary", message.Sender.DisplayName);
            Assert.AreEqual("*****@*****.**", message.Sender.Address);
        }
Esempio n. 20
0
        public void ToMailMessageMultiPartMessage()
        {
            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--";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(multipartMessage)).ToMailMessage();

            Assert.NotNull(mailMessage);

            // Check body
            Assert.NotNull(mailMessage.Body);
            Assert.AreEqual("This is the body of the message.", mailMessage.Body);
            Assert.AreEqual(Encoding.ASCII, mailMessage.BodyEncoding);
            Assert.IsFalse(mailMessage.IsBodyHtml);

            // Check no alternative view
            Assert.IsEmpty(mailMessage.AlternateViews);

            // Check attachments
            Assert.NotNull(mailMessage.Attachments);
            Assert.AreEqual(1, mailMessage.Attachments.Count);
            Attachment firstAttachment = mailMessage.Attachments[0];

            Assert.AreEqual("application/octet-stream", firstAttachment.ContentType.MediaType);
            Assert.AreEqual(TransferEncoding.Base64, firstAttachment.TransferEncoding);
            Assert.NotNull(firstAttachment.ContentStream);

            byte[] expectedBytes = Convert.FromBase64String(
                "PGh0bWw+CiAgPGhlYWQ+CiAgPC9oZWFkPgogIDxib2R5PgogICAgPHA+VGhpcyBpcyB0aGUg\r\n" +
                "Ym9keSBvZiB0aGUgbWVzc2FnZS48L3A+CiAgPC9ib2R5Pgo8L2h0bWw+Cg==");

            // Read the bytes from the attachment to see if they are equal
            byte[] actualBytes = getAttachmentBytes(firstAttachment);

            // Check if the bytes are equal
            Assert.AreEqual(expectedBytes, actualBytes);
        }
Esempio n. 21
0
        /// <summary>
        /// Example showing:
        ///  - how to save a message to a file
        ///  - how to load a message from a file at a later point
        /// </summary>
        /// <param name="message">The message to save and load at a later point</param>
        /// <returns>The Message, but loaded from the file system</returns>
        public static OPMessage SaveAndLoadFullMessage(OPMessage message)
        {
            // FileInfo about the location to save/load message
            FileInfo file = new FileInfo("someFile.eml");

            // Save the full message to some file
            message.Save(file);

            // Now load the message again. This could be done at a later point
            OPMessage loadedMessage = OPMessage.Load(file);

            // use the message again
            return(loadedMessage);
        }
Esempio n. 22
0
        public void TestSenderInvalidMailAddress()
        {
            const string messageString =
                "Content-Type: text/plain; \r\n" +
                "Sender: Secretary\r\n" +
                "\r\n" +
                "Testing";

            MailMessage message = new OPMessage(Encoding.ASCII.GetBytes(messageString)).ToMailMessage();

            // Unable to parse to MailAddress from RfcMailAddress
            // since no address is specified in the header
            Assert.IsNull(message.Sender);
        }
Esempio n. 23
0
        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);
        }
Esempio n. 24
0
        private void MenuViewSourceClick(object sender, EventArgs e)
        {
            if (listMessages.SelectedNode != null)
            {
                int       messageNumber = GetMessageNumberFromSelectedNode(listMessages.SelectedNode);
                OPMessage m             = messages[messageNumber];

                // We do not know the encoding of the full message - and the parts could be differently
                // encoded. Therefore we take a choice of simply using US-ASCII encoding on the raw bytes
                // to get the source code for the message. Any bytes not in th US-ASCII encoding, will then be
                // turned into question marks "?"
                ShowSourceForm sourceForm = new ShowSourceForm(Encoding.ASCII.GetString(m.RawMessage));
                sourceForm.ShowDialog();
            }
        }
Esempio n. 25
0
        public void ToMailMessageHasSubject()
        {
            const string message =
                "Subject: Testing\r\n" +
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);

            string subject = mailMessage.Subject;

            Assert.NotNull(subject);
            Assert.AreEqual("Testing", subject);
        }
Esempio n. 26
0
        public void ToMailMessageHasSubjectWithIso88599Encoding()
        {
            const string message =
                "Subject: Test =?ISO-8859-9?Q?=FE?=\r\n" +
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);

            string subject = mailMessage.Subject;

            Assert.NotNull(subject);
            Assert.AreEqual("Test ş", subject);
        }
Esempio n. 27
0
        public void ToMailMessageSimpleHasCorrectReplyTo()
        {
            const string message =
                "Reply-To: Test <*****@*****.**>\r\n" +
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);

            MailAddress replyTo = mailMessage.ReplyTo;

            Assert.NotNull(replyTo);
            Assert.AreEqual("Test", replyTo.DisplayName);
            Assert.AreEqual("*****@*****.**", replyTo.Address);
        }
Esempio n. 28
0
        public void ToMailMessageSimpleHasCorrectFrom()
        {
            const string message =
                "From: Test <*****@*****.**>\r\n" +
                "\r\n" +                 // Headers end
                "Test";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(message)).ToMailMessage();

            Assert.NotNull(mailMessage);

            MailAddress from = mailMessage.From;

            Assert.NotNull(from);
            Assert.AreEqual("Test", from.DisplayName);
            Assert.AreEqual("*****@*****.**", from.Address);
        }
Esempio n. 29
0
        public void ToMailMessageMultipleBodiesWithHtmlContent()
        {
            const string expectedPlain    = "This is the body of the message - in plain text.";
            const string expectedHtml     = "This is some <b>HTML</b> :)";
            const string multipartMessage =
                "MIME-Version: 1.0\r\n" +
                "Content-Type: multipart/alternative; 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" +
                expectedPlain +
                "\r\n" +
                "--frontier\r\n" +
                "Content-Type: text/html\r\n" +
                "\r\n" +
                expectedHtml +
                "\r\n" +
                "--frontier--";

            MailMessage mailMessage = new OPMessage(Encoding.ASCII.GetBytes(multipartMessage)).ToMailMessage();

            Assert.NotNull(mailMessage);

            // Check body for html
            Assert.NotNull(mailMessage.Body);
            Assert.AreEqual(expectedHtml, mailMessage.Body);
            Assert.AreEqual(Encoding.ASCII, mailMessage.BodyEncoding);
            Assert.IsTrue(mailMessage.IsBodyHtml);

            // Check alternative view for plain text
            Assert.IsNotEmpty(mailMessage.AlternateViews);
            Assert.AreEqual(1, mailMessage.AlternateViews.Count);

            AlternateView firstAlternative = mailMessage.AlternateViews[0];

            Assert.NotNull(firstAlternative.ContentType);
            Assert.NotNull(firstAlternative.ContentType.MediaType);
            Assert.AreEqual("text/plain", firstAlternative.ContentType.MediaType);
            Assert.AreEqual(expectedPlain, Encoding.ASCII.GetString(getAttachmentBytes(firstAlternative)));

            // Check attachments
            Assert.IsEmpty(mailMessage.Attachments);
        }
Esempio n. 30
0
        /// <summary>
        /// Example showing:
        ///  - how to find a MessagePart with a specified MediaType
        ///  - how to get the body of a MessagePart as a string
        /// </summary>
        /// <param name="message">The message to examine for xml</param>
        public static void FindXmlInMessage(OPMessage message)
        {
            MessagePart xml = message.FindFirstMessagePartWithMediaType("text/xml");

            if (xml != null)
            {
                // Get out the XML string from the email
                string xmlString = xml.GetBodyAsText();

                System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

                // Load in the XML read from the email
                doc.LoadXml(xmlString);

                // Save the xml to the filesystem
                doc.Save("test.xml");
            }
        }