forked from cosullivan/SmtpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerCancellingExample.cs
More file actions
65 lines (51 loc) · 2.06 KB
/
Copy pathServerCancellingExample.cs
File metadata and controls
65 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Threading;
using System.Threading.Tasks;
using SmtpServer;
using SmtpServer.ComponentModel;
namespace SampleApp.Examples
{
public static class ServerCancellingExample
{
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(5)));
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 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.");
}
static void OnSessionCreated(object sender, SessionEventArgs e)
{
Console.WriteLine("Session Created.");
}
static void OnSessionCompleted(object sender, SessionEventArgs e)
{
Console.WriteLine("Session Completed");
}
static void OnSessionFaulted(object sender, SessionFaultedEventArgs e)
{
Console.WriteLine("Session Faulted: {0}", e.Exception);
}
static void OnSessionCancelled(object sender, SessionEventArgs e)
{
Console.WriteLine("Session Cancelled");
}
}
}