What is aspNetMime?
|
|
aspNetMime is a server component. It requires the Microsoft .NET
runtime in order to function properly. aspNetMime is used to programmatically
query MIME messages for different
specific information found in the message. Some of this information may be
Headers, X-Headers, Body, attachments, or embedded images.
|
|
Who needs aspNetMime? |
|
Anyone who needs to check email, in a programmatic manner, can use aspNetMime. Some examples of aspNetMime
in use are:
- bounce back cleaners
- removing NDRs from a server
- looping through the SMTP Service directories found on Windows servers
|
|
What is the difference between MIME, POP3 and SMTP? |
|
MIME, POP3 and SMTP are all email related protocols. POP3 and SMTP are actually transport protocols.
POP3 is used for retrieving email, while SMTP is used for sending email. MIME is a specification for formatting
the email message itself, not for transporting the message.
|
|
What do I need to run aspNetMime? |
|
aspNetMime is a low overhead, highly optimized assembly, that only needs the Microsoft .NET (or equivalent)
framework to run. Any system that can support the Microsoft.NET framework will be able to use aspNetMime.
aspNetMime can be used from any environment supported by .NET, which can
include winforms, ASP.NET, console applications, components, or web services.
|
|
How do I use aspNetMime?
|
|
aspNetMime can only be used programmatically from a .NET environment, including, but not limited to, ASP.NET, winforms, console applications, and web services.
Here are two brief examples for using aspNetMime from C# and Visual Basic. This example reads an email message from the filesystem and loops through the message headres.
For more examples, click here.
[C#]
//an email on the filesystem
string filename = "testEmail.eml";
//open the email
FileStream fs = File.Open( filename, FileMode.Open );
//this creates a Message from a stream
aspNetMime.Message msg = new aspNetMime.Message( fs );
//close the stream
fs.Close();
foreach( Header h in msg.Headers )
{
Console.WriteLine( "Name -- " + h.Name );
Console.WriteLine( "Value -- " + h.Value );
Console.WriteLine( "Complete Value (includes parameters)
-- " + h.ValueComplete );
Console.WriteLine( "The raw header -- " + h.RawValue );
}
Console.WriteLine( "done..." );
Console.ReadLine();
[Visual Basic]
'an email on the filesystem
Dim filename As String = "testEmail.eml"
'open the email
Dim fs As FileStream = File.Open(filename,FileMode.Open)
'this creates a Message from a stream
Dim msg As aspNetMime.Message = New aspNetMime.Message(fs)
'close the stream
fs.Close()
Dim h As Header
For Each h In msg.Headers
Console.WriteLine("Name -- " + h.Name)
Console.WriteLine("Value -- " + h.Value)
Console.WriteLine("Complete Value (includes parameters)
-- " + h.ValueComplete)
Console.WriteLine("The raw header -- " + h.RawValue)
Next
Console.WriteLine("done...")
Console.ReadLine()
|
|
How do I access the X-Headers of a message?
How do I load an email from a file?
|
|
aspNetMime makes it easy to load an email from a file and access X-Headers. The following example demonstrates this technique, in both C# and Visual Basic.
[C#]
//an email on the filesystem
string filename = "testEmail.eml";
//open the email
FileStream fs = File.Open( filename, FileMode.Open );
//this creates a Message from a stream
aspNetMime.Message msg = new aspNetMime.Message( fs );
//close the stream
fs.Close();
//access different headers
Header h = msg.GetHeader( "Status" );
//check for a status header
if( h != null )
Console.WriteLine( h.RawValue );
//check for a X-header "X-Organization"
//the GetXHeader() method will find headers that start with X-
h = msg.GetXHeader( "Organization ");
if( h!= null )
Console.WriteLine( h.RawValue );
//check for X-OriginalArrivalTime
h = msg.GetXHeader( "OriginalArrivalTime" );
if( h != null )
Console.WriteLine( h.RawValue );
Console.WriteLine( "done..." );
Console.ReadLine();
[Visual Basic]
'an email on the filesystem
Dim filename As String = "testEmail.eml"
'open the email
Dim fs As FileStream = File.Open(filename,FileMode.Open)
'this creates a Message from a stream
Dim msg As aspNetMime.Message = New aspNetMime.Message(fs)
'close the stream
fs.Close()
'access different headers
Dim h As Header = msg.GetHeader("Status")
'check for a status header
If Not h Is Nothing Then
Console.WriteLine(h.RawValue)
End If
'check for a X-header "X-Organization"
'the GetXHeader() method will find headers that start with X-
h = msg.GetXHeader("Organization ")
If Not h Is Nothing Then
Console.WriteLine(h.RawValue)
End If
'check for X-OriginalArrivalTime
h = msg.GetXHeader("OriginalArrivalTime")
If Not h Is Nothing Then
Console.WriteLine(h.RawValue)
End If
Console.WriteLine("done...")
Console.ReadLine()
|
|
How can I get a listing of attachment names found in a MIME message?
|
|
To check if the aspNetMime Message has an attachment, obtain a reference to the property 'Attachments', which is a MimePartCollection. With this collection, a complete listing of names can be obtained.
See the following examples in both C# and Visual Basic.
[C#]
//an email on the filesystem
string filename = "testEmail.eml";
//open the email
StreamReader sr = new StreamReader( filename );
//read the stream into some text
string mytext = sr.ReadToEnd();
//close the stream
sr.Close();
//this creates a Message from a stream
aspNetMime.Message msg = new aspNetMime.Message( myte
//get the attachments and loop through them
MimePartCollection attachments = msg.Attachments;
foreach( MimePart attachment in attachments )
{
Console.WriteLine( attachment.AttachmentName() );
}
Console.WriteLine( "done..." );
Console.ReadLine();
[Visual Basic]
'an email on the filesystem
Dim filename As String = "testEmail.eml"
'open the email
Dim sr As StreamReader = New StreamReader(filename)
'read the stream into some text
Dim mytext As String = sr.ReadToEnd()
'close the stream
sr.Close()
'this creates a Message from a stream
aspNetMime.Message msg = New aspNetMime.Message(myte
'get the attachments and loop through them
Dim attachments As MimePartCollection = msg.Attachments
Dim attachment As MimePart
For Each attachment In attachments
Console.WriteLine(attachment.AttachmentName())
Next
Console.WriteLine("done...")
Console.ReadLine()
|
|
How can I stream an attachment to the browser?
|
|
aspNetMime has the very powerful method called WriteToStream() on the MimePart object. With this method, it is easy to write
MimePart data to any stream, including, but not limited to Network streams, ASP.NET streams, and file system stream.
The following example writes data to an ASP.NET stream.
[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
//binary content (the attachment) 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.Message msg = new aspNetMime.Message( emailContents );
//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>
[Visual Basic]
<%@ 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
'binary 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"
'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.Message = New aspNetMime.Message(emailContents)
'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>
|
|
How can I access the HTML part of an email?
|
|
aspNetMime provides a property on the Message object called HtmlMimePart.
From this property the CharSet and text can be accessed. For example:
[C#]
//an email on the filesystem
string filename = "testEmail.eml";
//open the email
FileStream fs = File.Open( filename, FileMode.Open );
//this creates a Message from a stream
aspNetMime.Message msg = new aspNetMime.Message( fs );
fs.Close();
MimePartCollection attachments = msg.Attachments;
foreach( MimePart mp in attachments )
{
Console.WriteLine( mp.AttachmentName() );
}
Console.WriteLine( "done..." );
Console.ReadLine();
[Visual Basic]
'an email on the filesystem
Dim filename As String = "testEmail.eml"
'open the email
Dim fs As FileStream = File.Open(filename, FileMode.Open)
'this creates a Message from a stream
Dim msg As New aspNetMime.Message(fs)
fs.Close()
Dim attachments As MimePartCollection = msg.Attachments
Dim mp As MimePart
For Each mp In attachments
Console.WriteLine(mp.AttachmentName())
Next mp
Console.WriteLine("done...")
Console.ReadLine()
|
|
I don't like aspNetMime, why didn't you make it better?
|
|
Tell us what we are doing wrong. We love to get feedback, both good and bad. If you have some specific points about aspNetMime, please tell us, so we can make a better product. Feel free to contact us at
support@advancedIntellect.com
|