示例#1
0
        public void RaisesEndpointStopped()
        {
            using (AutoResetEvent stopped = new AutoResetEvent(false))
            {
                ISmtpServerOptions builtOptions = null;
                Action <SmtpServerOptionsBuilder> configuration = options =>
                {
                    builtOptions = options.Build();
                };

                EndPointEventArgs   stoppedArgs = null;
                Action <SmtpServer> beforeStart = server =>
                {
                    server.EndPointStopped += (sender, args) =>
                    {
                        stoppedArgs = args;
                        stopped.Set();
                    };
                };

                using (CreateServer(configuration, beforeStart))
                {
                    // act
                    CancellationTokenSource.Cancel();

                    // assert
                    Assert.True(stopped.WaitOne(TestWaitTimeout));
                    Assert.Equal(builtOptions.Endpoints[0], stoppedArgs.EndPointDefinition);
                }
            }
        }
示例#2
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 /// <param name="tcpClient">The TCP client to operate the session on.</param>
 internal SmtpSession(ISmtpServerOptions options, TcpClient tcpClient)
 {
     _options      = options;
     _tcpClient    = tcpClient;
     _context      = new SmtpSessionContext(tcpClient);
     _stateMachine = new SmtpStateMachine(options, _context);
 }
示例#3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="options">The options that the server was created with.</param>
        /// <param name="address">The address.</param>
        /// <param name="parameters">The list of extended (ESMTP) parameters.</param>
        public MailCommand(ISmtpServerOptions options, IMailbox address, IReadOnlyDictionary <string, string> parameters)
        {
            _options = options;

            Address    = address;
            Parameters = parameters;
        }
 public void Update(ISmtpServerOptions smtpServerOptions)
 {
     AddOrUpdate(new SystemSettingDto
     {
         Name  = SmtpServer,
         Value = smtpServerOptions.SmtpServer
     });
     AddOrUpdate(new SystemSettingDto
     {
         Name  = SmtpServerPort,
         Value = smtpServerOptions.SmtpServerPort.ToString()
     });
     AddOrUpdate(new SystemSettingDto
     {
         Name  = SmtpSslEncryption,
         Value = smtpServerOptions.SmtpServerUseSSl.ToString()
     });
     AddOrUpdate(new SystemSettingDto
     {
         Name  = SmtpAuthentication,
         Value = smtpServerOptions.SmtpAuthentication.ToString()
     });
     AddOrUpdate(new SystemSettingDto
     {
         Name  = SmtpServerUsername,
         Value = productionCipher.Encrypt(smtpServerOptions.SmtpServerUser, encryptorKey)
     });
     AddOrUpdate(new SystemSettingDto
     {
         Name  = SmtpServerPassword,
         Value = productionCipher.Encrypt(smtpServerOptions.SmtpServerPassword, encryptorKey)
     });
 }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="tcpClient">The TCP client that the session is connected with.</param>
 internal SmtpSessionContext(ISmtpServerOptions options, TcpClient tcpClient)
 {
     Transaction    = new SmtpMessageTransaction();
     RemoteEndPoint = tcpClient.Client.RemoteEndPoint;
     Client         = new NetworkClient(tcpClient.GetStream(), options.NetworkBufferSize, options.NetworkBufferReadTimeout);
     Properties     = new Dictionary <string, object>();
 }
示例#6
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 /// <param name="serviceProvider">The service provider to use when resolving services.</param>
 public SmtpServer(ISmtpServerOptions options, IServiceProvider serviceProvider)
 {
     _options                 = options;
     _serviceProvider         = serviceProvider;
     _sessions                = new SmtpSessionManager(this);
     _endpointListenerFactory = serviceProvider.GetServiceOrDefault(EndpointListenerFactory.Default);
 }
示例#7
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="endpointDefinition">The endpoint definition.</param>
 internal SmtpSessionContext(ISmtpServerOptions options, IEndpointDefinition endpointDefinition)
 {
     ServerOptions      = options;
     EndpointDefinition = endpointDefinition;
     Transaction        = new SmtpMessageTransaction();
     Properties         = new Dictionary <string, object>();
 }
示例#8
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="tcpClient">The TCP client that the session is connected with.</param>
 /// <param name="networkClient">The network client to use for communications.</param>
 internal SmtpSessionContext(ISmtpServerOptions options, TcpClient tcpClient, INetworkClient networkClient)
 {
     ServerOptions  = options;
     Transaction    = new SmtpMessageTransaction();
     RemoteEndPoint = tcpClient.Client.RemoteEndPoint;
     NetworkClient  = networkClient;
     Properties     = new Dictionary <string, object>();
 }
示例#9
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="options">The server options.</param>
        protected SmtpCommand(ISmtpServerOptions options)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            Options = options;
        }
示例#10
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="options">The SMTP server options.</param>
        /// <param name="tcpClient">The TCP client to operate the session on.</param>
        /// <param name="stateMachine">The SMTP state machine.</param>
        internal SmtpSession(ISmtpServerOptions options, TcpClient tcpClient, SmtpStateMachine stateMachine)
        {
            _options   = options;
            _tcpClient = tcpClient;

            Context = new SmtpSessionContext(new SmtpTransaction(), stateMachine, tcpClient.Client.RemoteEndPoint)
            {
                Text = new NetworkTextStream(tcpClient.GetStream())
            };
        }
示例#11
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="options">The SMTP server options.</param>
        /// <param name="tcpClient">The TCP client to operate the session on.</param>
        /// <param name="stateMachine">The SMTP state machine.</param>
        internal SmtpSession(ISmtpServerOptions options, TcpClient tcpClient, SmtpStateMachine stateMachine)
        {
            _options = options;
            _tcpClient = tcpClient;
            _stateMachine = stateMachine;

            Context = new SmtpSessionContext(new SmtpTransaction(), stateMachine, tcpClient.Client.RemoteEndPoint)
            {
                Text = new NetworkTextStream(tcpClient)
            };
        }
        public EmailSettingsForm(ISmtpServerOptions smtpServerOptions,
                                 ISmtpConfigurationProvider smtpConfigurationProvider,
                                 SystemSettingsRepository systemSettingsRepository)
        {
            this.smtpServerOptions         = smtpServerOptions;
            this.smtpConfigurationProvider = smtpConfigurationProvider;
            this.systemSettingsRepository  = systemSettingsRepository;

            InitializeComponent();
            Translator.Translate(this);
        }
示例#13
0
        static async Task RunServerAsync(ISmtpServerOptions options, CancellationToken cancellationToken)
        {
            var smtpServer = new SmtpServer.SmtpServer(options);

            smtpServer.SessionCreated += OnSmtpServerSessionCreated;
            smtpServer.SessionCompleted += OnSmtpServerSessionCompleted;

            await smtpServer.StartAsync(cancellationToken);

            smtpServer.SessionCreated -= OnSmtpServerSessionCreated;
            smtpServer.SessionCompleted -= OnSmtpServerSessionCompleted;
        }
示例#14
0
        static async Task RunServerAsync(ISmtpServerOptions options, CancellationToken cancellationToken)
        {
            var smtpServer = new SmtpServer.SmtpServer(options);

            smtpServer.SessionCreated   += OnSmtpServerSessionCreated;
            smtpServer.SessionCompleted += OnSmtpServerSessionCompleted;

            await smtpServer.StartAsync(cancellationToken);

            smtpServer.SessionCreated   -= OnSmtpServerSessionCreated;
            smtpServer.SessionCompleted -= OnSmtpServerSessionCompleted;
        }
示例#15
0
        public SendMailForm(IEmailSender emailSender,
                            SystemSettingsRepository systemSettingsRepository,
                            UserProfileForm userProfileForm,
                            EmailSettingsForm emailSettingsForm)
        {
            this.emailSender       = emailSender;
            this.userProfileForm   = userProfileForm;
            this.emailSettingsForm = emailSettingsForm;
            smtpServerOptions      = systemSettingsRepository.GetSmtpServerOptions();

            InitializeComponent();
            Translator.Translate(this);
        }
        public MailServerService(IServiceProvider serviceProvider, MailServerConfiguration mailServerConfiguration, ILogger <MailServerService> logger)
        {
            this.serviceProvider         = serviceProvider;
            this.mailServerConfiguration = mailServerConfiguration;
            this.logger  = logger;
            this.options = new SmtpServerOptionsBuilder()
                           .ServerName(this.mailServerConfiguration.Domain)
                           .Port(25, false)
                           .CommandWaitTimeout(TimeSpan.FromSeconds(30))
                           .Build();

            this.messageStore = this.serviceProvider.Resolve <TempMessageStore>();
        }
示例#17
0
        public MailServer(int?numMessages = null, int?memoryLimit = null)
        {
            var options = new SmtpServerOptionsBuilder()
                          .ServerName("localhost")
                          .Port(25, 587)
                          .Port(465, isSecure: true)
                          //.Certificate(CreateX509Certificate2())
                          .MessageStore(new InMemoryMessageStore(numMessages, memoryLimit))
                          .MailboxFilter(new CustomMailboxFilter())
                          .UserAuthenticator(new CustomUserAuthenticator())
                          .Build();

            _options = options;
        }
示例#18
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="domain">The domain name.</param>
 internal HeloCommand(ISmtpServerOptions options, string domain) : base(options)
 {
     Domain = domain;
 }
示例#19
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 /// <param name="parser">The ABNF SMTP argument parser.</param>
 public SmtpCommandFactory(ISmtpServerOptions options, SmtpParser parser)
 {
     _options = options;
     _parser  = parser;
 }
示例#20
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="options">The options to assist when configuring the state machine.</param>
        /// <param name="commandFactory">The SMTP command factory.</param>
        public SmtpStateMachine(ISmtpServerOptions options, SmtpCommandFactory commandFactory)
        {
            _stateTable = new StateTable
            {
                new State(SmtpState.Initialized)
                {
#if DEBUG
                    { "DBUG", commandFactory.TryMakeDbug },
#endif
                    { "NOOP", commandFactory.TryMakeNoop },
                    { "RSET", commandFactory.TryMakeRset },
                    { "QUIT", commandFactory.TryMakeQuit },
                    { "HELO", commandFactory.TryMakeHelo, SmtpState.WaitingForMail },
                    { "EHLO", commandFactory.TryMakeEhlo, SmtpState.WaitingForMail },
                },
                new State(SmtpState.WaitingForMail)
                {
#if DEBUG
                    { "DBUG", commandFactory.TryMakeDbug },
#endif
                    { "NOOP", commandFactory.TryMakeNoop },
                    { "RSET", commandFactory.TryMakeRset },
                    { "QUIT", commandFactory.TryMakeQuit },
                    { "HELO", commandFactory.TryMakeHelo, SmtpState.WaitingForMail },
                    { "EHLO", commandFactory.TryMakeEhlo, SmtpState.WaitingForMail },
                    { "MAIL", commandFactory.TryMakeMail, SmtpState.WithinTransaction },
                    { "STARTTLS", commandFactory.TryMakeStartTls, SmtpState.WaitingForMailSecure },
                },
                new State(SmtpState.WaitingForMailSecure)
                {
#if DEBUG
                    { "DBUG", commandFactory.TryMakeDbug },
#endif
                    { "NOOP", commandFactory.TryMakeNoop },
                    { "RSET", commandFactory.TryMakeRset },
                    { "QUIT", commandFactory.TryMakeQuit },
                    { "AUTH", commandFactory.TryMakeAuth },
                    { "HELO", commandFactory.TryMakeHelo, SmtpState.WaitingForMailSecure },
                    { "EHLO", commandFactory.TryMakeEhlo, SmtpState.WaitingForMailSecure },
                    { "MAIL", commandFactory.TryMakeMail, SmtpState.WithinTransaction }
                },
                new State(SmtpState.WithinTransaction)
                {
#if DEBUG
                    { "DBUG", commandFactory.TryMakeDbug },
#endif
                    { "NOOP", commandFactory.TryMakeNoop },
                    { "RSET", commandFactory.TryMakeRset },
                    { "QUIT", commandFactory.TryMakeQuit },
                    { "RCPT", commandFactory.TryMakeRcpt, SmtpState.CanAcceptData },
                },
                new State(SmtpState.CanAcceptData)
                {
#if DEBUG
                    { "DBUG", commandFactory.TryMakeDbug },
#endif
                    { "NOOP", commandFactory.TryMakeNoop },
                    { "RSET", commandFactory.TryMakeRset },
                    { "QUIT", commandFactory.TryMakeQuit },
                    { "RCPT", commandFactory.TryMakeRcpt },
                    { "DATA", commandFactory.TryMakeData, SmtpState.WaitingForMail },
                }
            };

            if (options.AllowUnsecureAuthentication)
            {
                _stateTable[SmtpState.WaitingForMail].Add("AUTH", commandFactory.TryMakeAuth);
            }

            _stateTable.Initialize(SmtpState.Initialized);
        }
示例#21
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 public SmtpServer(ISmtpServerOptions options)
 {
     _options = options;
 }
示例#22
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="domainOrAddress">The domain name or address literal.</param>
 /// <param name="options">The list of server options.</param>
 public EhloCommand(string domainOrAddress, ISmtpServerOptions options)
 {
     _domainOrAddress = domainOrAddress;
     _options         = options;
 }
示例#23
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="options">The options to assist when configuring the state machine.</param>
        /// <param name="context">The SMTP server session context.</param>
        internal SmtpStateMachine(ISmtpServerOptions options, SmtpSessionContext context)
        {
            _options = options;
            _context = context;
            _context.SessionAuthenticated += OnSessionAuthenticated;
            _stateTable = new StateTable
            {
                new State(SmtpState.Initialized)
                {
#if DEBUG
                    { "DBUG", TryMakeDbug },
#endif
                    { "NOOP", TryMakeNoop },
                    { "RSET", TryMakeRset },
                    { "QUIT", TryMakeQuit },
                    { "HELO", TryMakeHelo, SmtpState.WaitingForMail },
                    { "EHLO", TryMakeEhlo, SmtpState.WaitingForMail },
                },
                new State(SmtpState.WaitingForMail)
                {
#if DEBUG
                    { "DBUG", TryMakeDbug },
#endif
                    { "NOOP", TryMakeNoop },
                    { "RSET", TryMakeRset },
                    { "QUIT", TryMakeQuit },
                    { "HELO", TryMakeHelo, SmtpState.WaitingForMail },
                    { "EHLO", TryMakeEhlo, SmtpState.WaitingForMail },
                    { "MAIL", TryMakeMail, SmtpState.WithinTransaction },
                    { "STARTTLS", TryMakeStartTls, SmtpState.WaitingForMailSecure },
                },
                new State(SmtpState.WaitingForMailSecure)
                {
#if DEBUG
                    { "DBUG", TryMakeDbug },
#endif
                    { "NOOP", TryMakeNoop },
                    { "RSET", TryMakeRset },
                    { "QUIT", TryMakeQuit },
                    { "AUTH", TryMakeAuth },
                    { "HELO", TryMakeHelo, SmtpState.WaitingForMailSecure },
                    { "EHLO", TryMakeEhlo, SmtpState.WaitingForMailSecure },
                    { "MAIL", TryMakeMail, SmtpState.WithinTransaction }
                },
                new State(SmtpState.WithinTransaction)
                {
#if DEBUG
                    { "DBUG", TryMakeDbug },
#endif
                    { "NOOP", TryMakeNoop },
                    { "RSET", TryMakeRset },
                    { "QUIT", TryMakeQuit },
                    { "RCPT", TryMakeRcpt, SmtpState.CanAcceptData },
                },
                new State(SmtpState.CanAcceptData)
                {
#if DEBUG
                    { "DBUG", TryMakeDbug },
#endif
                    { "NOOP", TryMakeNoop },
                    { "RSET", TryMakeRset },
                    { "QUIT", TryMakeQuit },
                    { "RCPT", TryMakeRcpt },
                    { "DATA", TryMakeData, SmtpState.WaitingForMail },
                }
            };

            if (options.AllowUnsecureAuthentication)
            {
                _stateTable[SmtpState.WaitingForMail].Add("AUTH", TryMakeAuth);
            }

            _stateTable.Initialize(SmtpState.Initialized);
        }
示例#24
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="domainOrAddress">The domain name or address literal.</param>
 /// <param name="options">The list of server options.</param>
 public EhloCommand(string domainOrAddress, ISmtpServerOptions options)
 {
     _domainOrAddress = domainOrAddress;
     _options = options;
 }
示例#25
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="method">The authentication method.</param>
 /// <param name="parameter">The authentication parameter.</param>
 internal AuthCommand(ISmtpServerOptions options, AuthenticationMethod method, string parameter) : base(options)
 {
     Method    = method;
     Parameter = parameter;
 }
示例#26
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 /// <param name="enumerator">The token enumerator to handle the incoming tokens.</param>
 public SmtpParser(ISmtpServerOptions options, ITokenEnumerator enumerator) : base(enumerator)
 {
     _options = options;
 }
示例#27
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 internal DataCommand(ISmtpServerOptions options) : base(options)
 {
 }
示例#28
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="address">The address.</param>
 /// <param name="parameters">The list of extended (ESMTP) parameters.</param>
 internal MailCommand(ISmtpServerOptions options, IMailbox address, IReadOnlyDictionary <string, string> parameters) : base(options)
 {
     Address    = address;
     Parameters = parameters;
 }
示例#29
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The options that the server is running within.</param>
 public DataCommand(ISmtpServerOptions options)
 {
     _options = options;
 }
示例#30
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 /// <param name="parser">The ABNF SMTP argument parser.</param>
 public SmtpCommandFactory(ISmtpServerOptions options, SmtpParser parser)
 {
     _options = options;
     _parser = parser;
 }
示例#31
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 public SmtpServer(ISmtpServerOptions options)
 {
     _options = options;
 }
示例#32
0
 public ProxyCommand(ISmtpServerOptions options, IPEndPoint sourceEndpoint, IPEndPoint destinationEndpoint) : base(options)
 {
     SourceEndpoint      = sourceEndpoint;
     DestinationEndpoint = destinationEndpoint;
 }
示例#33
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 internal RsetCommand(ISmtpServerOptions options) : base(options)
 {
 }
示例#34
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="domainOrAddress">The domain name.</param>
 internal HeloCommand(ISmtpServerOptions options, string domainOrAddress) : base(options)
 {
     DomainOrAddress = domainOrAddress;
 }
示例#35
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The server options.</param>
 /// <param name="address">The address.</param>
 internal RcptCommand(ISmtpServerOptions options, IMailbox address) : base(options)
 {
     Address = address;
 }
示例#36
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="options">The SMTP server options.</param>
 public SmtpServer(ISmtpServerOptions options)
 {
     _options  = options;
     _sessions = new SessionManager(this);
 }