The following example demonstrates streaming an attachment to the browser.
If an attachment is found, the attachment is checked to see if it is an image. If it is an image, then it is streamed directly to the client and should directly show up in the browser.
This is accomplished by the code
[C#]
Response.ContentType = attachment.ContentType.Value;
attachment.WriteToStream( Response.OutputStream );
[Visual Basic]
Response.ContentType = attachment.ContentType.Value
attachment.WriteToStream(Response.OutputStream)
If the attachment isn't an image, the "Save File As" dialog is presented to the client. This is achieved by the following code:
[C#]
Response.ContentType = "APPLICATION/OCTET-STREAM";
//set the filename
Response.AddHeader( "Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"" );
attachment.WriteToStream( Response.OutputStream );
[Visual Basic]
'set the Content-Type as an attachment
Response.ContentType = "APPLICATION/OCTET-STREAM"
'set the filename
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"")
attachment.WriteToStream(Response.OutputStream)
The complete ASP.NET .aspx pages can be found below for both C# and Visual
Basic. Please note that although these are ASP.NET pages, there ISN'T any HTML
content in the pages. This is because the pages are actually 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 (the attachment) to the browser
private void Page_Load(object sender, System.EventArgs e)
{
//an email on the filesystem
string filename = "testEmail.eml";
//this creates a MimeMessage from a stream
aspNetMime.MimeMessage msg = aspNetMime.MimeMessage.ParseFile( Server.MapPath( filename ) );
//send the first attachment to the browser
if( msg.Attachments.Count > 0 )
{
//get the first attachment
MimePart attachment = msg.Attachments[0];
//check to see if the attachment has a Content-Type, and if the Content-Type is an image
//if it is an image, then stream it directly to the brower.
if( ( attachment.ContentType != null ) && ( attachment.ContentType.Value.IndexOf( "image" )>-1 ) )
{
Response.ContentType = attachment.ContentType.Value;
attachment.WriteToStream( Response.OutputStream );
}
else
{
//this method should open the "Save File As" dialog box in the browser
//set the Content-Type as an attachment
Response.ContentType = "APPLICATION/OCTET-STREAM";
//set the filename
Response.AddHeader( "Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"" );
attachment.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 (the attachment) 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"
'this creates a MimeMessage from a file
Dim msg As aspNetMime.MimeMessage = aspNetMime.MimeMessage.ParseFile( Server.MapPath( filename ) )
'send the first attachment to the browser
If msg.Attachments.Count > 0 Then
'get the first attachment
Dim attachment As MimePart = msg.Attachments(0)
'check to see if the attachment has a Content-Type, and if the Content-Type is an image
'if it is an image, then stream it directly to the brower.
If (Not attachment.ContentType Is Nothing) AND (attachment.ContentType.Value.IndexOf("image")>-1) Then
Response.ContentType = attachment.ContentType.Value
attachment.WriteToStream(Response.OutputStream)
Else
'this method should open the "Save File As" dialog box in the browser
'set the Content-Type as an attachment
Response.ContentType = "APPLICATION/OCTET-STREAM"
'set the filename
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + attachment.AttachmentName() + "\"")
attachment.WriteToStream(Response.OutputStream)
End If
End If
End Sub
</script>