Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the unread/new messages from Gmail using POP3?

Tags:

c#

email

gmail

pop3

Using the OpenPOP .net client for getting messages from Gmail.

I'm wondering 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?

like image 627
Ashley Simpson Avatar asked Dec 05 '25 10:12

Ashley Simpson


2 Answers

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

like image 127
serge_gubenko Avatar answered Dec 06 '25 22:12

serge_gubenko


You have to store the UIDL of each email in a local database. When you want to check for new mail, you retrieve the UIDLs 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

like image 27
Mhmd Avatar answered Dec 07 '25 00:12

Mhmd