Mark emails as read using IMAP

First you need to download Mail.dll IMAP client.

The following code connects to IMAP server, downloads unique ids of all unseen emails and marks the first one as seen.

// C# version

using(Imap imap = new Imap())
{
	imap.Connect("server");

	imap.User = "user";
	imap.Password = "password";
	imap.Login();

	imap.SelectInbox();
	List<long> uids = client.SearchFlag(Flag.Unseen);
	if (uids.Count > 0)
		client.MarkMessageSeenByUID(uids[0]);
    imap.Close(true);
}
' VB.NET version

Using imap As New Imap()
	imap.Connect("server")

	imap.User = "user"
	imap.Password = "password"
	imap.Login()

	imap.SelectInbox()
	Dim uids As List(Of Long) = client.SearchFlag(Flag.Unseen)
	If uids.Count > 0 Then
		client.MarkMessageSeenByUID(uids(0))
	End If
	imap.Close(True)
End Using

Please note that it is not possible to mark messages as read using POP3 protocol.

Tags: ,

Leave a Reply