예제 #1
0
		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();
		}
예제 #2
0
		public void TestSaveAndLoadComplex()
		{
			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);
			}

			FileInfo testFile = new FileInfo("test_message_save_.testFile");
			message.Save(testFile);

			OPMessage message2 = OPMessage.Load(testFile);
			{
				System.Collections.Generic.List<MessagePart> parts2 = message2.FindAllMessagePartsWithMediaType("multipart/mixed");

				Assert.NotNull(parts2);
				Assert.IsNotEmpty(parts2);
				Assert.AreEqual(2, parts2.Count);

				MessagePart firstPart2 = parts2[0];
				Assert.IsTrue(firstPart2.IsMultiPart);
				Assert.AreEqual("multipart/mixed", firstPart2.ContentType.MediaType);
				Assert.AreEqual("simple boundary", firstPart2.ContentType.Boundary);

				MessagePart secondPart2 = parts2[1];
				Assert.IsTrue(secondPart2.IsMultiPart);
				Assert.AreEqual("multipart/mixed", secondPart2.ContentType.MediaType);
				Assert.AreEqual("anotherBoundary", secondPart2.ContentType.Boundary);
			}

			testFile.Delete();
		}
예제 #3
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;
		}