public static string MessageGenerate(Message msg) { if ((msg) == null) { Assert.Fail(); } string ret = msg.Generate(); Assert.AreEqual(2, EncodingTest.IsGoodAsciiMessageFormat(ret, false, ""), ret); return ret; }
public void TestGetDate() { var msg = new Message(); int[] date; msg.SetHeader("date", "Sat, 1 Jan 2000 12:34:56 +1034"); date = msg.GetDate(); Assert.AreEqual(2000, date[0]); Assert.AreEqual(1, date[1]); Assert.AreEqual(1, date[2]); Assert.AreEqual(12, date[3]); Assert.AreEqual(34, date[4]); Assert.AreEqual(56, date[5]); Assert.AreEqual(0, date[6]); Assert.AreEqual(10 * 60 + 34, date[7]); msg.SetHeader("date", "Mon, 1 Jan 1900 23:59:60 -1034"); date = msg.GetDate(); Assert.AreEqual(1900, date[0]); Assert.AreEqual(1, date[1]); Assert.AreEqual(1, date[2]); Assert.AreEqual(23, date[3]); Assert.AreEqual(59, date[4]); Assert.AreEqual(60, date[5]); Assert.AreEqual(0, date[6]); Assert.AreEqual(-(10 * 60 + 34), date[7]); msg.SetHeader("date", "Sun, 1 Jan 2000 12:34:56 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "1 Jan 2000 12:34:56 +1034"); date = msg.GetDate(); Assert.AreEqual(2000, date[0]); Assert.AreEqual(1, date[1]); Assert.AreEqual(1, date[2]); msg.SetHeader("date", "32 Jan 2000 12:34:56 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "30 Feb 2000 12:34:56 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "1 Feb 999999999999999999999 12:34:56 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "1 Jan 2000 24:34:56 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "1 Jan 2000 01:60:56 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "1 Jan 2000 01:01:61 +1034"); if (msg.GetDate() != null) { Assert.Fail(); } msg.SetHeader("date", "1 Jan 2000 01:01:01 +1099"); if (msg.GetDate() != null) { Assert.Fail(); } }
public void TestContentType() { var msg = new Message().SetTextBody("text"); msg.ContentType = MediaType.Parse("text/html"); try { msg.ContentType = null; Assert.Fail("Should have failed"); } catch (ArgumentNullException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } }
public void TestContentHeadersOnlyInBodyParts() { var msg = new Message().SetTextAndHtml("Hello", "Hello"); msg.SetHeader("x-test", "test"); msg.Parts[0].SetHeader("x-test", "test"); { string stringTemp = msg.GetHeader("x-test"); Assert.AreEqual( "test", stringTemp); } { string stringTemp = msg.Parts[0].GetHeader("x-test"); Assert.AreEqual( "test", stringTemp); } msg = MessageFromString(msg.Generate()); { string stringTemp = msg.GetHeader("x-test"); Assert.AreEqual( "test", stringTemp); } Assert.AreEqual(null, msg.Parts[0].GetHeader("x-test")); }
public void TestBodyString() { var msg = new Message().SetTextBody("Test"); string mtype = "text/plain;charset=x-unknown"; msg.ContentType = MediaType.Parse(mtype); try { msg.BodyString.ToString(); Assert.Fail("Should have failed"); } catch (NotSupportedException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } }
internal static Message MessageFromString(string valueStr) { var msgobj = new Message( DataUtilities.GetUtf8Bytes( valueStr, true)); MessageGenerate(msgobj); return msgobj; }
internal static bool HasNestedMessageType(Message message) { if (message.ContentType.TopLevelType.Equals("message")) { return (!message.ContentType.SubType.Equals("global")) && ((!message.ContentType.SubType.Equals("global-headers")) && (message.ContentType.SubType.Equals("global-delivery-status") || message.ContentType.SubType.Equals("global-disposition-notification"))); } foreach (Message part in message.Parts) { if (HasNestedMessageType(part)) { return true; } } return false; }
public void TestSubject() { var msg = new Message(); msg.Subject = "Test"; Assert.AreEqual("Test", msg.Subject); msg.Subject = "Test2"; Assert.AreEqual("Test2", msg.Subject); }
public void TestSetTextBody() { var msg = new Message(); try { msg.SetTextBody(null); Assert.Fail("Should have failed"); } catch (ArgumentNullException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } }
public void TestSetHeader() { try { new Message().SetHeader("from", "\"a\r\nb\" <*****@*****.**>"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"a\rb\" <*****@*****.**>"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"a\r b\" <*****@*****.**>"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"a\r\n b\" <*****@*****.**"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"a\r\n b\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "=?utf-8?q?=01?= <*****@*****.**"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "=?utf-8?q?=01?= <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"Me\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"a\nb\" <*****@*****.**>"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("from", "\"a\0b\" <*****@*****.**>"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } const string valueStr = "From: [email protected]\r\nX-Header: 1\r\n\r\nTest"; Message msg = MessageFromString(valueStr); try { msg.SetHeader(2, "X-Header2", "2"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetHeader(2, "2"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetHeader(1, (string)null); Assert.Fail("Should have failed"); } catch (ArgumentNullException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } { string stringTemp = new Message() .SetHeader("comments", "subject") .SetHeader("subject", "my subject") .GetHeader("subject"); Assert.AreEqual( "my subject", stringTemp); } try { new Message().SetHeader(EncodingTest.Repeat("a", 998), "x"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("e:d", "x"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("e d", "x"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("e\u007f", "x"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("e\u00a0", "x"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader("e\u0008", "x"); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } string longHeader = EncodingTest.Repeat("a", 997); try { MessageGenerate(new Message().SetHeader(longHeader, "x")); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } var headerNames = new string[] { "from", "to", "cc", "bcc", "reply-to" }; foreach (string headerName in headerNames) { try { new Message().SetHeader(headerName, "\"Me\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader(headerName, "\"Me Me\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader(headerName, "\"Me(x)\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader(headerName, "\"Me\u002c Me\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { new Message().SetHeader(headerName, "\"Me\u002c Me(x)\" <*****@*****.**>"); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } } }
public void TestSetDate() { var msg = new Message(); try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, 0, 0, 0 }); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(null); Assert.Fail("Should have failed"); } catch (ArgumentNullException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { -1, 1, 1, 0, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 0, 1, 0, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 13, 1, 0, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 0, 0, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 32, 0, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, -5, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 24, 0, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, -1, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 60, 0, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, -1, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, 61, 0, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, 0, -1, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, 0, 1000, 0 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, 0, 0, -1440 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } try { msg.SetDate(new int[] { 2000, 1, 1, 0, 0, 0, 0, 1440 }); Assert.Fail("Should have failed"); } catch (ArgumentException) { new Object(); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } }
public static string MessageGenerate(Message msg) { if (msg == null) { Assert.Fail(); } string ret = msg.Generate(); { object objectTemp = 2; object objectTemp2 = EncodingTest.IsGoodAsciiMessageFormat( ret, false, String.Empty); string messageTemp = ret; Assert.AreEqual(objectTemp, objectTemp2, messageTemp); } return ret; }
/// <include file='../../docs.xml' /// path='docs/doc[@name="M:PeterO.Mail.Message.SetTextAndHtml(System.String,System.String)"]/*'/> public Message SetTextAndHtml(string text, string html) { if (text == null) { throw new ArgumentNullException("text"); } if (html == null) { throw new ArgumentNullException("html"); } // The spec for multipart/alternative (RFC 2046) says that // the fanciest version of the message should go last (in // this case, the HTML version) var textMessage = new Message().SetTextBody(text); var htmlMessage = new Message().SetHtmlBody(html); this.contentType = MediaType.Parse("multipart/alternative; boundary=\"=_Boundary00000000\""); IList<Message> messageParts = this.Parts; messageParts.Clear(); messageParts.Add(textMessage); messageParts.Add(htmlMessage); return this; }
public MessageStackEntry(Message msg) { #if DEBUG if (msg == null) { throw new ArgumentNullException("msg"); } #endif this.message = msg; string newBoundary = String.Empty; MediaType mediaType = msg.ContentType; if (mediaType.IsMultipart) { newBoundary = mediaType.GetParameter("boundary"); if (newBoundary == null) { throw new MessageDataException("Multipart message has no boundary defined"); } if (!IsWellFormedBoundary(newBoundary)) { throw new MessageDataException("Multipart message has an invalid boundary defined: " + newBoundary); } } this.boundary = newBoundary; }
private void ReadMultipartBody(IByteReader stream) { int baseTransferEncoding = this.transferEncoding; var boundaryChecker = new BoundaryCheckerTransform(stream); // Be liberal on the preamble and epilogue of multipart // messages, as they will be ignored. IByteReader currentTransform = MakeTransferEncoding( boundaryChecker, baseTransferEncoding, true); IList<MessageStackEntry> multipartStack = new List<MessageStackEntry>(); var entry = new MessageStackEntry(this); multipartStack.Add(entry); boundaryChecker.PushBoundary(entry.Boundary); Message leaf = null; var buffer = new byte[8192]; var bufferCount = 0; int bufferLength = buffer.Length; this.body = new byte[0]; var aw = new ArrayWriter(); { while (true) { var ch = 0; try { ch = currentTransform.ReadByte(); } catch (MessageDataException ex) { string valueExMessage = ex.Message; #if DEBUG aw.Write(buffer, 0, bufferCount); buffer = aw.ToArray(); string ss = DataUtilities.GetUtf8String( buffer, Math.Max(buffer.Length - 35, 0), Math.Min(buffer.Length, 35), true); ss = String.Empty; string transferEnc = (leaf ?? this) .GetHeader("content-transfer-encoding"); valueExMessage += " [" + ss + "] [type=" + ((leaf ?? this).ContentType ?? MediaType.TextPlainAscii) + "] [encoding=" + transferEnc + "]"; valueExMessage = valueExMessage.Replace('\r', ' ') .Replace('\n', ' ').Replace('\0', ' '); #endif throw new MessageDataException(valueExMessage); } if (ch < 0) { if (boundaryChecker.HasNewBodyPart) { var msg = new Message(); int stackCount = boundaryChecker.BoundaryCount(); // Pop entries if needed to match the stack #if DEBUG if (multipartStack.Count < stackCount) { throw new ArgumentException("multipartStack.Count (" + multipartStack.Count + ") is less than " + stackCount); } #endif if (leaf != null) { if (bufferCount > 0) { aw.Write(buffer, 0, bufferCount); } leaf.body = aw.ToArray(); // Clear for the next body aw.Clear(); bufferCount = 0; } else { // Clear for the next body bufferCount = 0; aw.Clear(); } while (multipartStack.Count > stackCount) { multipartStack.RemoveAt(stackCount); } Message parentMessage = multipartStack[multipartStack.Count - 1].Message; boundaryChecker.StartBodyPartHeaders(); MediaType ctype = parentMessage.ContentType; bool parentIsDigest = ctype.SubType.Equals("digest") && ctype.IsMultipart; ReadHeaders(stream, msg.headers, false); msg.ProcessHeaders(true, parentIsDigest); entry = new MessageStackEntry(msg); // Add the body part to the multipart // message's list of parts parentMessage.Parts.Add(msg); multipartStack.Add(entry); aw.Clear(); ctype = msg.ContentType; leaf = ctype.IsMultipart ? null : msg; boundaryChecker.PushBoundary(entry.Boundary); boundaryChecker.EndBodyPartHeaders(); currentTransform = MakeTransferEncoding( boundaryChecker, msg.transferEncoding, ctype.TypeAndSubType.Equals("text/plain")); } else { // All body parts were read if (leaf != null) { if (bufferCount > 0) { aw.Write(buffer, 0, bufferCount); bufferCount = 0; } leaf.body = aw.ToArray(); } return; } } else { buffer[bufferCount++] = (byte)ch; if (bufferCount >= bufferLength) { aw.Write(buffer, 0, bufferCount); bufferCount = 0; } } } } }