VS.NET 2010/2012 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

Fetching only Unread Messages

by Dave 17. February 2011 07:39

Today I had a request from Darren about how to read unread messages from an IMAP server. This is an excellent question. I should have added something like this to the help file.

To fetch unread messages, there are a number of classes we need to take advantage of in aspNetIMAP.

They are:
IMAP4 -- the object used to manage the connection to the IMAP server
MailFolder -- the object used to get access to the individual folder
SearchClient -- the object used to search for unread messages
FetchClient -- the object that actually fetches the content of the messages.

Without further delay, below is some sample code that can get you started for downloading unread messages.

//configure and connect to the IMAP server
IMAP4 imap = new IMAP4( "mail.blah.com", "dave@blah.com", "test" );
imap.Logger = new IMAPLog( "c:\\imap.log", false);
imap.Logger.Overwrite = true;
imap.Login();

//get the inbox
MailFolder inbox = imap.SelectInbox();

//get a list of message ids that are unread
SearchClient sc = inbox.SearchClient;

//return results as UniqueIds
sc.IndexType = IndexType.UniqueId;
string messageSet = inbox.SearchClient.NotSeenSet();

if( (messageSet == null ) || (messageSet.Length == 0 ) )
	return;

char[] commaArray = new Char[] {','};

//trim any ending commas
if( messageSet.EndsWith(",") )
	messageSet = messageSet.Trim( commaArray );

//split into individual message ids, so we can process 1 at a time.
string[] ids = messageSet.Split( commaArray);

//get an instance to the FetchClient
FetchClient fc = inbox.FetchClient;
foreach( string id in ids )
{
	if( id.Length == 0 )
		continue;

	int uid = int.Parse(id);

	//download the message
	MimeMessage msg = fc.Message( uid, IndexType.UniqueId, true );

	//process according to business rules
	ProcessMessage( msg );
}

imap.Disconnect();

To keep the code short, it is left up to the user to implement exception handling.

As always, if anyone has any questions, comments or requests, please let me know.

Thanks!
Dave Wanta



 

 

Testimonial

I just wanted to pass along how much we TRULY appreciate your responsiveness when it comes to improving your products based on feedback. We have been making use of your components for almost 7 years now and I have yet to deal with a third-party supplier that has been as customer-oriented as you are. I hope that Advanced Intellect does very well, as it certainly deserves to! I always have (and will continue to) highly recommend your components to our partners and clients. "

D. Plaskon | Affirmative

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