コード例 #1
0
		public void TestRelatedAndMixedAttachments() 
		{
			EmailMessage emailmessage=new EmailMessage();

			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());
			emailmessage.Subject="EmailMessageTests Test HTML+Text+Graphic";
			emailmessage.TextPart=new TextAttachment("This\r\nis the\r\ntext\r\npart.");
			emailmessage.HtmlPart=new HtmlAttachment("<html><body>This<br>  \r\nis the<br><img src=\"cid:mycontentid@rhombus\">  \r\n<strong>HTML</strong><br>\r\npart.</body></html>");
			FileInfo relatedfileinfo=new FileInfo(@"..\..\TestFiles\grover.jpg");
			FileInfo mixedfileinfo=new FileInfo(@"..\..\TestFiles\groverUpsideDown.jpg");
			Assert.IsTrue(relatedfileinfo.Exists);
			Assert.IsTrue(mixedfileinfo.Exists);
			FileAttachment relatedfileattachment=new FileAttachment(relatedfileinfo,"mycontentid@rhombus");
			relatedfileattachment.ContentType="image/jpeg";
			FileAttachment mixedfileattachment=new FileAttachment(mixedfileinfo);
			mixedfileattachment.ContentType="image/jpeg";
			emailmessage.AddRelatedAttachment(relatedfileattachment);
			emailmessage.AddMixedAttachment(mixedfileattachment);
			//emailmessage.Send(_smtpserver);
			String content=emailmessage.ToDataString();
			StringReader sr=new StringReader(content);
			
			int i=0;
			String line=null;
			bool hasMimeVersion=false;
			bool hasMixedHeader=false;
			bool mixedHeaderComesFirst=false;
			bool hasMultipartAlternative=false;
			bool hasPlainText=false;
			bool hasHtmlText=false;
			bool hasAttachment=false;
			bool hasRelatedHeader=false;
			int quotedPrintableParts=0;

			String expectedToAddress=TestAddressHelper.GetToAddress().Name+" <"+TestAddressHelper.GetToAddress().Email+">";
			//log.Debug("To Address is "+expectedToAddress);
			while ((line=sr.ReadLine())!=null) 
			{
				i++;
				if (line.IndexOf("Content-Type: multipart/mixed")==0) 
				{
					hasMixedHeader=true;
					if (!hasMultipartAlternative && !hasRelatedHeader) 
					{
						mixedHeaderComesFirst=true;
					}
				}
				if (line.IndexOf("MIME-Version: 1.0")==0) 
				{
					hasMimeVersion=true;
				}
				if (line.IndexOf("Content-Type: multipart/alternative;")==0) 
				{
					hasMultipartAlternative=true;
				}
				if (line.IndexOf("Content-Type: text/html")==0) 
				{
					hasHtmlText=true;
				}
				if (line.IndexOf("Content-Type: text/plain")==0) 
				{
					hasPlainText=true;
				}
				if (line.IndexOf("Content-Type: image/jpeg")==0) 
				{
					hasAttachment=true;
				}
				if (line.IndexOf("Content-Transfer-Encoding: quoted-printable")==0) 
				{
					quotedPrintableParts++;
				}
				if (line.IndexOf("/9j/4AAQSkZJRgABAQEASABIAAD/")==0) 
				{
					hasAttachment=true;
				}
				if (line.IndexOf("Content-Type: multipart/related")>=0)
				{
					hasRelatedHeader=true;
				}
				//log.Debug("Line "+i+": "+line);				
			}
			Assert.IsTrue(hasMixedHeader, "Missing multipart/mixed header");
			Assert.IsTrue(hasMimeVersion, "Missing Mime Version header");
			Assert.IsTrue(mixedHeaderComesFirst, "The mixed header should come first");
			Assert.IsTrue(hasMultipartAlternative, "Missing Mime Multipart/Alternative setting");
			Assert.IsTrue(hasPlainText, "Missing Plain Text");
			Assert.IsTrue(hasHtmlText, "Missing HTML");
			Assert.IsTrue(hasAttachment, "Missing the base64 Attachment");
			Assert.IsTrue(hasRelatedHeader, "Missing the related header.");
			Assert.AreEqual(2, quotedPrintableParts, "Expected 2 quoted printable declarations, found "+quotedPrintableParts);

		}
コード例 #2
0
		public void TestFileFromStream() 
		{
			EmailMessage emailmessage=new EmailMessage();
	
			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
	
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());	
	
			emailmessage.Subject="Here's your license";
	
			emailmessage.TextPart=new TextAttachment("This is a license.\r\n\r\n"+
				"We will spend your money on a new plasma TV.");
		
			emailmessage.HtmlPart=new HtmlAttachment("<html><body>"+
				"<p>This is a license.</p>\r\n"+
				"<p>We will spend your money on a new <i>plasma TV</i>.</p>\r\n"+
				"</body></html>");
		
			MemoryStream stream=new MemoryStream();
			StreamWriter sw=new StreamWriter(stream);
			sw.WriteLine("this is some test data 1");
			sw.WriteLine("this is some test data 2");
			sw.WriteLine("this is some test data 3");
			sw.WriteLine("this is some test data 4");
			sw.Flush();
			stream.Seek(0, SeekOrigin.Begin);

			//BinaryReader br=new BinaryReader(stream);

			FileAttachment fileAttachment=new FileAttachment(new StreamReader(stream));
			
			//fileAttachment.ContentType
			fileAttachment.FileName="License.txt";
			fileAttachment.CharSet=System.Text.Encoding.ASCII;
			fileAttachment.ContentType="text/plain";
			emailmessage.AddMixedAttachment(fileAttachment);
			
			//emailmessage.Send(TestAddressHelper.GetSmtpServer());		
		}
コード例 #3
0
		public void TestDocFile()
		{
			EmailMessage mail = new EmailMessage();

			FileInfo fileinfo=new FileInfo(@"..\..\TestFiles\TestWord.doc");		
			Assert.IsTrue(fileinfo.Exists);

			FileAttachment fileAttachment = new FileAttachment(fileinfo);
			fileAttachment.ContentType = "application/msword";
			
			//EmailAddress emailAddress = new EmailAddress(emailAddressParam); 
			mail.TextPart=new TextAttachment("Here is your file");
			mail.AddMixedAttachment(fileAttachment);

			mail.FromAddress=TestAddressHelper.GetFromAddress();
			mail.AddToAddress("*****@*****.**");	

			SmtpServer smtpServer = TestAddressHelper.GetSmtpServer();

			mail.Send(smtpServer);
		}
コード例 #4
0
		public void TestLargerBinaryFileFromStream() 
		{

			String filename="casingTheJoint.jpg";

			EmailMessage emailmessage=new EmailMessage();
	
			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
	
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());	
	
			emailmessage.Subject="Here's your file";
	
			emailmessage.TextPart=new TextAttachment("This a zip file.");
		
			emailmessage.HtmlPart=new HtmlAttachment("<html><body>"+
				"<p>This a zip file.</p>\r\n");

			FileInfo fileinfo=new FileInfo(@"..\..\TestFiles\"+filename);		
			FileStream filestream = fileinfo.OpenRead();

			MemoryStream stream=new MemoryStream();

			StreamWriter sw=new StreamWriter(stream);

			sw.Flush();

			//BinaryReader br=new BinaryReader(stream);

			BinaryReader br=new BinaryReader(filestream);
			byte[] bytes=br.ReadBytes((int) fileinfo.Length);
			br.Close();

			FileAttachment fileAttachment=new FileAttachment(bytes);
			
			//fileAttachment.ContentType
			fileAttachment.FileName=filename;
			fileAttachment.ContentType="application/zip";
			emailmessage.AddMixedAttachment(fileAttachment);
			
			emailmessage.Send(TestAddressHelper.GetSmtpServer());		
		}
コード例 #5
0
		public void TestFileFromString() 
		{
			EmailMessage emailmessage=new EmailMessage();
	
			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
	
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());	
	
			emailmessage.Subject="Here's your license";
	
			emailmessage.TextPart=new TextAttachment("This is a license.\r\n\r\n"+
				"We will spend your money on a new plasma TV.");
		
			emailmessage.HtmlPart=new HtmlAttachment("<html><body>"+
				"<p>This is a license.</p>\r\n"+
				"<p>We will spend your money on a new <i>plasma TV</i>.</p>\r\n"+
				"</body></html>");
		
			String content="This is String Line 1\r\nThis is String Line 2";

			FileAttachment fileAttachment=new FileAttachment(content);
			
			//fileAttachment.ContentType
			fileAttachment.FileName="License.txt";
			fileAttachment.CharSet=System.Text.Encoding.ASCII;
			fileAttachment.ContentType="text/plain";
			fileAttachment.Encoding=DotNetOpenMail.Encoding.EncodingType.SevenBit;
			emailmessage.AddMixedAttachment(fileAttachment);
			
			//emailmessage.Send(TestAddressHelper.GetSmtpServer());		
		}
コード例 #6
0
		public void TestThree() 
		{
			EmailMessage emailmessage=new EmailMessage();
	
			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
	
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());
	
			emailmessage.Subject="Something has come up";
	
			emailmessage.TextPart=new TextAttachment("I regret that something has "+
				"come up unexpectedly,\r\n"+
				"and I must postpone our meeting.\r\n\r\n"+	
				"Please read the 20 pages of my thoughts on this in the attached\r\n"+
				"PDF file.\r\n\r\n-Marcel");
		
			emailmessage.HtmlPart=new HtmlAttachment("<p>I regret that something "+
				"has come up unexpectedly,\r\n"+
				"and I must postpone our meeting.</p>\r\n"+	
				"<p>Please read the 20 pages of my thoughts on this in the attached\r\n"+
				"PDF file.</p>\r\n<p>-Marcel</p>");
			
			FileAttachment fileattachment=new FileAttachment(new FileInfo(@"..\..\TestFiles\grover.jpg"));
	
			fileattachment.ContentType="image/jpeg";
			
			emailmessage.AddMixedAttachment(fileattachment);
		
			emailmessage.Send(TestAddressHelper.GetSmtpServer());
	
		}