public void TestTwo() 
		{
			EmailMessage emailmessage=new EmailMessage();
	
			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
	
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());	
	
			emailmessage.Subject="A photo of hawthornes";
	
			emailmessage.TextPart=new TextAttachment("This photo requires a better email reader.");		
			emailmessage.HtmlPart=new HtmlAttachment("<html><body>"+
				"<p>Note to self: look at this photo again in 30 years.</p>"+
				"<p><img src=\"cid:hawthornes\" alt=\"Hawthorne bush\"/></p>"+
				"<p>-Marcel</p>");
			
			FileInfo relatedfileinfo=new FileInfo(@"..\..\TestFiles\grover.jpg");
	
			FileAttachment relatedfileattachment=new FileAttachment(relatedfileinfo,"hawthornes");
	
			relatedfileattachment.ContentType="image/jpeg";
			
			emailmessage.AddRelatedAttachment(relatedfileattachment);
			
			SmtpServer smtpserver=TestAddressHelper.GetSmtpServer();
			emailmessage.Send(smtpserver);
			//Assert.IsNull(smtpserver.GetSmtpConversation());
		}
		private FileAttachment GetAttachmentFromBinaryReader(FileInfo fileinfo) 
		{
			FileStream filestream = fileinfo.OpenRead();
	
			BinaryReader binaryreader=new BinaryReader(filestream);
			

			FileAttachment fileAttachment=new FileAttachment(binaryreader);
			
			//fileAttachment.ContentType
			fileAttachment.Encoding=DotNetOpenMail.Encoding.EncodingType.Base64;
			fileAttachment.FileName="acrobattestpage.pdf";
			fileAttachment.ContentType="application/pdf";
			return fileAttachment;
		}
		private FileAttachment GetAttachmentFromByteArray(FileInfo fileinfo) 
		{
			FileStream filestream = fileinfo.OpenRead();

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

			FileAttachment fileAttachment=new FileAttachment(bytes);
			fileAttachment.Encoding=DotNetOpenMail.Encoding.EncodingType.Base64;
			fileAttachment.FileName="acrobattestpage.pdf";
			fileAttachment.ContentType="application/pdf";
			return fileAttachment;

		}
		private static FileAttachment CreateImageAttachment(string image, string ext, string contentId)
		{
			string path = System.Web.HttpContext.Current.Server.MapPath(image);
			
			if (!System.IO.File.Exists(path))
				return null;

			FileInfo file = new FileInfo(path);
			FileAttachment attachment = new FileAttachment(file, contentId);
			attachment.ContentType = "image/" + ext.ToLower().Replace("jpg", "jpeg");
            
			return attachment;
		}
Beispiel #5
0
 /// <summary>
 /// Add an image which is referred to from another part of
 /// the email (probably the HTML attachment).  You should
 /// set the ContentID of the file attachment before passing
 /// it in.
 /// </summary>
 /// <param name="fileattachment">The file attachment</param>
 public void AddRelatedAttachment(FileAttachment fileattachment)
 {
     _relatedfileattachments.Add(fileattachment);
 }
Beispiel #6
0
 /// <summary>
 /// Add an attachment which will appear to the user as
 /// a separate file.  (It is not referred to in the email itself.)
 /// </summary>
 /// <param name="fileattachment">The file attachment</param>
 public void AddMixedAttachment(FileAttachment fileattachment)
 {
     _mixedfileattachments.Add(fileattachment);
 }
Beispiel #7
0
 /// <summary>
 /// Add an image which is referred to from another part of
 /// the email (probably the HTML attachment).  You should
 /// set the ContentID of the file attachment before passing
 /// it in.
 /// </summary>
 /// <param name="fileattachment">The file attachment</param>
 public void AddRelatedAttachment(FileAttachment fileattachment)
 {
     _relatedfileattachments.Add(fileattachment);
 }
Beispiel #8
0
 /// <summary>
 /// Add an attachment which will appear to the user as
 /// a separate file.  (It is not referred to in the email itself.)
 /// </summary>
 /// <param name="fileattachment">The file attachment</param>
 public void AddMixedAttachment(FileAttachment fileattachment)
 {
     _mixedfileattachments.Add(fileattachment);
 }
		public void TestRelatedAttachments() 
		{

			EmailMessage emailmessage=new EmailMessage();

			emailmessage.FromAddress=TestAddressHelper.GetFromAddress();
			emailmessage.AddToAddress(TestAddressHelper.GetToAddress());
			emailmessage.Subject="EmailMessageTests Test Related 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\">\r\n<strong>HTML</strong><br>\r\npart.</body></html>");
			FileInfo relatedfileinfo=new FileInfo(@"..\..\TestFiles\grover.jpg");
			Assert.IsTrue(relatedfileinfo.Exists);
			FileAttachment relatedfileattachment=new FileAttachment(relatedfileinfo,"mycontentid");
			relatedfileattachment.ContentType="image/jpeg";			
			emailmessage.AddRelatedAttachment(relatedfileattachment);
			//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 relatedHeaderComesFirst=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 (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;
					if (!hasMultipartAlternative && !hasMixedHeader) 
					{
						relatedHeaderComesFirst=true;
					}
				}
				//log.Debug("Line "+i+": "+line);				
			}
			Assert.IsFalse(hasMixedHeader, "Should not have a  multipart/mixed header");
			Assert.IsTrue(hasMimeVersion, "Missing Mime Version header");
			Assert.IsTrue(relatedHeaderComesFirst, "The related 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);


		}
		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());		
		}
		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);
		}
		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());		
		}
		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());		
		}
		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());
	
		}