DotNetOpenMail  

 

 • Home

 • Overview

 • Examples

 • More Examples

 • Documentation

 • Download

 • Project Home

 • Forums

 • Announcements

SourceForge.net Logo

Support This Project

Examples

 

A simple text/html email:

				
				
	EmailMessage emailMessage = new EmailMessage();
	
	emailMessage.FromAddress = new EmailAddress("cswann@XXXXXX.com", 
		"Charles Swann");
	
	emailMessage.AddToAddress(new EmailAddress("ocrecy@XXXXXX.com", 
		"Odette de Crecy"));	
	
	emailMessage.Subject = "Missed you";
	
	emailMessage.TextPart = new TextAttachment("Just checking where "+
	    "you were last night.\r\nSend me a note!\r\n\r\n-Charles");
		
	emailMessage.HtmlPart = new HtmlAttachment("<html><body>"+
		"<p>Just checking up on where you were last night.</p>\r\n"+
		"<p>Send me a note!</p>\r\n\r\n"+
		"<p>-Charles</p></body></html>");
		
	emailMessage.Send(new SmtpServer("localhost"));
	
				
 

An email with an inline graphic:

				
				
	EmailMessage emailMessage = new EmailMessage();
	
	emailMessage.FromAddress = new EmailAddress("marcel@XXXXXX.com");
	
	emailMessage.AddToAddress(new EmailAddress("marcel@XXXXXX.com"));	
	
	emailMessage.Subject = "A photo of hawthorns";
	
	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:hawthorns\" alt=\"Hawthorn bush\"/></p>"+
		"<p>-Marcel</p>");
			
	FileInfo relatedFileInfo = new FileInfo(@"somehawthorns.jpg");
	
	FileAttachment relatedFileAttachment = new FileAttachment(relatedFileInfo,
		"hawthorns");
	
	relatedFileAttachment.ContentType = "image/jpeg";
			
	emailMessage.AddRelatedAttachment(relatedFileAttachment);
		
	emailMessage.Send(new SmtpServer("localhost"));
	
				
 

An email with a PDF attachment:

				
				
	EmailMessage emailMessage = new EmailMessage();
	
	emailMessage.FromAddress = new EmailAddress("marcel@XXXXXX.com");
	
	emailMessage.AddToAddress(new EmailAddress("gilberte@XXXXXX.com"));	
	
	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></body><html>");
			
	FileAttachment fileAttachment = new FileAttachment(new FileInfo("mythoughts.pdf"));
	
	fileAttachment.ContentType = "application/pdf";
			
	emailMessage.AddMixedAttachment(fileAttachment);
		
	emailMessage.Send(new SmtpServer("localhost"));
	
				
 

An email with a text file created in memory:

				
				
	EmailMessage emailMessage = new EmailMessage();
	
	emailMessage.FromAddress = new EmailAddress("licensing@XXXXXX.com");
	
	emailMessage.AddToAddress(new EmailAddress("gleek@XXXXXX.com"));	
	
	emailMessage.Subject = "Here is your license";
	
	emailMessage.TextPart = new TextAttachment("Here is your"+
		"license file.\r\n");
		
	emailMessage.HtmlPart = new HtmlAttachment("<p>Here is your"+
		"license file.</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);

	FileAttachment fileAttachment = new FileAttachment(new StreamReader(stream));
			
	fileAttachment.FileName = "License.txt";
	fileAttachment.CharSet = System.Text.Encoding.ASCII;
	fileAttachment.ContentType = "text/plain";
	
	emailMessage.AddMixedAttachment(fileAttachment);
			
	emailMessage.Send(new SmtpServer("localhost"));	

	
				
 

Using SMTP AUTH (LOGIN):

				
				
	EmailMessage emailMessage = new EmailMessage();
	
	emailMessage.FromAddress = new EmailAddress("marcel@XXXXXX.com");
	
	emailMessage.AddToAddress(new EmailAddress("albertine@XXXXXX.com"));	
	
	emailMessage.Subject = "Something has come up";
		
	emailMessage.HtmlPart = new HtmlAttachment("<html><p>body<p>The greatness "+
		"of true art is that it rediscovers, holds, and communicates "+
		"to us that reality from which we live further and further away, 
		"as the conventional knowledge we substitute for it becomes heavier "+
		"and more impenetrable.\r\n</p>"+
		"<p>-Marcel</p></body><html>");
			
	SmtpServer smtpServer=new SmtpServer("localhost");
	
	smtpServer.SmtpAuthToken=new SmtpAuthToken("marcel", "madeleine");			
	
	emailMessage.Send(smtpServer);