Hi,
I am periodically checking for new email in an account to process it. Originally I checked every second (with pausing timer while checking) connecting each time, processing 1 message at a time and then disconnecting. Then I found out Connect is really slow and takes around 2 sec, so I wanted to connect at the start (with a reconnect every hour to keep connection from deteriorating) and just periodically check ListMessages().Count to see if there is new email. To my surprise, this doesn’t work, after my program starts running, connects and starts the timer ticking, no new email that arrives to the account gets picked up by ListMessages() and no more messages ever get processed until a reconnect occurs.
A simplified version of my code is here (took out credentials obviously):
using Aspose.Email.Imap;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace AsposeTest
{
class Program
{
private static ImapClient client;
private static Timer scanTimer;
static void Main(string[] args)
{
client = new ImapClient(“[imap.gmail.com](http://imap.gmail.com/)”, 993, @“xxxx@gmail.com”, “xxxx”);
client.EnableSsl = true;
client.Connect(true);
client.SelectFolder(ImapFolderInfo.InBox);
scanTimer = new Timer(Fetch, null, 1000, Timeout.Infinite);
Console.ReadLine();
client.Disconnect();
}
static void Fetch(Object o)
{
scanTimer.Change(Timeout.Infinite, Timeout.Infinite);
FetchAndDel();
scanTimer.Change(1000, Timeout.Infinite);
}
static void FetchAndDel()
{
int count = client.ListMessages().Count;
Console.WriteLine(“You have {0} new messages”, count);
if (count == 0) return;
var message = client.FetchMessage(1);
Console.WriteLine(“Fetched message 1, subject: {0}”, message.Subject);
client.DeleteMessage(1);
client.ExpungeMessages();
Console.WriteLine(“Deleted message 1”);
}
}
}
The out put is that it processes all message in the Inbox that are present when program starts, but no more new massages are processed. I managed to get it working by adding this line to the top of the FetchAndDel() method:
client.SelectFolder(ImapFolderInfo.InBox);
But it feels quite unnecessary having to do that on every tick, and SelectFolder also takes about half a second, making it not very efficient. Is this as intended? Is there a way to get around it?
Thank you,
Martin