当前位置: 首页>>代码示例>>C#>>正文


C# Server.StopAsync方法代码示例

本文整理汇总了C#中Server.StopAsync方法的典型用法代码示例。如果您正苦于以下问题:C# Server.StopAsync方法的具体用法?C# Server.StopAsync怎么用?C# Server.StopAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Server的用法示例。


在下文中一共展示了Server.StopAsync方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: TestSendManyMessagesOnSingleConnections

        public void TestSendManyMessagesOnSingleConnections()
        {
            Func<ISession> connectionFactory = () =>
                new SmtpServerSession(new InMemoryCommandHandler(), new NullLog(), new SmtpServerSessionConfiguration());

            var serverConfiguration = new ServerConfiguration();

            var smtpServer = new Server(connectionFactory, new NullLog(), serverConfiguration);
            var runTask = smtpServer.RunAsync();
            
            using (var client = new SmtpClient(smtpServer.LocalEndpoint.Address.ToString(), smtpServer.LocalEndpoint.Port))
            {
                for (int i = 0; i < 50; i++)
                {
                    using (var message = new MailMessage())
                    {
                        message.From = new MailAddress("[email protected]");
                        message.To.Add(new MailAddress("[email protected]"));
                        message.To.Add(new MailAddress("[email protected]"));
                        message.Body = "Test";
                        client.Send(message);
                    }
                }
            }

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));
        }
开发者ID:hmailserver,项目名称:hmailserver-net,代码行数:30,代码来源:SmtpVolumeTests.cs

示例2: TestSendManyMessagesOnSeparateConnections

        public void TestSendManyMessagesOnSeparateConnections()
        {
            Func<ISession> connectionFactory = () =>
                new SmtpServerSession(new InMemoryCommandHandler(), new NullLog(), new SmtpServerSessionConfiguration());

            var serverConfiguration = new ServerConfiguration();

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

            var throttler = new SemaphoreSlim(initialCount: 5);

            var sendTasks = new List<Task>();

            for (int i = 0; i < 100; i++)
                sendTasks.Add(SendMessageThrottled(throttler, smtpServer.LocalEndpoint));

            Task.WaitAll(sendTasks.ToArray());
            

            var stopTask = smtpServer.StopAsync();

            Assert.IsTrue(stopTask.Wait(TimeSpan.FromMilliseconds(2000)));
            Assert.IsTrue(runTask.Wait(TimeSpan.FromMilliseconds(2000)));
        }
开发者ID:hmailserver,项目名称:hmailserver-net,代码行数:25,代码来源:SmtpVolumeTests.cs

示例3: TestSmtpEndToEnd

        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("[email protected]");
                message.To.Add(new MailAddress("[email protected]"));
                message.To.Add(new MailAddress("[email protected]"));
                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("[email protected]", commandHandler.MailFrom);
            Assert.AreEqual(2, commandHandler.Recipients.Count);
            Assert.AreEqual("[email protected]", commandHandler.Recipients[0]);
            Assert.AreEqual("[email protected]", 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);
        }
开发者ID:hmailserver,项目名称:hmailserver-net,代码行数:48,代码来源:SmtpServerTests.cs

示例4: SendMessageWithAttachment

        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("[email protected]");
                message.To.Add(new MailAddress("[email protected]"));
                message.To.Add(new MailAddress("[email protected]"));
                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;
        }
开发者ID:hmailserver,项目名称:hmailserver-net,代码行数:38,代码来源:SmtpMessageBodySizeTests.cs

示例5: TestSmtpEndToEndWithSsl

        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("[email protected]");
                message.To.Add(new MailAddress("[email protected]"));
                message.To.Add(new MailAddress("[email protected]"));
                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("[email protected]", commandHandler.MailFrom);
            Assert.AreEqual(2, commandHandler.Recipients.Count);
            Assert.AreEqual("[email protected]", commandHandler.Recipients[0]);
            Assert.AreEqual("[email protected]", 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);
        }
开发者ID:hmailserver,项目名称:hmailserver-net,代码行数:65,代码来源:SmtpServerTests.cs


注:本文中的Server.StopAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。