MailKit
MailKit copied to clipboard
The ImapClientDemo sample should also demo IDLE support
Ideally, the ImapClientDemo sample would also demo IDLE support and restoring the connection if/when it gets dropped.
Maintaining the connection would be AWESOME and super helpful. That's the hardest part to get right! :+1:
AWESOME!!! :thumbsup: :thumbsup: :thumbsup:
That would be awesome!
+1 This would really help!!!
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
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:
- Using
AddFlags()instead ofSetFlags(). The reason for this is becauseSetFlags()assigns an absolute collection of flags on the message which means that your second call toSetFlags()overrides your first call and so your message ends up with only the\Deletedflag set and not the\Seenflag. - The bit-wise or'ing of flags to save a round trip.
Hope that helps.
Thanks...didn't know that. I fixed it in the example
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?
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 thanks! I will follow your advice
When I have some time I will update the IMAP idle demo :-)
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?
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.
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.
At this point I don't really see myself ever implementing this - it's just too "complex" for a simple sample app.