简体   繁体   English

Gembox.Email.Imap 获取未读邮件数

[英]Gembox.Email.Imap get the count of unread mails

i have been searching all over the place for a way to get the count of unread emails in my Gmail account using GemBox.Email.Imap.我一直在到处寻找一种方法,使用 GemBox.Email.Imap 来计算我的 Gmail 帐户中未读电子邮件的数量。 So far i am able to connect, get the count of all mails but i need just the unread, is there anyone that have some experience using this Package?.到目前为止,我能够连接,获取所有邮件的数量,但我只需要未读邮件,有没有人有使用这个 Package 的经验?

Okay, after a little bit i found out how to make this work, this is the code for a simple console aplication but it is extensible for any case.好的,经过一段时间后,我发现了如何进行这项工作,这是一个简单的控制台应用程序的代码,但它在任何情况下都是可扩展的。

using System;
using System.Collections.Generic;
using GemBox.Email;
using GemBox.Email.Imap;

namespace IMapAccess
{
    class Program
    {
        static void Main(string[] args)
        {
            ComponentInfo.SetLicense("FREE-LIMITED-KEY");

            using (ImapClient imap = new ImapClient("imap.gmail.com", 993)){
                imap.ConnectTimeout = TimeSpan.FromSeconds(10);
                imap.Connect();
                imap.Authenticate("MyEmail@gmail.com", "MySuperSecretPassword", ImapAuthentication.Native);
                imap.SelectInbox();
                IList<int> messages = imap.SearchMessageNumbers("UNSEEN");
                Console.WriteLine($"Number of unseen messages {messages.Count}");
            }
        }
    }
}

First you need an IMAP client;首先你需要一个IMAP客户端; this is the client you get from the library:这是您从图书馆获得的客户:

private readonly MailKit.Net.Imap.ImapClient _client = new MailKit.Net.Imap.ImapClient();

Next, open access to your inbox folder:接下来,打开对收件箱文件夹的访问:

await _client.Inbox.OpenAsync(FolderAccess.ReadOnly).ConfigureAwait(false);

Finally, get the list of messages with the flag "Not seen" and after return the count of values:最后,获取带有“Not seen”标志的消息列表,然后返回值的计数:

var result = _client.Inbox.Search(SearchQuery.NotSeen);

return result?.Count;

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

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