Check for unread emails
Asked Answered
P

3

7

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

Periodicity answered 24/10, 2010 at 2:5 Comment(1)
You need to include more information. You've tagged this as C# so we can assume that is the programming language in question, but you need to specify mail protocol (POP, IMAP, Exchange, ...)Sheeting
K
4

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.

Kagera answered 24/10, 2010 at 2:20 Comment(1)
I edited my question. I'll check out the IMAP link and check this as answered if it's the way to go. Thanks for now.Periodicity
F
3

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);
}
Forward answered 12/10, 2016 at 21:39 Comment(0)
L
1

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.

Lobbyist answered 30/3, 2011 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.