Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connection doesn't restart after disruption to internet connection #170

Open
Scylla2020 opened this issue Aug 6, 2021 · 0 comments
Open

Comments

@Scylla2020
Copy link

I tried the example code below and turned internet connection off for about a minute then back on to test reconnection. Disrupting the connection gives the error Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. No more emails get fetched after that. How can I get the reconnection to always work?

using S22.Imap;
using System;

namespace ConsoleApplication1 {
    class Program {
        static AutoResetEvent reconnectEvent = new AutoResetEvent(false);
        static ImapClient client;

        static void Main(string[] args) {
            try {
                while (true) {
                    Console.Write("Connecting...");
                    InitializeClient();
                    Console.WriteLine("OK");

                    reconnectEvent.WaitOne();
                }
            } finally {
                if (client != null)
                    client.Dispose();
            }
        }

        static void InitializeClient() {
            // Dispose of existing instance, if any.
            if (client != null)
                client.Dispose();
            client = new ImapClient("imap.gmail.com", 993, "username", "password", AuthMethod.Login, true);
            // Setup event handlers.
            client.NewMessage += client_NewMessage;
            client.IdleError += client_IdleError;
        }

        static void client_IdleError(object sender, IdleErrorEventArgs e) {
            Console.Write("An error occurred while idling: ");
            Console.WriteLine(e.Exception.Message);

            reconnectEvent.Set();
        }

        static void client_NewMessage(object sender, IdleMessageEventArgs e) {
            Console.WriteLine("Got a new message, uid = " + e.MessageUID);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant