forked from cosullivan/SmtpServer
-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
d5a5b2f
commit fbb5f1d
Showing
2 changed files
with
99 additions
and
224 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 |
---|---|---|
@@ -1,277 +1,152 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Net.Sockets; | ||
using System.Security.Authentication; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MailKit.Net.Smtp; | ||
using MimeKit; | ||
using MimeKit.Text; | ||
using SampleApp.Examples; | ||
using SmtpServer; | ||
using SmtpServer.Mail; | ||
using SampleApp.Examples; | ||
|
||
namespace SampleApp | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
if (args == null || args.Length == 0) | ||
{ | ||
var serverTask = RunServerAsync(cancellationTokenSource.Token); | ||
var clientTask1 = RunClientAsync("A", forceConnection: false, cancellationToken: cancellationTokenSource.Token); | ||
var clientTask2 = RunClientAsync("B", forceConnection: false, cancellationToken: cancellationTokenSource.Token); | ||
var clientTask3 = RunClientAsync("C", forceConnection: false, cancellationToken: cancellationTokenSource.Token); | ||
SecureServerExample.Run(); | ||
} | ||
|
||
Console.WriteLine("Press any key to continue"); | ||
Console.ReadKey(); | ||
//static void Main(string[] args) | ||
//{ | ||
// var cancellationTokenSource = new CancellationTokenSource(); | ||
|
||
// if (args == null || args.Length == 0) | ||
// { | ||
// var serverTask = RunServerAsync(cancellationTokenSource.Token); | ||
// var clientTask1 = RunClientAsync("A", forceConnection: false, cancellationToken: cancellationTokenSource.Token); | ||
// var clientTask2 = RunClientAsync("B", forceConnection: false, cancellationToken: cancellationTokenSource.Token); | ||
// var clientTask3 = RunClientAsync("C", forceConnection: false, cancellationToken: cancellationTokenSource.Token); | ||
|
||
cancellationTokenSource.Cancel(); | ||
// Console.WriteLine("Press any key to continue"); | ||
// Console.ReadKey(); | ||
|
||
serverTask.WaitWithoutException(); | ||
clientTask1.WaitWithoutException(); | ||
clientTask2.WaitWithoutException(); | ||
clientTask3.WaitWithoutException(); | ||
// cancellationTokenSource.Cancel(); | ||
|
||
return; | ||
} | ||
// serverTask.WaitWithoutException(); | ||
// clientTask1.WaitWithoutException(); | ||
// clientTask2.WaitWithoutException(); | ||
// clientTask3.WaitWithoutException(); | ||
|
||
if (args[0] == "server") | ||
{ | ||
var serverTask = RunServerAsync(cancellationTokenSource.Token); | ||
// return; | ||
// } | ||
|
||
Console.WriteLine("Press any key to continue"); | ||
Console.ReadKey(); | ||
// if (args[0] == "server") | ||
// { | ||
// var serverTask = RunServerAsync(cancellationTokenSource.Token); | ||
|
||
cancellationTokenSource.Cancel(); | ||
// Console.WriteLine("Press any key to continue"); | ||
// Console.ReadKey(); | ||
|
||
serverTask.WaitWithoutException(); | ||
// cancellationTokenSource.Cancel(); | ||
|
||
return; | ||
} | ||
// serverTask.WaitWithoutException(); | ||
|
||
if (args[0] == "client") | ||
{ | ||
var clientTask = RunClientAsync(args[1], cancellationToken: cancellationTokenSource.Token); | ||
// return; | ||
// } | ||
|
||
Console.WriteLine("Press any key to continue"); | ||
Console.ReadKey(); | ||
// if (args[0] == "client") | ||
// { | ||
// var clientTask = RunClientAsync(args[1], cancellationToken: cancellationTokenSource.Token); | ||
|
||
cancellationTokenSource.Cancel(); | ||
// Console.WriteLine("Press any key to continue"); | ||
// Console.ReadKey(); | ||
|
||
clientTask.WaitWithoutException(); | ||
} | ||
// cancellationTokenSource.Cancel(); | ||
|
||
if (args[0] == "folder") | ||
{ | ||
var clientTask = RunClientAsync(args[1], cancellationToken: cancellationTokenSource.Token); | ||
// clientTask.WaitWithoutException(); | ||
// } | ||
|
||
Console.WriteLine("Press any key to continue"); | ||
Console.ReadKey(); | ||
// if (args[0] == "folder") | ||
// { | ||
// var clientTask = RunClientAsync(args[1], cancellationToken: cancellationTokenSource.Token); | ||
|
||
cancellationTokenSource.Cancel(); | ||
// Console.WriteLine("Press any key to continue"); | ||
// Console.ReadKey(); | ||
|
||
clientTask.WaitWithoutException(); | ||
} | ||
} | ||
// cancellationTokenSource.Cancel(); | ||
|
||
static async Task RunServerAsync(CancellationToken cancellationToken) | ||
{ | ||
var options = new SmtpServerOptionsBuilder().Port(9025).Build(); | ||
// clientTask.WaitWithoutException(); | ||
// } | ||
//} | ||
|
||
var smtpServer = new SmtpServer.SmtpServer(options); | ||
//static async Task RunServerAsync(CancellationToken cancellationToken) | ||
//{ | ||
// var options = new SmtpServerOptionsBuilder().Port(9025).Build(); | ||
|
||
await smtpServer.StartAsync(cancellationToken); | ||
} | ||
// var smtpServer = new SmtpServer.SmtpServer(options); | ||
|
||
static async Task RunClientAsync( | ||
string name, | ||
int limit = Int32.MaxValue, | ||
bool forceConnection = true, | ||
CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
//var message = MimeKit.MimeMessage.Load(ParserOptions.Default, @"C:\Dev\Enron Corpus\maildir\allen-p\inbox\31_"); | ||
|
||
var message = new MimeMessage(); | ||
|
||
message.From.Add(new MailboxAddress("from@sample.com")); | ||
message.To.Add(new MailboxAddress("to@sample.com")); | ||
message.Subject = "Hello"; | ||
message.Body = new TextPart("plain") | ||
{ | ||
Text = "Hello World" | ||
}; | ||
|
||
var stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||
|
||
var counter = 1; | ||
using (var smtpClient = new SmtpClient()) | ||
{ | ||
while (limit-- > 0 && cancellationToken.IsCancellationRequested == false) | ||
{ | ||
//Console.WriteLine("Name={0} Count={1}", name, counter); | ||
|
||
try | ||
{ | ||
if (smtpClient.IsConnected == false) | ||
{ | ||
await smtpClient.ConnectAsync("localhost", 9025, false, cancellationToken); | ||
|
||
if (smtpClient.Capabilities.HasFlag(SmtpCapabilities.Authentication)) | ||
{ | ||
await smtpClient.AuthenticateAsync("user", "password", cancellationToken); | ||
} | ||
} | ||
|
||
await smtpClient.SendAsync(message, cancellationToken).ConfigureAwait(false); | ||
} | ||
catch (Exception exception) | ||
{ | ||
if (cancellationToken.IsCancellationRequested) | ||
{ | ||
break; | ||
} | ||
} | ||
|
||
if (forceConnection) | ||
{ | ||
await smtpClient.DisconnectAsync(true, cancellationToken); | ||
} | ||
|
||
counter++; | ||
} | ||
} | ||
|
||
stopwatch.Stop(); | ||
|
||
Console.WriteLine(); | ||
Console.WriteLine("{0} Finished.", name); | ||
Console.WriteLine(" {0} Messages Sent.", counter); | ||
Console.WriteLine(" {0} Time Taken (ms).", stopwatch.ElapsedMilliseconds); | ||
Console.WriteLine(" {0} Throughput (mps).", counter / (stopwatch.ElapsedMilliseconds / 1000.0)); | ||
} | ||
// await smtpServer.StartAsync(cancellationToken); | ||
//} | ||
|
||
//static async Task SendMessageAsync(SmtpClient smtpClient, string name, int counter, CancellationToken cancellationToken = default(CancellationToken)) | ||
//static async Task RunClientAsync( | ||
// string name, | ||
// int limit = Int32.MaxValue, | ||
// bool forceConnection = true, | ||
// CancellationToken cancellationToken = default(CancellationToken)) | ||
//{ | ||
// var message = new MimeKit.MimeMessage(); | ||
// message.From.Add(new MimeKit.MailboxAddress($"{name}{counter}@test.com")); | ||
// message.To.Add(new MimeKit.MailboxAddress("sample@test.com")); | ||
// message.Subject = $"{name} {counter}"; | ||
// //var message = MimeKit.MimeMessage.Load(ParserOptions.Default, @"C:\Dev\Enron Corpus\maildir\allen-p\inbox\31_"); | ||
|
||
// var message = new MimeMessage(); | ||
|
||
// message.Body = new TextPart(TextFormat.Plain) | ||
// message.From.Add(new MailboxAddress("from@sample.com")); | ||
// message.To.Add(new MailboxAddress("to@sample.com")); | ||
// message.Subject = "Hello"; | ||
// message.Body = new TextPart("plain") | ||
// { | ||
// Text = ".Assunto teste acento çãõáéíóú" | ||
// Text = "Hello World" | ||
// }; | ||
|
||
// await smtpClient.SendAsync(message, cancellationToken).ConfigureAwait(false); | ||
//} | ||
// var stopwatch = new Stopwatch(); | ||
// stopwatch.Start(); | ||
|
||
//static async Task RunFolderAsync(string folder, string pattern = "*", CancellationToken cancellationToken = default(CancellationToken)) | ||
//{ | ||
// foreach (var directory in Directory.GetDirectories(folder, "*", SearchOption.AllDirectories)) | ||
// var counter = 1; | ||
// using (var smtpClient = new SmtpClient()) | ||
// { | ||
// Console.WriteLine(directory); | ||
// cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
// foreach (var file in Directory.GetFiles(directory, pattern).ToList()) | ||
// while (limit-- > 0 && cancellationToken.IsCancellationRequested == false) | ||
// { | ||
// cancellationToken.ThrowIfCancellationRequested(); | ||
// Console.WriteLine(new FileInfo(file).Name); | ||
// //Console.WriteLine("Name={0} Count={1}", name, counter); | ||
|
||
// MimeKit.MimeMessage message; | ||
// try | ||
// { | ||
// message = MimeKit.MimeMessage.Load(ParserOptions.Default, file); | ||
// } | ||
// catch | ||
// { | ||
// continue; | ||
// } | ||
|
||
// using (var smtpClient = new SmtpClient()) | ||
// { | ||
// try | ||
// if (smtpClient.IsConnected == false) | ||
// { | ||
// await smtpClient.ConnectAsync("localhost", 9025, false, cancellationToken); | ||
// await smtpClient.AuthenticateAsync("user", "password", cancellationToken); | ||
|
||
// await smtpClient.SendAsync(message, cancellationToken).ConfigureAwait(false); | ||
// if (smtpClient.Capabilities.HasFlag(SmtpCapabilities.Authentication)) | ||
// { | ||
// await smtpClient.AuthenticateAsync("user", "password", cancellationToken); | ||
// } | ||
// } | ||
// catch (Exception exception) | ||
|
||
// await smtpClient.SendAsync(message, cancellationToken).ConfigureAwait(false); | ||
// } | ||
// catch (Exception exception) | ||
// { | ||
// if (cancellationToken.IsCancellationRequested) | ||
// { | ||
// Console.WriteLine(exception); | ||
// break; | ||
// } | ||
// } | ||
|
||
// if (forceConnection) | ||
// { | ||
// await smtpClient.DisconnectAsync(true, cancellationToken); | ||
// } | ||
// } | ||
// } | ||
//} | ||
|
||
//static async Task RunFileAsync(string file, CancellationToken cancellationToken = default(CancellationToken)) | ||
//{ | ||
// var message = MimeKit.MimeMessage.Load(ParserOptions.Default, file); | ||
|
||
// using (var smtpClient = new SmtpClient()) | ||
// { | ||
// try | ||
// { | ||
// await smtpClient.ConnectAsync("localhost", 9025, false, cancellationToken); | ||
// await smtpClient.AuthenticateAsync("user", "password", cancellationToken); | ||
|
||
// await smtpClient.SendAsync(message, cancellationToken).ConfigureAwait(false); | ||
// } | ||
// catch (Exception exception) | ||
// { | ||
// Console.WriteLine(exception); | ||
// counter++; | ||
// } | ||
|
||
// await smtpClient.DisconnectAsync(true, cancellationToken); | ||
// } | ||
//} | ||
|
||
|
||
//static void OnSmtpServerSessionCreated(object sender, SessionEventArgs e) | ||
//{ | ||
// Console.WriteLine("SessionCreated: {0}", e.Context.RemoteEndPoint); | ||
|
||
// e.Context.CommandExecuting += OnCommandExecuting; | ||
//} | ||
|
||
//static void OnCommandExecuting(object sender, SmtpCommandExecutingEventArgs e) | ||
//{ | ||
// new TracingSmtpCommandVisitor(Console.Out).Visit(e.Command); | ||
//} | ||
|
||
//static void OnSmtpServerSessionCompleted(object sender, SessionEventArgs e) | ||
//{ | ||
// e.Context.CommandExecuting -= OnCommandExecuting; | ||
|
||
// Console.WriteLine("SessionCompleted: {0}", e.Context.RemoteEndPoint); | ||
//} | ||
|
||
//static bool IgnoreCertificateValidationFailureForTestingOnly(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) | ||
//{ | ||
// return true; | ||
//} | ||
|
||
//static X509Certificate2 CreateCertificate() | ||
//{ | ||
// // to create an X509Certificate for testing you need to run MAKECERT.EXE and then PVK2PFX.EXE | ||
// // http://www.digitallycreated.net/Blog/38/using-makecert-to-create-certificates-for-development | ||
|
||
// var certificate = File.ReadAllBytes(@"C:\Dropbox\Documents\Cain\Programming\SmtpServer\SmtpServer.pfx"); | ||
// var password = File.ReadAllText(@"C:\Dropbox\Documents\Cain\Programming\SmtpServer\SmtpServerPassword.txt"); | ||
// stopwatch.Stop(); | ||
|
||
// return new X509Certificate2(certificate, password); | ||
// Console.WriteLine(); | ||
// Console.WriteLine("{0} Finished.", name); | ||
// Console.WriteLine(" {0} Messages Sent.", counter); | ||
// Console.WriteLine(" {0} Time Taken (ms).", stopwatch.ElapsedMilliseconds); | ||
// Console.WriteLine(" {0} Throughput (mps).", counter / (stopwatch.ElapsedMilliseconds / 1000.0)); | ||
//} | ||
} | ||
} |
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