Click or drag to resize
MimeKit

Pop3ClientGetMessageUids Method

Get the full list of available message UIDs.

Namespace: MailKit.Net.Pop3
Assembly: MailKit (in MailKit.dll) Version: 4.3.0
Syntax
C#
public override IList<string> GetMessageUids(
	CancellationToken cancellationToken = default
)

Parameters

cancellationToken  CancellationToken  (Optional)
The cancellation token.

Return Value

IListString
The message uids.

Implements

IMailSpoolGetMessageUids(CancellationToken)
IMailSpoolGetMessageUids(CancellationToken)
Exceptions
ExceptionCondition
ObjectDisposedException The Pop3Client has been disposed.
ServiceNotConnectedException The Pop3Client is not connected.
ServiceNotAuthenticatedException The Pop3Client is not authenticated.
NotSupportedException The POP3 server does not support the UIDL extension.
OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.
Pop3CommandException The POP3 command failed.
Pop3ProtocolException A POP3 protocol error occurred.
Remarks

Gets the full list of available message UIDs.

Caution note  Caution
Not all servers support UIDs, so you should first check the Capabilities property for the UIDL flag or the SupportsUids convenience property.
Example
C#
public static void DownloadNewMessages (HashSet<string> previouslyDownloadedUids)
{
    using (var client = new Pop3Client ()) {
        client.Connect ("pop.gmail.com", 995, SecureSocketOptions.SslOnConnect);

        client.Authenticate ("username", "password");

        if (!client.Capabilities.HasFlag (Pop3Capabilities.UIDL))
            throw new Exception ("The POP3 server does not support UIDs!");

        var uids = client.GetMessageUids ();

        for (int i = 0; i < client.Count; i++) {
            // check that we haven't already downloaded this message
            // in a previous session
            if (previouslyDownloadedUids.Contains (uids[i]))
                continue;

            var message = client.GetMessage (i);

            // write the message to a file
            message.WriteTo (string.Format ("{0}.msg", uids[i]));

            // add the message uid to our list of downloaded uids
            previouslyDownloadedUids.Add (uids[i]);
        }

        client.Disconnect (true);
    }
}
See Also