0

Now i'm just getting all the messages in a backgroundworker dowork event:

private int numberofallmessages = 0; private int countMsg = 0;

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        List<string> uids = new List<string>();
        using (var client = new Pop3Client())
        {
            client.Connect(textServer.Text, Convert.ToInt32(textPort.Text), ssl);
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.Authenticate(textUser.Text, textPassword.Text);

            for (int i = client.Count -1; i > 0; i--)
            {
                if (backgroundWorker1.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }
                if (i > 0)
                {
                    allMessages.Add(client.GetMessage(i));
                }
                    int nProgress = (client.Count - i + 1) * 100 / client.Count;
                    backgroundWorker1.ReportProgress(nProgress, client.Count.ToString() + "/" + i);
            }

            client.Disconnect(true);
        }
   }

I'm getting all the messages about 6000. Now i want to have in a button click event a code so each time i click on the button it will check for a new email. Same idea like in outlook for example.

And also maybe to use an event that will automatic give a notice when a new emails arraived.

I saw this code example:

MailKit E_MailKit_MailFolder_CountChanged

But first i'm not sure if this is what i need/want in my case and second in the example they are using imap but in my code i'm using pop3 and also my account i'm using in the backgroundworker in the Authenticate is a pop3 account.

I want that once after i got all messages like 6000 from now on when clicking the button it will check/get only new emails and not all over again the 6000 emails.

Daniel Lipman
  • 41
  • 1
  • 8
  • I know on pop3 you can't check on the server for new emails. But maybe there is a way to do it as client ? Maybe somehow with the id's of the mails ? In outlook for example you click on a button and it's checking pop3 for new emails very fast it's not downloading/getting all the messages each time. – Daniel Lipman May 20 '16 at 07:12

1 Answers1

1

What you will need to do is keep track of the UIDs of the messages that you've already downloaded. Unfortunately, not all POP3 servers support UIDs...

Assuming yours does, here's how you would do it:

private HashSet<string> downloaded = new HashSet<string> ();

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    using (var client = new Pop3Client())
    {
        client.Connect(textServer.Text, Convert.ToInt32(textPort.Text), ssl);
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.Authenticate(textUser.Text, textPassword.Text);

        var uids = client.GetMessageUids ();

        for (int i = 0; i < client.Count; i++)
        {
            if (backgroundWorker1.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            if (!downloaded.Contains (uids[i]))
            {
                allMessages.Add(client.GetMessage(i));
                downloaded.Add (uids[i]);
            }

            int nProgress = (client.Count - i + 1) * 100 / client.Count;
            backgroundWorker1.ReportProgress(nProgress, client.Count.ToString() + "/" + i);
        }

        client.Disconnect(true);
    }

}

Obviously, this will only work as long as the program stays open. Once you close the program and re-run it, the downloaded variable will be cleared and it will re-download everything again, so you'll need to add logic to save and restore that variable between runs (assuming you care).

The way to check if a POP3 server supports UIDs is:

if (client.Capabilities.HasFlag (Pop3Capabilities.UIDL))
{
    // UIDs are supported
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • This part is working fine. What i did before using openpop was to save the emails as emails files format to the hard disk and also save to a text file all the messages uids and when running the program ovr again each time i was reading the emails back loading them and also the uids and when trying to download a new emails i compared them with the uids list from the text file and only those not exist uids i downloaded. Logic or it should be in other way ? – Daniel Lipman May 20 '16 at 13:20