Check for unread messages

I am looking for a way to check the number of unread emails on an email account. Any tips?

EDIT: as described in tags for C #. Since I found out that IMAP is the way to go, and I confirmed that all the email accounts that I am going to use are activated by IMAP :)

+4
source share
3 answers

Pop

You can use OpenPOP.net to read emails using the POP protocol. The problem with POP is that it does not contain details whether it was unread or not. Therefore, I think that it will not be very useful for you. You have your own way of downloading and marking emails as read or unread.

IMAP

This SO question contains some links for examples using IMAP. IMAP contains mail status information (read / unread).

Please explain more about your requirement.

+4
source

Here is a sample code with the LumiSoft IMAP library:

using LumiSoft.Net.IMAP; using LumiSoft.Net.IMAP.Client; using LumiSoft.Net; 

...

 using (IMAP_Client client = new IMAP_Client()) { client.Connect("imap.gmail.com", 993, true); client.Login(" your.username@gmail.com ", "your_cool_password"); client.SelectFolder("INBOX"); IMAP_SequenceSet sequence = new IMAP_SequenceSet(); //sequence.Parse("*:1"); // from first to last IMAP_Client_FetchHandler fetchHandler = new IMAP_Client_FetchHandler(); fetchHandler.NextMessage += new EventHandler(delegate(object s, EventArgs e) { Console.WriteLine("next message"); }); fetchHandler.Envelope += new EventHandler<EventArgs<IMAP_Envelope>>(delegate(object s, EventArgs<IMAP_Envelope> e){ IMAP_Envelope envelope = e.Value; if (envelope.From != null && !String.IsNullOrWhiteSpace(envelope.Subject)) { Console.WriteLine(envelope.Subject); } }); // the best way to find unread emails is to perform server search int[] unseen_ids = client.Search(false, "UTF-8", "unseen"); Console.WriteLine("unseen count: " + unseen_ids.Count().ToString()); // now we need to initiate our sequence of messages to be fetched sequence.Parse(string.Join(",", unseen_ids)); // fetch messages now client.Fetch(false, sequence, new IMAP_Fetch_DataItem[] { new IMAP_Fetch_DataItem_Envelope() }, fetchHandler); // uncomment this line to mark messages as read // client.StoreMessageFlags(false, sequence, IMAP_Flags_SetType.Add, IMAP_MessageFlags.Seen); } 

The bit is complicated, but works great. The Limisoft library is not perfect, so make sure you have tested it well.

+1
source

If what you want to do is get the number of unread messages in the IMAP folder, you can use MailKit to do this:

 using MailKit; using MailKit.Search; using MailKit.Net.Imap; 

...

 using (var client = new ImapClient ()) { // Note: depending on your server, you might need to connect // on port 993 using SecureSocketOptions.SslOnConnect client.Connect ("imap.server.com", 143, SecureSocketOptions.StartTls); // Note: use your real username/password here... client.Authenticate ("username", "password"); // open the Inbox folder... client.Inbox.Open (FolderAccess.ReadOnly); // search the folder for new messages (aka recently // delivered messages that have not been read yet) var uids = client.Inbox.Search (SearchQuery.New); Console.WriteLine ("You have {0} new message(s).", uids.Count); // ...but maybe you mean unread messages? if so, use this query uids = client.Inbox.Search (SearchQuery.NotSeen); Console.WriteLine ("You have {0} unread message(s).", uids.Count); client.Disconnect (true); } 
0
source

All Articles