static void Main(string[] args) { var client = new DnsClient(); var log = new Log(); var config = ServiceConfigurationReader.Read(); var container = new Container(new DependencyRegistry(config)); var smtpServerSessionConfiguration = new SmtpServerSessionConfiguration { TempDirectory = config.TempDirectory }; Func<ISession> smtpSessionFactory = () => new SmtpServerSession(new SmtpServerCommandHandler(container), log, smtpServerSessionConfiguration); var serverConfiguration = new ServerConfiguration() { Port = 25 }; var smtpServer = new Server(smtpSessionFactory, log, serverConfiguration); var smtpRunTask = smtpServer.RunAsync(); Func<ISession> pop3SessionFactory = () => new Pop3ServerSession(new Pop3ServerCommandHandler(container), log, new Pop3ServerSessionConfiguration()); var pop3ServerConfiguration = new ServerConfiguration() { Port = 110 }; var deliverer = new MessageDeliverer(container); var delivererTask = deliverer.RunAsync(); var pop3Server = new Server(pop3SessionFactory, log, pop3ServerConfiguration); var pop3RunTask = pop3Server.RunAsync(); log.LogApplicationInfo("Server running..."); Console.WriteLine("Press any key to exit"); Console.ReadLine(); log.LogApplicationInfo("Shutting down"); var smtpStopTask = smtpServer.StopAsync(); var pop3StopTask = pop3Server.StopAsync(); }
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("*****@*****.**"); message.To.Add(new MailAddress("*****@*****.**")); message.To.Add(new MailAddress("*****@*****.**")); 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("*****@*****.**", commandHandler.MailFrom); Assert.AreEqual(2, commandHandler.Recipients.Count); Assert.AreEqual("*****@*****.**", commandHandler.Recipients[0]); Assert.AreEqual("*****@*****.**", 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); }
public SmtpServerSession(ISmtpServerCommandHandler commandHandler, ILog log, SmtpServerSessionConfiguration configuration) { _commandHandler = commandHandler; _configuration = configuration; _log = log; }