本文整理汇总了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)));
}
示例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)));
}
示例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);
}
示例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;
}
示例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);
}