Home | Demo | Contact Us | Download | Support | Purchase | Products


Other Products
 

Stream an Embedded Image to the Browser

The following example demonstrates streaming an embedded image from an ASP.NET page to the browser. This is a useful technique for building web based email clients.  Notice that although these are ASP.NET pages there ISN'T any HTML conent in the page. This is because the ASP.NET pages are streaming binary content to the browser.


[C#]

<%@ Page language="C#" %>
<%@ Import Namespace="aspNetMime"%>
<%@ Import Namespace="System.IO"%>

<script runat=server>
		
		//notice that even though this is an ASP.NET page there ISN'T any HTML code on this page.
		//that's because this page is meant to send binay content to the browser
 
				
		private void Page_Load(object sender, System.EventArgs e)
		{

			//an email on the filesystem
			string filename = "testEmail.eml";

			//open the email
			StreamReader sr = new StreamReader( Server.MapPath( filename ) );
			string emailContents = sr.ReadToEnd();
			sr.Close();

			//this creates a Message from a stream
			aspNetMime.MimeMessage msg = new aspNetMime.MimeMessage( emailContents );

			//send the embedded part to the browser named 'My-Company-Logo'
			MimePart embeddedPart = msg.GetEmbeddedPart("My-Company-Logo" );

			if( embeddedPart != null )
			{
				//set the content type
				if( embeddedPart.ContentType != null )
					Response.ContentType = embeddedPart.ContentType.Value;

				//stream the embedded part to the browser
				embeddedPart.WriteToStream( Response.OutputStream );
			}


		}
				

</script>

 

[VB.NET]

<%@ Page language="vb" %>
<%@ Import Namespace="aspNetMime"%>
<%@ Import Namespace="System.IO"%>

<script runat=server>
		
		'notice that even though this is an ASP.NET page there ISN'T any HTML code on this page.
		'that's because this page is meant to send binay content to the browser
 
 
		Private  Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 
			'an email on the filesystem
			Dim filename As String =  "testEmail.eml" 
 
			'open the email
			Dim sr As StreamReader =  New StreamReader(Server.MapPath(filename)) 
			Dim emailContents As String =  sr.ReadToEnd() 
			sr.Close()
 
			'this creates a Message from a stream
			Dim msg As aspNetMime.MimeMessage =  New aspNetMime.MimeMessage(emailContents) 
 
			'send the embedded part to the browser named 'My-Company-Logo'
			Dim embeddedPart As MimePart =  msg.GetEmbeddedPart("My-Company-Logo") 
 
			If Not embeddedPart Is Nothing Then
				'set the content type
				If Not embeddedPart.ContentType Is Nothing Then
					Response.ContentType = embeddedPart.ContentType.Value
				End If
 
				'stream the embedded part to the browser
				embeddedPart.WriteToStream(Response.OutputStream)
			End If
 
 
		End Sub

				

</script>

 

 

 



The box is not shipped. aspNetMime is a downloadable product.