MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

The ImapClientDemo sample should also demo IDLE support

Open jstedfast opened this issue 11 years ago • 14 comments

Ideally, the ImapClientDemo sample would also demo IDLE support and restoring the connection if/when it gets dropped.

jstedfast avatar Oct 16 '14 16:10 jstedfast

Maintaining the connection would be AWESOME and super helpful. That's the hardest part to get right! :+1:

erikporter avatar Oct 16 '14 17:10 erikporter

AWESOME!!! :thumbsup: :thumbsup: :thumbsup:

mirror222 avatar Mar 04 '15 13:03 mirror222

That would be awesome!

xavier-rigau avatar Mar 10 '15 19:03 xavier-rigau

+1 This would really help!!!

Izikio avatar Oct 26 '15 07:10 Izikio

I made a demo program that uses MailKit IDLE to connect to an IMAP server. It can handle multiple connections. I tested it with 40 mailboxes all in IDLE mode without any problems.

https://github.com/Sicos1977/MailKitImapIdler/tree/master

Sicos1977 avatar Mar 19 '16 16:03 Sicos1977

Wow, very cool!

Just an FYI, instead of doing this:

folder.SetFlags(uniqueId, MessageFlags.Seen, true);
folder.SetFlags(uniqueId, MessageFlags.Deleted, true);

You actually want to do this:

folder.AddFlags(uniqueId, MessageFlags.Seen | MessageFlags.Deleted, true);

There's 2 changes here:

  1. Using AddFlags() instead of SetFlags(). The reason for this is because SetFlags() assigns an absolute collection of flags on the message which means that your second call to SetFlags() overrides your first call and so your message ends up with only the \Deleted flag set and not the \Seen flag.
  2. The bit-wise or'ing of flags to save a round trip.

Hope that helps.

jstedfast avatar Mar 19 '16 17:03 jstedfast

Thanks...didn't know that. I fixed it in the example

Sicos1977 avatar Mar 19 '16 17:03 Sicos1977

I'm receving emails in background for the lifecycle of my application. I started with something simple: just fetching emails every second but it's definitely not a proper solution. I'm trying to figure out which sample is closer to what I need but I'm a little confused. The basic sample seems to be doing what I need (I stole reconnection logic from it) but Idle sample also seems relevant (I don't want to poll for emails, ideally I'd like to subscribe to events and keep the app Idle when there're no emails). Another seemingly relevant sample was mentioned in this thread (though it hasn't been updated for years) and it's a little confusing this issue hasn't been close for many years while all other issues are closed fast. Could anyone clarify which samples are the closest to what I'm looking for?

siberianguy avatar Sep 09 '18 06:09 siberianguy

The ImapIdle sample is definitely something you want to look at for what you are trying to do, but you'll need to re-work the done CancellationToken such that you can cancel it within the event callbacks. That's really all you need to do to change the logic in the ImapIdle sample.

I haven't fixed the basic client demo to use IDLE yet because I just haven't had the time to do it and writing Windows.Forms code is not my favorite thing to do. I much prefer writing back-end library code. In other words, I guess fixing bugs in MimeKit and MailKit themselves brings me joy. Writing samples doesn't. If that makes sense...

jstedfast avatar Sep 09 '18 12:09 jstedfast

@jstedfast thanks! I will follow your advice

siberianguy avatar Sep 09 '18 12:09 siberianguy

When I have some time I will update the IMAP idle demo :-)

Sicos1977 avatar Oct 09 '18 06:10 Sicos1977

Hi @jstedfast ,

I was trying to upgrade my MailKitImapIdler but I have some issues.

Can you tell me what the replacement is for the folder.MessagesArrived += MessagesArrived; event?

Sicos1977 avatar Oct 09 '18 15:10 Sicos1977

Yea, so there's just the folder.CountChanged event now. The MessagesArrived just used a hack where it compared the old Count value with the new Count value and if the new Count value was greater than the old Count value, it emitted MessagesArrived.

jstedfast avatar Oct 09 '18 15:10 jstedfast

I implement a ClientPool to store many mailbox factory and instance, ClientPool will maintaining connection status if caller want to do something in a mailbox.

Deliay avatar Oct 16 '19 06:10 Deliay

At this point I don't really see myself ever implementing this - it's just too "complex" for a simple sample app.

jstedfast avatar Feb 22 '24 16:02 jstedfast