Using the OpenPOP .net client for getting messages from Gmail.
I'm won开发者_如何学Pythondering how I can get only the new messages?
Currently, I get the atom feed and then get as many emails as the feed has with the OpenPOP client (starting from the first).
GmailAtomFeed feed = new GmailAtomFeed("user", "pass");
feed.GetFeed();
int unread = feed.FeedEntries.Count;
POPClient client = new POPClient("pop.gmail.com", 995, "user", "pass", AuthenticationMethod.USERPASS, true);
for (int i = 0; i < unread; i++)
{
Message m = client.GetMessage(i, false);
// ...
}
Is there a better way to do this?
And how do I set the unread messages to be read?
I doubt you can do it with pop3. From my understanding POP3 doesn't support the notion of the unread\unseen email. It should be up to the client to track messages which were already shown to the user and which were not.
What you can do is switch to using IMAP protocol to access gmail. Check this link for how you can switch it on for your gmail account Getting started with IMAP for Gmail.
Now, if you're using c# there are some commercial libraries for IMAP and there free\opensource ones: like this one on codeproject: IMAP Client library using C#. What you have to do to get unseen messages is to specify "unseen" flag for the select command. Here's an example
You have to store the UIDL
of each email in a local database. When you want to check for new mail, you retrieve the UIDL
s on the server and see if you have if already in your local database; if not, it's a new mail.
Outlook uses the same strategy.
same Q How to retrieve only new emails using POP3 protocol
精彩评论