开发者

Check for unread emails

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 :)


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.


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


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("your.username@gmail.com", "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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜