SMTPMessageTest.cs
上传用户:wdhx888
上传日期:2017-06-08
资源大小:112k
文件大小:8k
源码类别:

WEB邮件程序

开发平台:

C#

  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.Text;
  5. using System.Threading;
  6. using log4net;
  7. using log4net.Config;
  8. using NUnit.Framework;
  9. using EricDaugherty.CSES.Net;
  10. namespace EricDaugherty.CSES.SmtpServer
  11. {
  12. [TestFixture]
  13. public class SMTPMessageTest
  14. {
  15. #region Constants
  16. private static string ATTACHMENT_1_HEADER_DATA = 
  17. "Content-Type: application/pdf;rn" +
  18. " name="000000001.pdf"rn" +
  19. "Content-Transfer-Encoding: base64rn" +
  20. "Content-Disposition: attachment;rn" +
  21. " filename="000000001.pdf"rn" +
  22. "rn";
  23. private static string ATTACHMENT_1_BODY_DATA = 
  24. "JVBERi0xLjIKekdf1fnfSqQYt7AjczYfpmRSIEyEcx8KMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvrn" +
  25. "ZwovUGFnZXMgMyAwIFIKL091dGxpbmVzIDIgMCBSCj4+CmVuZG9iagoyIDAgb2JqCjw8Ci9UeXBlrn" +
  26. "IC9PdXRsaW5lcwovQ291bnQgMQovRmlyc3QgOCAwIFIKL0xhc3QgOCAwIFIKPj4KZW5kb2JqCjMgrn" +
  27. "MCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9Db3VudCAxCi9LaWRzIFsgNSAwIFIgXQo+PgplbmRvYmoKrn" +
  28. "NCAwIG9iagpbL1BERiAvVGV4dCAvSW1hZ2VCIF0KZW5kb2JqCjUgMCBvYmoKPDwKL1R5cGUgL1Bhrn" +
  29. "Z2UKL1BhcmVudCAzIDAgUgovUmVzb3VyY2VzIDw8Ci9YT2JqZWN0IDw8Ci9JTWNwZGYwIDcgMCBSrn" +
  30. "Cj4+Ci9Qcm9jU2V0IDQgMCBSID4+Ci9NZWRpYUJveCBbMCAwIDM5OSA1NjFdCi9Dcm9wQm94IFswrn" +
  31. "IDAgMzk5IDU2MV0KL1JvdGF0ZSAwCi9Db250ZW50cyA2IDAgUgo+PgplbmRvYmoKNiAwIG9iago8rn" +
  32. "PAovTGVuZ3RoIDQ3Ci9GaWx0ZXIgWy9GbGF0ZURlY29kZV0KPj4Kc3RyZWFtCnic4yrkMtQzAAIFrn" +
  33. "AxQKq2ByLpexpSU2GVMzXBr0PX2TC1LSDBRc8rkCuQCBJhXPCmVuZHN0cmVhbQplbmRvYmoKNyAwrn" +
  34. "IG9iago8PAovVHlwZSAvWE9iamVjdAovU3VidHlwZSAvSW1hZ2UKL05hbWUgL0lNY3BkZjAKL1dprn" +
  35. "ZHRoIDE2NjQKL0hlaWdodCAyMzM4Ci9GaWx0ZXIgL0ZsYXRlRGVjb2RlCi9CaXRzUGVyQ29tcG9urn" +
  36. "ZW50IDEKL0NvbG9yU3BhY2UgL0RldmljZUdyYXkKL0xlbmd0aCAyNTAxMgo+PgpzdHJlYW0KeJzsrn" +
  37. "vV1sJEl+4BfZ2epsnSjmyGvguBBV0cIKXr+Z47HhGlxtxQgyTjBwwD74xfCXejyG51FsD2xVQ6WKrn" +
  38. "rn";
  39. private string TEST_SINGLE_BASE64 = 
  40. "Received: from development02 (development02 [127.0.0.1])rn" +
  41. "     by adexs.com (Eric Daugherty's C# Email Server)rn" +
  42. "     4/16/2004 10:35:58 AMrn" +
  43. "From: "Eric Daugherty" <edaugherty@adexs.com>rn" +
  44. "To: <test@test.com>rn" +
  45. "Subject: CofAsrn" +
  46. "Date: Fri, 16 Apr 2004 10:35:55 -0500rn" +
  47. "Message-ID: <LIEBLGPCEJMNGHIPABGIGEABCAAA.edaugherty@adexs.com>rn" +
  48. "MIME-Version: 1.0rn" +
  49. "Content-Type: multipart/mixed;rn" +
  50. " boundary="----=_NextPart_000_0000_01C4239E.999350F0"rn" +
  51. "X-Priority: 3 (Normal)rn" +
  52. "X-MSMail-Priority: Normalrn" +
  53. "X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0)rn" +
  54. "Importance: Normalrn" +
  55. "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409rn" +
  56. "rn" +
  57. "This is a multi-part message in MIME format.rn" +
  58. "rn" +
  59. "------=_NextPart_000_0000_01C4239E.999350F0rn" +
  60. ATTACHMENT_1_HEADER_DATA +
  61. ATTACHMENT_1_BODY_DATA +
  62. "------=_NextPart_000_0000_01C4239E.999350F0--rn" +
  63. "rn";
  64. private string TEST_DOUBLE_BASE64 = 
  65. "Received: from development02 (development02 [127.0.0.1])rn" +
  66. "     by adexs.com (Eric Daugherty's C# Email Server)rn" +
  67. "     4/16/2004 10:35:58 AMrn" +
  68. "From: "Eric Daugherty" <edaugherty@adexs.com>rn" +
  69. "To: <test@test.com>rn" +
  70. "Subject: CofArn" +
  71. "Date: Fri, 16 Apr 2004 10:35:55 -0500rn" +
  72. "Message-ID: <LIEBLGPCEJMNGHIPABGIGEABCAAA.edaugherty@adexs.com>rn" +
  73. "MIME-Version: 1.0rn" +
  74. "Content-Type: multipart/mixed;rn" +
  75. " boundary="----=_NextPart_000_0000_01C4239E.999350F0"rn" +
  76. "X-Priority: 3 (Normal)rn" +
  77. "X-MSMail-Priority: Normalrn" +
  78. "X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0)rn" +
  79. "Importance: Normalrn" +
  80. "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409rn" +
  81. "rn" +
  82. "This is a multi-part message in MIME format.rn" +
  83. "rn" +
  84. "------=_NextPart_000_0000_01C4239E.999350F0rn" +
  85. ATTACHMENT_1_HEADER_DATA +
  86. ATTACHMENT_1_BODY_DATA +
  87. "------=_NextPart_000_0000_01C4239E.999350F0rn" +
  88. "Content-Type: application/pdf;rn" +
  89. " name="test.pdf"rn" +
  90. "Content-Transfer-Encoding: 7bitrn" +
  91. "Content-Disposition: attachment;rn" +
  92. " filename="test.pdf"rn" +
  93. "rn" +
  94. "PAovTGVuZ3RoIDQ3Ci9GaWx0ZXIgWy9GbGF0ZURlY29kZV0KPj4Kc3RyZWFtCnic4yrkMtQzAAIFrn" +
  95. "AxQKq2ByLpexpSU2GVMzXBr0PX2TC1LSDBRc8rkCuQCBJhXPCmVuZHN0cmVhbQplbmRvYmoKNyAwrn" +
  96. "IG9iago8PAovVHlwZSAvWE9iamVjdAovU3VidHlwZSAvSW1hZ2UKL05hbWUgL0lNY3BkZjAKL1dprn" +
  97. "rn" +
  98. "------=_NextPart_000_0000_01C4239E.999350F0--rn" +
  99. "rn";
  100. private string TEST_BODY_BASE64 = 
  101. "Received: from development02 (development02 [127.0.0.1])rn" +
  102. "     by adexs.com (Eric Daugherty's C# Email Server)rn" +
  103. "     4/22/2004 4:36:14 PMrn" +
  104. "From: "Eric Daugherty" <edaugherty@adexs.com>rn" +
  105. "To: <cc_1000@test.com>rn" +
  106. "Subject: CofAsrn" +
  107. "Date: Thu, 22 Apr 2004 16:36:14 -0500rn" +
  108. "Message-ID: <LIEBLGPCEJMNGHIPABGIKEAHCAAA.edaugherty@adexs.com>rn" +
  109. "MIME-Version: 1.0rn" +
  110. "Content-Type: application/pdf;rn" +
  111. " name="000000002.pdf"rn" +
  112. "Content-Transfer-Encoding: base64rn" +
  113. "Content-Disposition: attachment;rn" +
  114. " filename="000000002.pdf"rn" +
  115. "X-Priority: 3 (Normal)rn" +
  116. "X-MSMail-Priority: Normalrn" +
  117. "X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0)rn" +
  118. "Importance: Normalrn" +
  119. "X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409rn" +
  120. "" +
  121. ATTACHMENT_1_BODY_DATA;
  122. #endregion
  123. #region SetUp/TearDown
  124. [SetUp]
  125. public void Setup()
  126. {
  127. }
  128. [TearDown]
  129. public void Teardown()
  130. {
  131. }
  132. #endregion
  133. #region Tests
  134. [Test]
  135. public void MessageHeaders()
  136. {
  137. SMTPMessage message = new SMTPMessage();
  138. message.AddData( TEST_SINGLE_BASE64 );
  139. Assertion.AssertEquals( "Received", "from development02 (development02 [127.0.0.1]) by adexs.com (Eric Daugherty's C# Email Server) 4/16/2004 10:35:58 AM", message.Headers["Received"] );
  140. Assertion.AssertEquals( "From", ""Eric Daugherty" <edaugherty@adexs.com>", message.Headers["From" ] );
  141. Assertion.AssertEquals( "Subject", "CofAs", message.Headers["Subject"] );
  142. Assertion.AssertEquals( "Date", "Fri, 16 Apr 2004 10:35:55 -0500", message.Headers["Date"] );
  143. Assertion.AssertEquals( "X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2800.1409", message.Headers["X-MimeOLE"] );
  144. }
  145. [Test]
  146. public void SingleBase64Attachment()
  147. {
  148. SMTPMessage message = new SMTPMessage();
  149. message.AddData( TEST_SINGLE_BASE64 );
  150.   
  151.   SMTPMessagePart[] messageParts = message.MessageParts;
  152. Assertion.AssertEquals( "AttachmentCount", 1, messageParts.Length );
  153. Assertion.AssertEquals( "AttachmentHeaderData", ATTACHMENT_1_HEADER_DATA, messageParts[0].HeaderData );
  154. Assertion.AssertEquals( "AttachmentBodyData", ATTACHMENT_1_BODY_DATA, messageParts[0].BodyData );
  155. Assertion.AssertEquals( "AttachmentContentType", "application/pdf; name="000000001.pdf"", messageParts[0].Headers["Content-Type"] );
  156. Assertion.AssertEquals( "AttachmentContentDisposition", "attachment; filename="000000001.pdf"", messageParts[0].Headers["Content-Disposition"] );
  157. }
  158. [Test]
  159. public void DoubleBase64Attachment()
  160. {
  161. SMTPMessage message = new SMTPMessage();
  162. message.AddData( TEST_DOUBLE_BASE64 );
  163.   
  164.   SMTPMessagePart[] messageParts = message.MessageParts;
  165. Assertion.AssertEquals( "AttachmentCount", 2, messageParts.Length );
  166. Assertion.AssertEquals( "AttachmentHeaderData1", ATTACHMENT_1_HEADER_DATA, messageParts[0].HeaderData );
  167. Assertion.AssertEquals( "AttachmentBodyData1", ATTACHMENT_1_BODY_DATA, messageParts[0].BodyData );
  168. Assertion.AssertEquals( "AttachmentContentType1", "application/pdf; name="000000001.pdf"", messageParts[0].Headers["Content-Type"] );
  169. Assertion.AssertEquals( "AttachmentContentDisposition1", "attachment; filename="000000001.pdf"", messageParts[0].Headers["Content-Disposition"] );
  170. }
  171. [Test]
  172. public void BodyBase64()
  173. {
  174. SMTPMessage message = new SMTPMessage();
  175. message.AddData( TEST_BODY_BASE64 );
  176.   
  177.   SMTPMessagePart[] messageParts = message.MessageParts;
  178.   
  179. Assertion.AssertEquals( "AttachmentCount", 0, messageParts.Length );
  180. Assertion.AssertEquals( "ContentDisposition", "attachment; filename="000000002.pdf"", message.Headers["Content-Disposition"] );
  181. }
  182. #endregion
  183. }
  184. }