简体   繁体   English

检索未读电子邮件并将其保留为收件箱中的未读

[英]Retrieve unread emails and leave them as unread in inbox

I'm using GemBox.Email and I'm retrieving unread emails from my inbox like this:我正在使用GemBox.Email并且我正在从我的收件箱中检索未读电子邮件,如下所示:

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.我想用ImapClient下载它们,并在 email 服务器上将它们保留为未读。

EDIT (2021-01-19):编辑(2021-01-19):

Please try again with the latest version from the BugFixes page or from NuGet .请使用错误修复页面或NuGet中的最新版本重

The latest version provides ImapClient.PeekMessage methods which you can use like this:最新版本提供了ImapClient.PeekMessage方法,您可以像这样使用:

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.在检索 email 时,大多数服务器会用“SEEN”标志对其进行标记。 If you want to leave an email as unread then you can just remove the flag.如果您想将 email 保留为未读,则只需删除该标志即可。

Also, instead of using ImapClient.ListMessages you could use ImapClient.SearchMessageUids to get IDs of unread emails.此外,您可以使用ImapClient.SearchMessageUids来获取未读电子邮件的 ID,而不是使用ImapClient.ListMessages

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM