-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cosullivan
committed
Jun 21, 2020
1 parent
ed92f42
commit b231b3e
Showing
7 changed files
with
220 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SmtpServer; | ||
|
||
namespace SampleApp.Examples | ||
{ | ||
public static class ServerCancellingExample | ||
{ | ||
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."); | ||
} | ||
|
||
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using SmtpServer; | ||
|
||
namespace SampleApp.Examples | ||
{ | ||
public static class ServerShutdownExample | ||
{ | ||
public static void Run() | ||
{ | ||
var cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
var options = new SmtpServerOptionsBuilder() | ||
.ServerName("SmtpServer SampleApp") | ||
.Port(9025) | ||
.MailboxFilter(new SampleMailboxFilter(TimeSpan.FromSeconds(2))) | ||
.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 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."); | ||
} | ||
|
||
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.