Exemplo n.º 1
0
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Certificate(CreateCertificate())
                          .Endpoint(builder =>
                                    builder
                                    .Port(9025, true)
                                    .AllowUnsecureAuthentication(false))
                          .Build();

            var serviceProvider = new ServiceProvider();

            serviceProvider.Add(new CustomEndpointListenerFactory());

            var server = new SmtpServer.SmtpServer(options, serviceProvider);

            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            SampleMailClient.Send(useSsl: true);

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();
        }
        public static void Run()
        {
            // this is important when dealing with a certificate that isnt valid
            ServicePointManager.ServerCertificateValidationCallback = IgnoreCertificateValidationFailureForTestingOnly;

            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Certificate(CreateCertificate())
                          .AllowUnsecureAuthentication(false)
                          .UserAuthenticator(new SampleUserAuthenticator())
                          .Port(9025, true)
                          .Build();

            var server = new SmtpServer.SmtpServer(options);

            server.SessionCreated += OnSessionCreated;

            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            SampleMailClient.Send(user: "******", password: "******", useSsl: true);

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();
        }
        public static void Run()
        {
            _cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Endpoint(builder =>
                                    builder
                                    .AllowUnsecureAuthentication()
                                    .AuthenticationRequired()
                                    .Port(9025))
                          .Build();

            var serviceProvider = new ServiceProvider();

            serviceProvider.Add(new AuthenticationHandler());

            var server = new SmtpServer.SmtpServer(options, serviceProvider);

            server.SessionCreated   += OnSessionCreated;
            server.SessionCompleted += OnSessionCompleted;

            var serverTask = server.StartAsync(_cancellationTokenSource.Token);

            SampleMailClient.Send(user: "******", password: "******", count: 5);

            serverTask.WaitWithoutException();
        }
Exemplo n.º 4
0
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          .MailboxFilter(new SampleMailboxFilter(TimeSpan.FromSeconds(5)))
                          .Build();

            var server = new SmtpServer.SmtpServer(options);

            server.SessionCreated   += OnSessionCreated;
            server.SessionCompleted += OnSessionCompleted;
            server.SessionFaulted   += OnSessionFaulted;
            server.SessionCancelled += OnSessionCancelled;

            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            // ReSharper disable once MethodSupportsCancellation
            Task.Run(() => SampleMailClient.Send());

            Console.WriteLine("Press any key to cancel the server.");
            Console.ReadKey();

            Console.WriteLine("Forcibily cancelling the server and any active sessions");

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();

            Console.WriteLine("The server has been cancelled.");
        }
Exemplo n.º 5
0
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new OptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          .Build();

            var server     = new SmtpServer.SmtpServer(options);
            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            SampleMailClient.Send();

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();
        }
Exemplo n.º 6
0
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          //.Endpoint(new EndpointDefinitionBuilder().Endpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9025)).Build())
                          .Build();

            var server     = new SmtpServer.SmtpServer(options);
            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            SampleMailClient.Send();

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();
        }
        public static void Run()
        {
            _cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          .Build();

            var server = new SmtpServer.SmtpServer(options, ServiceProvider.Default);

            server.SessionCreated   += OnSessionCreated;
            server.SessionCompleted += OnSessionCompleted;

            var serverTask = server.StartAsync(_cancellationTokenSource.Token);

            SampleMailClient.Send();

            serverTask.WaitWithoutException();
        }
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          .Build();

            var serviceProvider = new ServiceProvider();

            serviceProvider.Add(new SampleMessageStore(Console.Out));

            var server     = new SmtpServer.SmtpServer(options, serviceProvider);
            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            SampleMailClient.Send();

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();
        }
Exemplo n.º 9
0
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          .Build();

            var serviceProvider = new ServiceProvider();

            serviceProvider.Add(new SampleMailboxFilter(TimeSpan.FromSeconds(2)));

            var server = new SmtpServer.SmtpServer(options, serviceProvider);

            server.SessionCreated   += OnSessionCreated;
            server.SessionCompleted += OnSessionCompleted;
            server.SessionFaulted   += OnSessionFaulted;
            server.SessionCancelled += OnSessionCancelled;

            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            // ReSharper disable once MethodSupportsCancellation
            Task.Run(() => SampleMailClient.Send());

            Console.WriteLine("Press any key to shudown the server.");
            Console.ReadKey();

            Console.WriteLine("Gracefully shutting down the server.");
            server.Shutdown();

            server.ShutdownTask.WaitWithoutException();
            Console.WriteLine("The server is no longer accepting new connections.");

            Console.WriteLine("Waiting for active sessions to complete.");
            serverTask.WaitWithoutException();

            Console.WriteLine("All active sessions are complete.");
        }
Exemplo n.º 10
0
        public static void Run()
        {
            var cancellationTokenSource = new CancellationTokenSource();

            var options = new SmtpServerOptionsBuilder()
                          .ServerName("SmtpServer SampleApp")
                          .Port(9025)
                          .Build();

            var services = new ServiceCollection();

            services.AddSingleton(options);
            services.AddTransient <ISmtpCommandFactory, CustomSmtpCommandFactory>();

            var server = new SmtpServer.SmtpServer(options, services.BuildServiceProvider());

            var serverTask = server.StartAsync(cancellationTokenSource.Token);

            SampleMailClient.Send();

            cancellationTokenSource.Cancel();
            serverTask.WaitWithoutException();
        }