Delete emails with POP3

This article describes how to delete email messages using Mail.dll .NET POP3 component and POP3 protocol.

Internally deleted messages are only marked for deletion. Email messages are actually deleted by POP3 server after the client issues successful QUIT command – in other words: when client disconnects.

Following code will find unique ids of all messages and delete them one by one.

// C# version

using Limilabs.Client.POP3;

class Program
{
    static void Main(string[] args)
    {
        using (Pop3 pop3 = new Pop3())
        {
            pop3.Connect("pop3.company.com");    // use ConnectSSL for SSL.
            pop3.Login("user", "password");

            // Delete all messages
            foreach (string uid in pop3.GetAll())
            {
                pop3.DeleteMessageByUID(uid);
            }

            pop3.Close();
        }
    }
};
' VB.NET version

Imports Limilabs.Client.POP3

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

        Using pop3 As New Pop3()
            pop3.Connect("pop3.company.com")  ' use ConnectSSL for SSL.
            pop3.Login("user", "password")

            ' Delete all messages
            For Each uid As String In pop3.GetAll()
                pop3.DeleteMessageByUID(uid)
            Next

            pop3.Close()
        End Using

    End Sub
End Module

Here you can find Gmail POP3 behavior regarding downloading and deleting email messages.

Tags:    

Questions?

Consider using our Q&A forum for asking questions.

4 Responses to “Delete emails with POP3”

  1. Peter Steiness Says:

    Hello Limilabs

    On some servers, the method “pop3.DeleteMessageByUID (uid);” does not work.
    There are no error messages, but nothing happens, the mail is still there.

    What can be causing this problem?

    Sincerely,
    Peter Steiness

  2. Limilabs support Says:

    @Peter Steiness

    1.
    Remember to use Close method, as DeleteMessageByUID only marks message for deletion. Message is actually deleted after successful close command is received by the server.

    2.
    Some servers behave differently than the standard describes. For example Gmail:
    http://www.limilabs.com/blog/gmail-pop3-behavior

  3. cezar alejandro Says:

    This is my code:

    pop3.Connect("pop3.live.com") ' use ConnectSSL for SSL.

    whenever I try to connect says “An error occurred during the connection attempt because the connected party did not properly respond after a period of time, or there was an error in the connection established because connected host has failed to respond 157.55.1.215:110”

  4. Limilabs support Says:

    @Cezar

    You must use SSL when connecting to Hotmail servers: http://www.limilabs.com/blog/hotmail-imap-pop3-smtp-settings

    pop3.ConnectSSL("pop3.live.com");