Receive unseen emails using POP3

If you want to receive unseen emails only, you must know, that this is not possible using POP3 protocol. There is no way to mark which message was received by the client application (such as Mail.dll POP3 component) on the POP3 server. POP3 servers simply don’t store such information.

You have to mark which message has been read by yourself on the client application.

UIDL command is very helpful in achieving this goal. UIDL command returns unique-id for every message in the mailbox. You can save this id (in a custom file for example) for every message you have received. You can learn more about unique IDs used in POP3 protocol.

The following example downloads email messages from POP3 server and parses them. You should provide your own WasReceived method implementation which checks if specified id was already processed and stores this id if it wasn’t.

// C# version

using System;
using Limilabs.Mail;
using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("pop3.example.com");
            pop3.Login("user", "password");

            MailBuilder builder = new MailBuilder();
            foreach (string uid in pop3.GetAll())
            {
                Console.WriteLine("Message unique-id: {0};", uid);

                if (WasReceived(uid) == true)
                   continue;

                IMail email = builder.CreateFromEml(
                    pop3.GetMessageByUID(uid));

                Console.WriteLine(email.Subject);
                Console.WriteLine(email.Text);
            }
            pop3.Close();
        }
    }

    private static bool WasReceived(string uid)
    {
        // Here you should check if this uid was already received.
        // Store this uid for future reference if it wasn't.
        throw new NotImplementedException();
    }
}
' VB.NET version


Imports System
Imports Limilabs.Mail
Imports Limilabs.Client.POP3

Public Module Module1
    Public Sub Main(ByVal args As String())

        Using pop3 As New Pop3()
            pop3.Connect("pop3.example.com")
            pop3.Login("user", "password")

            Dim builder As New MailBuilder()
            For Each uid As String In pop3.GetAll()
                Console.WriteLine("Message unique-id: {0};", uid)

                if (WasReceived(uid) == true)
                   continue;

                Dim email As IMail = builder.CreateFromEml(pop3.GetMessageByUID(uid))

                Console.WriteLine(email.Subject)
                Console.WriteLine(email.Text)
            Next
            pop3.Close()
        End Using

    End Sub


    Private Shared Function WasReceived(uid As String) As Boolean
        ' Here you should check if this uid was already received.
        ' Store this uid for future reference if it wasn't.
	Throw New NotImplementedException()
    End Function
End Module

Tags:    

Questions?

Consider using our Q&A forum for asking questions.