Click or drag to resize
MimeKit

MailSpoolGetMessageUids Method

Get the full list of available message UIDs.

Namespace: MailKit
Assembly: MailKit (in MailKit.dll) Version: 4.7.1
Syntax
C#
public abstract IList<string> GetMessageUids(
	CancellationToken cancellationToken = default
)

Parameters

cancellationToken  CancellationToken  (Optional)
The cancellation token.

Return Value

IListString
The message uids.

Implements

IMailSpoolGetMessageUids(CancellationToken)
Exceptions
ExceptionCondition
ObjectDisposedException The MailSpool has been disposed.
ServiceNotConnectedException The MailSpool is not connected.
ServiceNotAuthenticatedException The MailSpool is not authenticated.
NotSupportedException The mail spool does not support UIDs.
OperationCanceledException The operation was canceled via the cancellation token.
IOException An I/O error occurred.
CommandException The command failed.
ProtocolException A protocol error occurred.
Remarks
Not all servers support UIDs, so you should first check the SupportsUids 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