VS.NET 2010 (and above) Users Click Here   

HOMECONTACT PRODUCTS DOWNLOADS PURCHASE TESTIMONIALS FORUMS COMPANY CONTACT
Home
Products
Downloads
Purchase
Licensing
Licensing FAQ
Software Updates
Support Forums
Testimonials
Feature Requests
Guarantee
About Us
Contact Us
Hosting Companies
Privacy Policy
   
Shopping Cart


Search

aspNetIMAP: Fetching Headers, Size, Importance and Flags

by Dave 8. March 2011 00:58

I recently had a request about fetching different parts of a message from an IMAP server.  They wanted to get the following information as easily as possible:

Flags
Message Size
Importance
Attachment Names
Customer Headers
Recipients

The IMAP protocol does not support fetching this information in a single network call. However, all of these values can be fetched for a single message, using multiple calls. aspNetIMAP can do this for you in a nice, neat object called the MimeEnvelope object.  The MimeEnvelope object fetches all of the above data, and more.

Below is a code example that demonstrates using the MimeEnvelope object.

C#

	 IMAP4 imap = new IMAP4();
	 imap.Server = "192.168.1.4";
	 imap.Username = "dave@example.com";
	 imap.Password = "test";
	 imap.Login();
	 
	 MailFolder mf = imap.SelectInbox();       
	 
	 FetchClient fc = mf.FetchClient;
	 
	 MimeEnvelope[] menvelopes = fc.MimeEnvelope( "1:*", IndexType.Ordinal );
	 
	 
	 foreach( MimeEnvelope menvelope in menvelopes)
	 {
	 	//write out the attachment names
	 	Response.Write( "Attachment Count: " + menvelope.AttachmentNames.Length );
	 	if( menvelope.AttachmentNames.Length > 0 )
	 	{
	 		foreach( string attachmentName in menvelope.AttachmentNames )
	 		{
	 			Response.Write( attachmentName );
	 		}
	 	}
	 
	 	//write out the From, To, CC address information
	 	Response.Write( menvelope.FromName );
	 	Response.Write( menvelope.FromAddress );
	 	//To addresses
	 	foreach( string emailAddress in menvelope.To )
	 	{
	 		string name = menvelope.To[ emailAddress ];
	 		Response.Write( name );
	 		Response.Write( emailAddress );
	 	}
	 	//CC Addresses
	 	foreach( string emailAddress in menvelope.CC )
	 	{
	 		string name = menvelope.CC[ emailAddress ];
	 		Response.Write( name );
	 		Response.Write( emailAddress );
	 
	 	}
	 
	 	//write out other information
	 	Response.Write( "UniqueId: " + menvelope.UniqueId.ToString() );
	 	Response.Write( "Size: " + menvelope.Size.ToString() );
	 	Response.Write( "Flags: "+ menvelope.Flags.ToString() );
	 	Response.Write( "InternalDate: " + menvelope.InternalDate.ToString( "f",  DateTimeFormatInfo.InvariantInfo) );
	 	Response.Write( "Date: " + menvelope.Date.ToString( "f",  DateTimeFormatInfo.InvariantInfo) );
	 
	 	//subject
	 	Response.Write( menvelope.Subject );
	 	
	 	//priority
	 	Response.Write( "Priority: " + menvelope.Priority.ToString() );
	 
	 	Response.Write( "IsAnswered: " + menvelope.IsAnswered().ToString() );
	 	Response.Write( "IsDrafted: " + menvelope.IsDrafted().ToString() );
	 	Response.Write( "IsFlagged: " + menvelope.IsFlagged().ToString() );
	 	Response.Write( "IsRead: " + menvelope.IsRead().ToString() );
	 	Response.Write( "IsRecent: " + menvelope.IsRecent	().ToString() );
	 
	 	//retrieve a specific header named 'x-organization'
	 	Header h = menvelope.Headers[ "x-organization" ];
	 	if( ( h != null ) && ( h.Value != null ) )
	 	{
	 		Response.Write( h.Value );
	 	}
	 
	 	//write out the headers
	 	Response.Write( menvelope.HeaderString );
	 
	 	//write out the bodystructure of the message
	 	if( menvelope.BodyStructure != null )
	 	{
	 		Response.Write( menvelope.BodyStructure.ToString() );
	 	}
	 }
	 
	 
	 imap.Disconnect();
	 

VB.NET

	 Dim imap As New IMAP4()
	 imap.Server = "192.168.1.4"
	 imap.Username = "dave@example.com"
	 imap.Password = "test"
	 imap.Login()
	 
	 Dim mf As MailFolder = imap.SelectInbox()
	 
	 Dim fc As FetchClient = mf.FetchClient
	 
	 Dim menvelopes As MimeEnvelope() = fc.MimeEnvelope("1:*", IndexType.Ordinal)
	 
	 
	 Dim menvelope As MimeEnvelope
	 For Each menvelope In  menvelopes
	    'write out the attachment names
	    Response.Write(("Attachment Count: " + menvelope.AttachmentNames.Length))
	    If menvelope.AttachmentNames.Length > 0 Then
	       Dim attachmentName As String
	       For Each attachmentName In  menvelope.AttachmentNames
	          Response.Write(attachmentName)
	       Next attachmentName
	    End If
	    
	    'write out the From, To, CC address information
	    Response.Write(menvelope.FromName)
	    Response.Write(menvelope.FromAddress)
	    'To addresses
	    Dim emailAddress As String
	    For Each emailAddress In  menvelope.To
	       Dim name As String = menvelope.To(emailAddress)
	       Response.Write(name)
	       Response.Write(emailAddress)
	    Next emailAddress
	    'CC Addresses
	    Dim emailAddress As String
	    For Each emailAddress In  menvelope.CC
	       Dim name As String = menvelope.CC(emailAddress)
	       Response.Write(name)
	       Response.Write(emailAddress)
	    Next emailAddress 
	    
	    'write out other information
	    Response.Write(("UniqueId: " + menvelope.UniqueId.ToString()))
	    Response.Write(("Size: " + menvelope.Size.ToString()))
	    Response.Write(("Flags: " + menvelope.Flags.ToString()))
	    Response.Write(("InternalDate: " + menvelope.InternalDate.ToString("f", DateTimeFormatInfo.InvariantInfo)))
	    Response.Write(("Date: " + menvelope.Date.ToString("f", DateTimeFormatInfo.InvariantInfo)))
	    
	    'subject
	    Response.Write(menvelope.Subject)
	    
	    'priority
	    Response.Write(("Priority: " + menvelope.Priority.ToString()))
	    
	    Response.Write(("IsAnswered: " + menvelope.IsAnswered().ToString()))
	    Response.Write(("IsDrafted: " + menvelope.IsDrafted().ToString()))
	    Response.Write(("IsFlagged: " + menvelope.IsFlagged().ToString()))
	    Response.Write(("IsRead: " + menvelope.IsRead().ToString()))
	    Response.Write(("IsRecent: " + menvelope.IsRecent().ToString()))
	    
	    'retrieve a specific header named 'x-organization'
	    Dim h As Header = menvelope.Headers("x-organization")
	    If Not (h Is Nothing) And Not (h.Value Is Nothing) Then
	       Response.Write(h.Value)
	    End If
	    
	    'write out the headers
	    Response.Write(menvelope.HeaderString)
	    
	    'write out the bodystructure of the message
	    If Not (menvelope.BodyStructure Is Nothing) Then
	       Response.Write(menvelope.BodyStructure.ToString())
	    End If
	 Next menvelope
	 
	 
	 imap.Disconnect()

 

 

Testimonial

Could I get the upgrade please. Although to be perfectly honest I feel I should be buying another copy considering the great support I've got. "

Guy

Read more testimonials
ListNanny aspNetDNS aspNetEmail aspNetPOP3 aspNetMX IPMuncher aspNetMIME aspNetPING aspNetTraceRoute aspNetIMAP aspNetMHT