检查未读电子邮件

发布于 2024-09-28 22:40:11 字数 116 浏览 4 评论 0原文

我正在寻找一种方法来检查电子邮件帐户上未读电子邮件的数量。 有什么建议吗?

编辑:如标签中所述,适用于 C#。据我所知,IMAP 是最佳选择,并且我确认我要使用的所有电子邮件帐户都已激活 IMAP :)

I'm looking for a way to check the number of unread emails on an email account.
Any tips?

EDIT: As described in the tags, for C#. As I learned IMAP is the way to go and I confirmed all email accounts I'm going to use have IMAP activated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寻梦旅人 2024-10-05 22:40:11

POP

您可以使用 OpenPOP.net 使用 POP 协议阅读电子邮件。 POP 的问题在于它不保存是否未读的详细信息。所以我认为这对你来说没有多大用处。您有自己的方式来下载电子邮件并将其标记为已读或未读。

IMAP

SO 中的这个问题有一些使用 IMAP 的示例链接。 IMAP 包含有关邮件状态(已读/未读)的详细信息。

请详细说明您的要求。

POP

You can use OpenPOP.net to read emails using POP protocol. The problem with POP is that it does not hold details whether it was unread or not. So I think this will not be of much use to you. You have have your own way of downloading and tagging emails as read or unread.

IMAP

This question in SO has some links for examples using IMAP. IMAP has details about mail status(read/unread).

Please explain more about your requirement.

心房敞 2024-10-05 22:40:11

如果您想要获取 IMAP 文件夹中未读邮件的数量,您可以使用 MailKit 来这样做:

using MailKit;
using MailKit.Search;
using MailKit.Net.Imap;

...

using (var client = new ImapClient ()) {
    // Note: depending on your server, you might need to connect
    // on port 993 using SecureSocketOptions.SslOnConnect
    client.Connect ("imap.server.com", 143, SecureSocketOptions.StartTls);

    // Note: use your real username/password here...
    client.Authenticate ("username", "password");

    // open the Inbox folder...
    client.Inbox.Open (FolderAccess.ReadOnly);

    // search the folder for new messages (aka recently
    // delivered messages that have not been read yet)
    var uids = client.Inbox.Search (SearchQuery.New);

    Console.WriteLine ("You have {0} new message(s).", uids.Count);

    // ...but maybe you mean unread messages? if so, use this query
    uids = client.Inbox.Search (SearchQuery.NotSeen);

    Console.WriteLine ("You have {0} unread message(s).", uids.Count);

    client.Disconnect (true);
}

If what you want to do is get the number of unread messages in an IMAP folder, you can use MailKit to do this:

using MailKit;
using MailKit.Search;
using MailKit.Net.Imap;

...

using (var client = new ImapClient ()) {
    // Note: depending on your server, you might need to connect
    // on port 993 using SecureSocketOptions.SslOnConnect
    client.Connect ("imap.server.com", 143, SecureSocketOptions.StartTls);

    // Note: use your real username/password here...
    client.Authenticate ("username", "password");

    // open the Inbox folder...
    client.Inbox.Open (FolderAccess.ReadOnly);

    // search the folder for new messages (aka recently
    // delivered messages that have not been read yet)
    var uids = client.Inbox.Search (SearchQuery.New);

    Console.WriteLine ("You have {0} new message(s).", uids.Count);

    // ...but maybe you mean unread messages? if so, use this query
    uids = client.Inbox.Search (SearchQuery.NotSeen);

    Console.WriteLine ("You have {0} unread message(s).", uids.Count);

    client.Disconnect (true);
}
无法言说的痛 2024-10-05 22:40:11

以下是 LumiSoft IMAP 库的代码示例:

using LumiSoft.Net.IMAP;
using LumiSoft.Net.IMAP.Client;
using LumiSoft.Net;

...

using (IMAP_Client client = new IMAP_Client())
{
    client.Connect("imap.gmail.com", 993, true);
    client.Login("[email protected]", "your_cool_password");
    client.SelectFolder("INBOX");

    IMAP_SequenceSet sequence = new IMAP_SequenceSet();
    //sequence.Parse("*:1"); // from first to last

    IMAP_Client_FetchHandler fetchHandler = new IMAP_Client_FetchHandler();

    fetchHandler.NextMessage += new EventHandler(delegate(object s, EventArgs e)
    {
        Console.WriteLine("next message");
    });

    fetchHandler.Envelope += new EventHandler<EventArgs<IMAP_Envelope>>(delegate(object s, EventArgs<IMAP_Envelope> e){
        IMAP_Envelope envelope = e.Value;
        if (envelope.From != null && !String.IsNullOrWhiteSpace(envelope.Subject))
        {
            Console.WriteLine(envelope.Subject);
        }

    });

    // the best way to find unread emails is to perform server search

    int[] unseen_ids = client.Search(false, "UTF-8", "unseen");
    Console.WriteLine("unseen count: " + unseen_ids.Count().ToString());

    // now we need to initiate our sequence of messages to be fetched
    sequence.Parse(string.Join(",", unseen_ids));

    // fetch messages now
    client.Fetch(false, sequence, new IMAP_Fetch_DataItem[] { new IMAP_Fetch_DataItem_Envelope() }, fetchHandler);

    // uncomment this line to mark messages as read
    // client.StoreMessageFlags(false, sequence, IMAP_Flags_SetType.Add, IMAP_MessageFlags.Seen);
}

有点复杂,但工作正常。 Limisoft 库并不完美,因此请务必对其进行良好的测试。

Here is the sample of code with LumiSoft IMAP library:

using LumiSoft.Net.IMAP;
using LumiSoft.Net.IMAP.Client;
using LumiSoft.Net;

...

using (IMAP_Client client = new IMAP_Client())
{
    client.Connect("imap.gmail.com", 993, true);
    client.Login("[email protected]", "your_cool_password");
    client.SelectFolder("INBOX");

    IMAP_SequenceSet sequence = new IMAP_SequenceSet();
    //sequence.Parse("*:1"); // from first to last

    IMAP_Client_FetchHandler fetchHandler = new IMAP_Client_FetchHandler();

    fetchHandler.NextMessage += new EventHandler(delegate(object s, EventArgs e)
    {
        Console.WriteLine("next message");
    });

    fetchHandler.Envelope += new EventHandler<EventArgs<IMAP_Envelope>>(delegate(object s, EventArgs<IMAP_Envelope> e){
        IMAP_Envelope envelope = e.Value;
        if (envelope.From != null && !String.IsNullOrWhiteSpace(envelope.Subject))
        {
            Console.WriteLine(envelope.Subject);
        }

    });

    // the best way to find unread emails is to perform server search

    int[] unseen_ids = client.Search(false, "UTF-8", "unseen");
    Console.WriteLine("unseen count: " + unseen_ids.Count().ToString());

    // now we need to initiate our sequence of messages to be fetched
    sequence.Parse(string.Join(",", unseen_ids));

    // fetch messages now
    client.Fetch(false, sequence, new IMAP_Fetch_DataItem[] { new IMAP_Fetch_DataItem_Envelope() }, fetchHandler);

    // uncomment this line to mark messages as read
    // client.StoreMessageFlags(false, sequence, IMAP_Flags_SetType.Add, IMAP_MessageFlags.Seen);
}

Bit complicated, but works fine. Limisoft library is not perfect, so be sure you test it well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文