Пример #1
0
        public void WhenStarted_AutomaticPortNumberAvailable()
        {
            IMAP_Server imapServer = new IMAP_Server()
            {
                Bindings     = new[] { new IPBindInfo(Dns.GetHostName(), BindInfoProtocol.TCP, IPAddress.Loopback, 0) },
                GreetingText = "smtp4dev"
            };

            var errorTcs = new TaskCompletionSource <Error_EventArgs>();

            imapServer.Error += (s, ea) => errorTcs.SetResult(ea);
            var startedTcs = new TaskCompletionSource <EventArgs>();

            imapServer.Started += (s, ea) => startedTcs.SetResult(ea);

            imapServer.Start();

            var errorTask   = errorTcs.Task;
            var startedTask = startedTcs.Task;


            int index = Task.WaitAny(startedTask, errorTask);

            Assert.Equal(0, index);

            Assert.NotEqual(0, ((IPEndPoint)imapServer.ListeningPoints.Single().Socket.LocalEndPoint).Port);
        }
Пример #2
0
        public void TryStart()
        {
            if (!serverOptions.CurrentValue.ImapPort.HasValue)
            {
                log.Information("IMAP server disabled");
                return;
            }

            imapServer = new IMAP_Server()
            {
                Bindings     = new[] { new IPBindInfo(Dns.GetHostName(), BindInfoProtocol.TCP, serverOptions.CurrentValue.AllowRemoteConnections ? IPAddress.Any : IPAddress.Loopback, serverOptions.CurrentValue.ImapPort.Value) },
                GreetingText = "smtp4dev"
            };
            imapServer.SessionCreated += (o, ea) => new SessionHandler(ea.Session, this.serviceScopeFactory);


            var errorTcs = new TaskCompletionSource <Error_EventArgs>();

            imapServer.Error += (s, ea) =>
            {
                if (!errorTcs.Task.IsCompleted)
                {
                    errorTcs.SetResult(ea);
                }
            };
            var startedTcs = new TaskCompletionSource <EventArgs>();

            imapServer.Started += (s, ea) => startedTcs.SetResult(ea);

            imapServer.Start();

            var errorTask   = errorTcs.Task;
            var startedTask = startedTcs.Task;

            int index = Task.WaitAny(startedTask, errorTask, Task.Delay(TimeSpan.FromSeconds(30)));

            if (index == 1)
            {
                log.Warning("The IMAP server failed to start: {Exception}" + errorTask.Result.Exception.ToString());
            }
            else if (index == 2)
            {
                log.Warning("The IMAP server failed to start: Timeout");

                try
                {
                    imapServer.Stop();
                }
                catch { }
            }
            else
            {
                int port = ((IPEndPoint)imapServer.ListeningPoints[0].Socket.LocalEndPoint).Port;
                log.Information("IMAP Server is listening on port {port}", port);
            }
        }
Пример #3
0
        public void TryStart()
        {
            if (!serverOptions.CurrentValue.ImapPort.HasValue)
            {
                Console.WriteLine($"IMAP server disabled");
                return;
            }

            imapServer = new IMAP_Server()
            {
                Bindings     = new[] { new IPBindInfo(Dns.GetHostName(), BindInfoProtocol.TCP, serverOptions.CurrentValue.AllowRemoteConnections ? IPAddress.Any : IPAddress.Loopback, serverOptions.CurrentValue.ImapPort.Value) },
                GreetingText = "smtp4dev"
            };
            imapServer.SessionCreated += (o, ea) => new SessionHandler(ea.Session, this.messagesRepository);

            imapServer.Start();
            Console.WriteLine($"IMAP Server listening on port {imapServer.Bindings[0].Port}");
        }