BeginFetchMessage and BeginDeleteMessage is not working correctly

Hi,

I am trying to regularly fetch, process and delete incoming messages to a Gmail inbox. THis is the code I am using:

private static Pop3Client pop3 = new Pop3Client("pop.gmail.com", 995, @"****", "****");
private static Timer scanTimer;

public static void Main() {
var license = LicenseProvider.GetAsposeLicense();
License emailLicense = new License();
emailLicense.SetLicense(license);
pop3.EnableSsl = true;
scanTimer = new Timer(Pop3Async, null, 0, -1);
Console.Read();
}

static async void Pop3Async(Object o) {
scanTimer.Change(Timeout.Infinite, Timeout.Infinite);
var messages = pop3.ListMessages(true);
var count = messages.Count;
Console.WriteLine("new message count: {0}", count);

foreach (var id in messages.Select(m => m.UniqueId)) {
Console.WriteLine("fetching message with id {0}", id);
var message = await Task.Factory.FromAsync(pop3.BeginFetchMessage(id), pop3.EndFetchMessage);
Console.WriteLine("message with subject {0} fetched, deleting", message.Subject);
await Task.Factory.FromAsync(pop3.BeginDeleteMessage(id), pop3.EndDeleteMessage);
}
//pop3.Quit();
scanTimer.Change(1000, 1000);
}

When I run this on an empty inbox and then send a message in, the asynchronous fetch call throws an exception "Wrong unique identifier". It is a bit intermittent, doesn't happen always but most of the time. If I stop the program then and run again, it fetches and deletes the message just fine with the same uniqueID, so the problem might be in the session that BeginFetchMessage is using... If I change the fetch to a synchronous pop3.FetchMessage() operation, it works and the asynchronous delete throws the exception. With both fetch and delete synchronous this works fine, but I want those calls to be asynchronous in my application if possible.

Thanks,
Martin

Hi Martin,

The code sample you have shared uses the old version of BeginDeleteMessage that has been rewritten now due to a bug found in earlier implementation. The latest version of API uses the Asynchronous operations as shown in the following code sample. Please try it at your end and let us know your feedback?

Sample Code:

static void Pop3Async()
{
    Pop3Client pop3 = new Pop3Client("pop.gmail.com", 995, @"username", "pwd");
    pop3.EnableSsl = true;

    var messages = pop3.ListMessages(true);
    var count = messages.Count;
    Console.WriteLine("new message count: {0}", count);

    foreach (var id in messages.Select(m => m.UniqueId))
    {
        Console.WriteLine("fetching message with id {0}", id);
        ThreadPool.QueueUserWorkItem(delegate(object o)
        {
            var message = pop3.FetchMessage(id);
            Console.WriteLine("message with subject {0} fetched, deleting", message.Subject);
            pop3.DeleteMessage(id);
            pop3.CommitDeletes();
        });
    }
}

Hi Kashif,


I am pretty sure BeginDeleteMessage with id as parameter is pretty new since I asked for it when I only found on with sequencenumber parameter in 3.8.0. This sample code is not truly asynchronous, it just blocks another thread for seconds, waiting for the synchronous operations to complete.
Plus, I can’t use “fire and forget”, I have to wait for the whole email processing to be done and execute the rest of my code (that Console.Writeline is obviously just for demonstration).
As I mentioned in another thread, I really hope the Begin/End implementation are truly asynchronous with a fire and callback and there is actual merit in using them as opposed to the synchronous operations. Is that so?

thanks,
Martin

Hi Martin,


The implementation of Being/End were modified to handle asynchronous operations using the ThreadPool.QueueUserWorkItem which launches a separate connection for the operation specified and that is why the use of Being/End messages were not necessary in this case. For further technical information in this regard, I have requested development team’s feedback in this regard and will update you here once the information is available. Your patience is highly appreciated in this regard.

Hi Kashif,


any new information on this? I would like to use the Begin/End methods for asynchronous email processing, but they still seem bugged.

Martin

Hi Martin,


Thank you for writing to Aspose support team.

I am afraid to share but there are no updates available in this regard since the implementation and usage of ThreadPool.QueueUserWorkItem is used for Asynchronous operations now, enabling each request to be launched in a new thread. We’ll update you here once there is some information available in this regard.

Hi Kashif,


same as in Pop3.BeginListMessages not working correctly, the issue is Begin/End methods being bugged, the usage of them in my example code is pretty basic and straightforward and there is an obvious bug in it so please get it fixed or remove all the Begin/End methods from the Pop3Client, don’t even get me started on what happens if I try to use BeginFetch in parallel to fetch 2 emails asynchronously, and not serially in a for cycle…

Martin

Hi Martin,


Current implementation of Begin/End methods in our library uses additional background threads. To implement Begin/End methods without blocking another thread we need seriously rework our client. This task require enough much time, and by this moment we are afraid to share that we do not include this task in our plans.

Please feel free to write us back if you have any other query related to Aspose.Email

Hi Kashif,


well that is not great news as this is a serious flaw that makes the Begin/End methods close to useless, but at least I know not to await a fix.

Martin

Hi Martin,

Thank you for your understanding. Please feel free yo write to us in case you have any additional query related to Aspose.Email.