Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

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 subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant