Reputation: 505
Below code works well, but the code will be checked every 5000ms, when the inbox folder is empty system shows an error. We need to check inbox every 5000ms even it be empty we need to read it.
using (var client = new ImapClient())
{
client.Connect(EXT_IMAP_SERVER, EXT_IMAP_PORT, true);
client.Authenticate(EXT_USERNAME, EXT_PASSWORD);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadWrite);
var LAST_MSG = inbox.GetMessage (inbox.Count - 1);
DATA = LAST_MSG.Subject;
if(DATA != null); // this condition didn't solve our issue
{
inbox.AddFlags(inbox.Count - 1, MessageFlags.Deleted, true);
}
client.Disconnect(true);
}
**ERROR:
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'index')**
Upvotes: 0
Views: 405
Reputation: 3524
Instead of checking if last message was null, check the Count
of messages and if it's more than 0, then you do what you need to do.
using (var client = new ImapClient())
{
client.Connect(EXT_IMAP_SERVER, EXT_IMAP_PORT, true);
client.Authenticate(EXT_USERNAME, EXT_PASSWORD);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadWrite);
// THIS
if(inbox.Count > 0) {
var LAST_MSG = inbox.GetMessage (inbox.Count - 1);
// .....
}
client.Disconnect(true);
}
Upvotes: 2