Inheritance: ISmtpServerCommandHandler
        public void TestSmtpEndToEnd()
        {
            var commandHandler = new InMemoryCommandHandler();

            Func <ISession> connectionFactory = () =>
                                                new SmtpServerSession(commandHandler, new NullLog(), new SmtpServerSessionConfiguration());

            var serverConfiguration = new ServerConfiguration()
            {
                IpAddress = IPAddress.Parse("127.0.0.1")
            };

            var smtpServer = new Server(connectionFactory, new NullLog(), serverConfiguration);
            var runTask    = smtpServer.RunAsync();

            using (var message = new MailMessage())
            {
                message.From = new MailAddress("*****@*****.**");
                message.To.Add(new MailAddress("*****@*****.**"));
                message.To.Add(new MailAddress("*****@*****.**"));
                message.Body = "Test";

                using (
                    var client = new SmtpClient(smtpServer.LocalEndpoint.Address.ToString(),
                                                smtpServer.LocalEndpoint.Port))

                {
                    client.Send(message);
                }
            }

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));

            Assert.AreEqual("*****@*****.**", commandHandler.MailFrom);
            Assert.AreEqual(2, commandHandler.Recipients.Count);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[0]);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[1]);

            var bodyStream = new MemoryStream();

            commandHandler.Body.CopyTo(bodyStream);
            string mailMessage = Encoding.UTF8.GetString(bodyStream.ToArray());
            var    bodyStart   = mailMessage.IndexOf("\r\n\r\n", StringComparison.InvariantCultureIgnoreCase);
            var    body        = mailMessage.Substring(bodyStart + 4);

            Assert.AreEqual("Test\r\n\r\n", body);
        }
        public void TestSmtpEndToEnd()
        {
            var commandHandler = new InMemoryCommandHandler();

            Func<ISession> connectionFactory = () =>
                new SmtpServerSession(commandHandler, new NullLog(), new SmtpServerSessionConfiguration());

            var serverConfiguration = new ServerConfiguration()
                {
                    IpAddress = IPAddress.Parse("127.0.0.1")
                };

            var smtpServer = new Server(connectionFactory, new NullLog(), serverConfiguration);
            var runTask = smtpServer.RunAsync();

            using (var message = new MailMessage())
            {
                message.From = new MailAddress("*****@*****.**");
                message.To.Add(new MailAddress("*****@*****.**"));
                message.To.Add(new MailAddress("*****@*****.**"));
                message.Body = "Test";

                using (
                    var client = new SmtpClient(smtpServer.LocalEndpoint.Address.ToString(),
                        smtpServer.LocalEndpoint.Port))

                {
                    client.Send(message);
                }
            }

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));

            Assert.AreEqual("*****@*****.**", commandHandler.MailFrom);
            Assert.AreEqual(2, commandHandler.Recipients.Count);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[0]);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[1]);

            var bodyStream = new MemoryStream();
            commandHandler.Body.CopyTo(bodyStream);
            string mailMessage = Encoding.UTF8.GetString(bodyStream.ToArray());
            var bodyStart = mailMessage.IndexOf("\r\n\r\n", StringComparison.InvariantCultureIgnoreCase);
            var body = mailMessage.Substring(bodyStart + 4);
            Assert.AreEqual("Test\r\n\r\n", body);
        }
        private static InMemoryCommandHandler SendMessageWithAttachment(Attachment attachment)
        {
            var commandHandler = new InMemoryCommandHandler();

            Func<ISession> connectionFactory = () =>
                new SmtpServerSession(commandHandler, new NullLog(), new SmtpServerSessionConfiguration());

            var serverConfiguration = new ServerConfiguration();

            var smtpServer = new Server(connectionFactory, new NullLog(),  serverConfiguration);
            var runTask = smtpServer.RunAsync();
            
            var stopwatch = new Stopwatch();
            stopwatch.Start();

            using (var client = new SmtpClient(smtpServer.LocalEndpoint.Address.ToString(), smtpServer.LocalEndpoint.Port))
            using (var message = new MailMessage())
            {
                message.From = new MailAddress("*****@*****.**");
                message.To.Add(new MailAddress("*****@*****.**"));
                message.To.Add(new MailAddress("*****@*****.**"));
                message.Body = "Test";
                message.Attachments.Add(attachment);


                client.Send(message);
            }
            stopwatch.Stop();

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));

            Console.WriteLine("Sending time was {0}", stopwatch.Elapsed);

            return commandHandler;
        }
        public void TestSmtpEndToEndWithSsl()
        {
            var commandHandler = new InMemoryCommandHandler();

            var certificate = new X509Certificate2();

            certificate.Import(Resources.debugcert, "secret", X509KeyStorageFlags.Exportable);

            var smtpSessionConfiguration = new SmtpServerSessionConfiguration()
            {
                SslCertificate = certificate
            };

            Func <ISession> connectionFactory = () =>
                                                new SmtpServerSession(commandHandler, new NullLog(), smtpSessionConfiguration);

            var serverConfiguration = new ServerConfiguration()
            {
                IpAddress = IPAddress.Parse("127.0.0.1")
            };


            var smtpServer = new Server(connectionFactory, new NullLog(), serverConfiguration);
            var runTask    = smtpServer.RunAsync();

            using (var message = new MailMessage())
            {
                message.From = new MailAddress("*****@*****.**");
                message.To.Add(new MailAddress("*****@*****.**"));
                message.To.Add(new MailAddress("*****@*****.**"));
                message.Body = "Test";

                using (var client = new SmtpClient(smtpServer.LocalEndpoint.Address.ToString(),
                                                   smtpServer.LocalEndpoint.Port))


                {
                    ServicePointManager.ServerCertificateValidationCallback = (sender, serverCertificate, chain, sslPolicyErrors) =>
                    {
                        var certificate2 = (X509Certificate2)serverCertificate;
                        return(certificate2.Thumbprint == certificate.Thumbprint);
                    };

                    client.EnableSsl = true;
                    client.Send(message);
                }
            }

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));

            Assert.AreEqual("*****@*****.**", commandHandler.MailFrom);
            Assert.AreEqual(2, commandHandler.Recipients.Count);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[0]);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[1]);

            var bodyStream = new MemoryStream();

            commandHandler.Body.CopyTo(bodyStream);

            string mailMessage = Encoding.UTF8.GetString(bodyStream.ToArray());
            var    bodyStart   = mailMessage.IndexOf("\r\n\r\n", StringComparison.InvariantCultureIgnoreCase);
            var    body        = mailMessage.Substring(bodyStart + 4);

            Assert.AreEqual("Test\r\n\r\n", body);
        }
        public void TestSmtpEndToEndWithSsl()
        {
            var commandHandler = new InMemoryCommandHandler();

            var certificate = new X509Certificate2();
            certificate.Import(Resources.debugcert, "secret", X509KeyStorageFlags.Exportable);

            var smtpSessionConfiguration = new SmtpServerSessionConfiguration()
                {
                    SslCertificate = certificate
                };

            Func<ISession> connectionFactory = () =>
                new SmtpServerSession(commandHandler, new NullLog(), smtpSessionConfiguration);

            var serverConfiguration = new ServerConfiguration()
            {
                IpAddress = IPAddress.Parse("127.0.0.1")
            };


            var smtpServer = new Server(connectionFactory, new NullLog(), serverConfiguration);
            var runTask = smtpServer.RunAsync();

            using (var message = new MailMessage())
            {
                message.From = new MailAddress("*****@*****.**");
                message.To.Add(new MailAddress("*****@*****.**"));
                message.To.Add(new MailAddress("*****@*****.**"));
                message.Body = "Test";

                using (var client = new SmtpClient(smtpServer.LocalEndpoint.Address.ToString(),
                        smtpServer.LocalEndpoint.Port))


                {
                    ServicePointManager.ServerCertificateValidationCallback = (sender, serverCertificate, chain, sslPolicyErrors) =>
                    {
                        var certificate2 = (X509Certificate2) serverCertificate;
                        return certificate2.Thumbprint == certificate.Thumbprint;
                    };

                    client.EnableSsl = true;
                    client.Send(message);
                }
            }

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));

            Assert.AreEqual("*****@*****.**", commandHandler.MailFrom);
            Assert.AreEqual(2, commandHandler.Recipients.Count);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[0]);
            Assert.AreEqual("*****@*****.**", commandHandler.Recipients[1]);

            var bodyStream = new MemoryStream();
            commandHandler.Body.CopyTo(bodyStream);

            string mailMessage = Encoding.UTF8.GetString(bodyStream.ToArray());
            var bodyStart = mailMessage.IndexOf("\r\n\r\n", StringComparison.InvariantCultureIgnoreCase);
            var body = mailMessage.Substring(bodyStart + 4);
            Assert.AreEqual("Test\r\n\r\n", body);
        }