Code Monkey home page Code Monkey logo

Comments (11)

q00Dree avatar q00Dree commented on May 20, 2024

I recently saw a ticket with the same error in the MailKit repository, maybe this will somehow help in solving the problem

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

Hi @q00Dree, thanks for taking the time to post about this issue.

The IMAP receiver can only have one call at a time (it's not re-entrant) so this is a bug. I thought it'd be a quick and easy fix by doing a deep copy of the receiver in the idle client, but not sure yet why it's not working for me yet. Thanks for pointing it out!

In the mean-time, do something like this as a work-around (it does the same thing as your code):

// Don't do this in real code! Use Dependancy Injection instead.
using var imapReceiver1 = ImapReceiver.Create("imapHost:imapPort")
    .SetCredential("login", "applicationPassword");
using var imapReceiver2 = ImapReceiver.Create("imapHost:imapPort")
    .SetCredential("login", "applicationPassword");

await imapReceiver1.MonitorFolder
   .SetIgnoreExistingMailOnConnect(false)
   .OnMessageArrival(OnArrivalAsync)
   .IdleAsync(cancellationToken);

async Task OnArrivalAsync(IMessageSummary messageSummary)
{
    try
    {
        Console.WriteLine($"{imapReceiver1} message #{messageSummary.UniqueId} arrived.");
        var mimeMessage = await imapReceiver2.ReadMail.GetMimeMessageAsync(messageSummary.UniqueId);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex, ex.Message);
    }
}

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

If you use Dependancy Injection it becomes much simpler. Refer to the Samples folder examples.

await _mailFolderMonitor
   .OnMessageArrival(OnArrivalAsync)
   .IdleAsync(cancellationToken);

async Task OnArrivalAsync(IMessageSummary messageSummary)
{
    try
    {
        _logger.LogInformation($"{_imapReceiver} message #{messageSummary.UniqueId} arrived.");
        var mimeMessage = await _imapReceiver.ReadMail.GetMimeMessageAsync(messageSummary.UniqueId);
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, ex.Message);
    }
}

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

I've got a simple deep copy fix working for the idle client, but it won't work for var mimeMessage = await messageSummary.GetMimeMessageAsync(); as that uses a readonly MailFolder assigned by MailKit in a Fetch function. I'll keep looking.

from mailkitsimplified.

q00Dree avatar q00Dree commented on May 20, 2024

Oh, got you.
Thanks for samples. I keep several idle clients for different emails at the same time, so I have to create them without using DI.

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

I got await messageSummary.GetMimeMessageAsync(); working as well now in a new branch.

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

Update to the latest version to get the fix.

from mailkitsimplified.

q00Dree avatar q00Dree commented on May 20, 2024

Thank you for the quick solution. But I wonder, how it works in MailKit ImapIdle using one ImapClient for idling and fetching. Or I miss something?

from mailkitsimplified.

q00Dree avatar q00Dree commented on May 20, 2024

This code from sample works without any problem

async Task FetchMessageSummariesAsync(bool print)
{
    IList<IMessageSummary> fetched;

    do
    {
        try
        {
            // fetch summary information for messages that we don't already have
            int startIndex = messages.Count;

            fetched = client.Inbox.Fetch(startIndex, -1, request, cancel.Token);
            break;
        }
        catch (ImapProtocolException)
        {
            // protocol exceptions often result in the client getting disconnected
            await ReconnectAsync();
        }
        catch (IOException)
        {
            // I/O exceptions always result in the client getting disconnected
            await ReconnectAsync();
        }
    } while (true);

    foreach (var message in fetched)
    {
        if (print)
        {
            var mimeMessage = await client.Inbox.GetMessageAsync(message.UniqueId);
            Console.WriteLine("{0}: new message: {1}", client.Inbox, mimeMessage.TextBody);
        }
        messages.Add(message);
    }
}

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

It's a good idea but it could raise some other issues.

  1. It relies on exception handling to run, normally not a good practice.
  2. If anyone tries to download the IMessageSummary returned using the internal IMailFolder it will throw another exception.

from mailkitsimplified.

danzuep avatar danzuep commented on May 20, 2024

I keep several idle clients for different emails at the same time, so I have to create them without using DI.

This could be a good use-case for the Factory Method Design Pattern. Please let me know if you'd like to make one and submit a merge request.

from mailkitsimplified.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.