コード例 #1
0
 private static void TrimSessions(Smtp4devDbContext dbContext, ServerOptions serverOptions)
 {
     dbContext.Sessions.RemoveRange(dbContext.Sessions.Where(s => s.EndDate.HasValue).OrderByDescending(m => m.EndDate).Skip(serverOptions.NumberOfSessionsToKeep));
 }
コード例 #2
0
ファイル: ImapServer.cs プロジェクト: zhouzu/smtp4dev
        private void OnServerOptionsChanged(ServerOptions serverOptions)
        {
            Stop();

            TryStart();
        }
コード例 #3
0
 private static void TrimMessages(Smtp4devDbContext dbContext, ServerOptions serverOptions)
 {
     dbContext.Messages.RemoveRange(dbContext.Messages.OrderByDescending(m => m.ReceivedDate).Skip(serverOptions.NumberOfMessagesToKeep));
 }
コード例 #4
0
ファイル: Smtp4devServer.cs プロジェクト: zemien/smtp4dev
        public Smtp4devServer(Func <Smtp4devDbContext> dbContextFactory, IOptions <ServerOptions> serverOptions, MessagesHub messagesHub, SessionsHub sessionsHub)
        {
            this.messagesHub      = messagesHub;
            this.sessionsHub      = sessionsHub;
            this.serverOptions    = serverOptions;
            this.dbContextFactory = dbContextFactory;


            System.Security.Cryptography.X509Certificates.X509Certificate2 cert = null;

            Console.WriteLine($"\nTLS mode: {serverOptions.Value.TlsMode}");

            if (serverOptions.Value.TlsMode != TlsMode.None)
            {
                if (!string.IsNullOrEmpty(serverOptions.Value.TlsCertificate))
                {
                    Console.WriteLine($"Using certificate from {serverOptions.Value.TlsCertificate}");
                    cert = new X509Certificate2(File.ReadAllBytes(serverOptions.Value.TlsCertificate), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
                }
                else
                {
                    string pfxPath = Path.GetFullPath("selfsigned-certificate.pfx");
                    string cerPath = Path.GetFullPath("selfsigned-certificate.cer");

                    if (File.Exists(pfxPath))
                    {
                        cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "", X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

                        if (cert.Subject != $"CN={serverOptions.Value.HostName}" || DateTime.Parse(cert.GetExpirationDateString()) < DateTime.Now.AddDays(30))
                        {
                            cert = null;
                        }
                        else
                        {
                            Console.WriteLine($"Using existing self-signed certificate with subject name '{serverOptions.Value.HostName} and expiry date {cert.GetExpirationDateString()}");
                        }
                    }

                    if (cert == null)
                    {
                        cert = CreateSelfSignedCertificate();
                        File.WriteAllBytes(pfxPath, cert.Export(X509ContentType.Pkcs12));
                        File.WriteAllBytes(cerPath, cert.Export(X509ContentType.Cert));
                        Console.WriteLine($"Generated new self-signed certificate with subject name '{serverOptions.Value.HostName} and expiry date {cert.GetExpirationDateString()}");
                    }

                    Console.WriteLine($"Ensure that the hostname you enter into clients and '{serverOptions.Value.HostName}' from ServerOptions:HostName configuration match exactly");
                    Console.WriteLine($"and trust the issuer certificate at {cerPath} in your client/OS to avoid certificate validation errors.");
                }
            }
            Console.WriteLine();

            ServerOptions serverOptionsValue = serverOptions.Value;

            this.smtpServer = new DefaultServer(serverOptionsValue.AllowRemoteConnections, serverOptionsValue.Port,
                                                serverOptionsValue.TlsMode == TlsMode.ImplicitTls ? cert : null,
                                                serverOptionsValue.TlsMode == TlsMode.StartTls ? cert : null
                                                );
            this.smtpServer.MessageReceivedEventHandler  += OnMessageReceived;
            this.smtpServer.SessionCompletedEventHandler += OnSessionCompleted;
            this.smtpServer.SessionStartedHandler        += OnSessionStarted;
            this.smtpServer.AuthenticationCredentialsValidationRequiredEventHandler += OnAuthenticationCredentialsValidationRequired;
        }