I'm using GemBox.Email and I'm retrieving unread emails from my inbox like this:
using (var imap = new ImapClient("imap.gmail.com"))
{
imap.Connect();
imap.Authenticate("username", "password");
imap.SelectInbox();
IEnumerable<string> unreadUids = imap.ListMessages()
.Where(info => !info.Flags.Contains(ImapMessageFlags.Seen))
.Select(info => info.Uid);
foreach (string uid in unreadUids)
{
MailMessage unreadEmail = imap.GetMessage(uid);
unreadEmail.Save(uid + ".eml");
}
}
The code is from the Receive example, but the problem is that after retrieving them they end up being marked as read in my inbox.
How can I prevent this from happening?
I want to download them with ImapClient
and leave them as unread on email server.
EDIT (2021-01-19):
Please try again with the latest version from the BugFixes page or from NuGet.
The latest version provides ImapClient.PeekMessage
methods which you can use like this:
using (var imap = new ImapClient("imap.gmail.com"))
{
imap.Connect();
imap.Authenticate("username", "password");
imap.SelectInbox();
foreach (string uid in imap.SearchMessageUids("UNSEEN"))
{
MailMessage unreadEmail = imap.PeekMessage(uid);
unreadEmail.Save(uid + ".eml");
}
}
ORIGINAL:
When retrieving an email, most servers will mark it with the "SEEN" flag. If you want to leave an email as unread then you can just remove the flag.
Also, instead of using ImapClient.ListMessages
you could use ImapClient.SearchMessageUids
to get IDs of unread emails.
So, try the following:
using (var imap = new ImapClient("imap.gmail.com"))
{
imap.Connect();
imap.Authenticate("username", "password");
imap.SelectInbox();
// Get IDs of unread emails.
IEnumerable<string> unreadUids = imap.SearchMessageUids("UNSEEN");
foreach (string uid in unreadUids)
{
MailMessage unreadEmail = imap.GetMessage(uid);
unreadEmail.Save(uid + ".eml");
// Remove "SEEN" flag from read email.
imap.RemoveMessageFlags(uid, ImapMessageFlags.Seen);
}
}