Package org.subethamail.smtp.server
Class SMTPServer
java.lang.Object
org.subethamail.smtp.server.SMTPServer
Main SMTPServer class. Construct this object, set the
hostName, port, and bind address if you wish to override the
defaults, and call start().
This class starts opens a ServerSocket and creates a new
instance of the ConnectionHandler class when a new connection
comes in. The ConnectionHandler then parses the incoming SMTP
stream and hands off the processing to the CommandHandler which
will execute the appropriate SMTP command class.
To use this class, construct a server with your implementation
of the MessageHandlerFactory. This provides low-level callbacks
at various phases of the SMTP exchange. For a higher-level
but more limited interface, you can pass in a
org.subethamail.smtp.helper.SimpleMessageListenerAdapter.
By default, no authentication methods are offered. To use
authentication, set an AuthenticationHandlerFactory.
- Author:
- Jon Stevens, Ian McFarland <ian@neo.com>, Jeff Schnitzer
-
Constructor Summary
ConstructorsConstructorDescriptionSMTPServer(MessageHandlerFactory handlerFactory) Simple constructor.SMTPServer(MessageHandlerFactory handlerFactory, AuthenticationHandlerFactory authHandlerFact) Constructor withAuthenticationHandlerFactory.SMTPServer(MessageHandlerFactory msgHandlerFact, AuthenticationHandlerFactory authHandlerFact, ExecutorService executorService) Complex constructor. -
Method Summary
Modifier and TypeMethodDescriptionprotected ServerSocketOverride this method if you want to create your own server sockets.createSSLSocket(Socket socket) Create a SSL socket that wraps the existing socket.intThe backlog is the Socket backlog.null means all interfacesThe CommandHandler manages handling the SMTP commands such as QUIT, MAIL, RCPT, DATA, etc.intbooleanbooleanDeprecated.booleanbooleanintintintintgetPort()booleanbooleanThe string reported to the public as the software running here.booleanIs the server running after start() has been called?voidvoidsetBacklog(int backlog) The backlog is the Socket backlog.voidsetBindAddress(InetAddress bindAddress) null means all interfacesvoidsetConnectionTimeout(int connectionTimeout) Set the number of milliseconds that the server will wait for client input.voidsetDisableReceivedHeaders(boolean disableReceivedHeaders) voidsetDisableTLS(boolean value) Deprecated.voidsetEnableTLS(boolean enableTLS) If set to true, TLS will be supported.voidsetHideTLS(boolean value) If set to true, TLS will not be advertised in the EHLO string.voidsetHostName(String hostName) The host name that will be reported to SMTP clientsvoidsetMaxConnections(int maxConnections) Set's the maximum number of connections this server instance will accept.voidsetMaxMessageSize(int maxMessageSize) voidsetMaxRecipients(int maxRecipients) Set the maximum number of recipients allowed for each message.voidvoidsetPort(int port) voidsetRequireAuth(boolean requireAuth) voidsetRequireTLS(boolean requireTLS) voidsetSessionIdFactory(SessionIdFactory sessionIdFactory) Sets theSessionIdFactorywhich will allocate a unique identifier for each mail sessions.voidsetSoftwareName(String value) Changes the publicly reported software information.voidstart()Call this method to get things rolling after instantiating the SMTPServer.voidstop()Shut things down gracefully.
-
Constructor Details
-
SMTPServer
Simple constructor. -
SMTPServer
public SMTPServer(MessageHandlerFactory handlerFactory, AuthenticationHandlerFactory authHandlerFact) Constructor withAuthenticationHandlerFactory. -
SMTPServer
public SMTPServer(MessageHandlerFactory msgHandlerFact, AuthenticationHandlerFactory authHandlerFact, ExecutorService executorService) Complex constructor.- Parameters:
authHandlerFact- theAuthenticationHandlerFactorywhich performs authentication in the SMTP AUTH command. If null, authentication is not supported. Note that setting an authentication handler does not enforce authentication, it only makes authentication possible. Enforcing authentication is the responsibility of the client application, which usually enforces it only selectively. UseSession.isAuthenticated()to check whether the client was authenticated in the session.executorService- the ExecutorService which will handle client connections, one task per connection. The SMTPServer will shut down this ExecutorService when the SMTPServer itself stops. If null, a default one is created byExecutors.newCachedThreadPool().
-
-
Method Details
-
getHostName
- Returns:
- the host name that will be reported to SMTP clients
-
setHostName
The host name that will be reported to SMTP clients -
getBindAddress
null means all interfaces -
setBindAddress
null means all interfaces -
getPort
public int getPort() -
setPort
public void setPort(int port) -
getSoftwareName
The string reported to the public as the software running here. Defaults to SubEthaSTP and the version number. -
setSoftwareName
Changes the publicly reported software information. -
getExecutorService
- Returns:
- the ExecutorService handling client connections
-
isRunning
public boolean isRunning()Is the server running after start() has been called? -
getBacklog
public int getBacklog()The backlog is the Socket backlog. The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed.- Returns:
- the backlog
-
setBacklog
public void setBacklog(int backlog) The backlog is the Socket backlog. The backlog argument must be a positive value greater than 0. If the value passed if equal or less than 0, then the default value will be assumed. -
start
public void start()Call this method to get things rolling after instantiating the SMTPServer.An SMTPServer which has been shut down, must not be reused.
-
stop
public void stop()Shut things down gracefully. -
createServerSocket
Override this method if you want to create your own server sockets. You must return a bound ServerSocket instance- Throws:
IOException
-
createSSLSocket
Create a SSL socket that wraps the existing socket. This method is called after the client issued the STARTTLS command.Subclasses may override this method to configure the key stores, enabled protocols/ cipher suites, enforce client authentication, etc.
- Parameters:
socket- the existing socket as created bycreateServerSocket()(not null)- Returns:
- a SSLSocket
- Throws:
IOException- when creating the socket failed
-
getDisplayableLocalSocketAddress
-
getMessageHandlerFactory
- Returns:
- the factory for message handlers, cannot be null
-
setMessageHandlerFactory
-
getAuthenticationHandlerFactory
- Returns:
- the factory for auth handlers, or null if no such factory has been set.
-
setAuthenticationHandlerFactory
-
getCommandHandler
The CommandHandler manages handling the SMTP commands such as QUIT, MAIL, RCPT, DATA, etc.- Returns:
- An instance of CommandHandler
-
getMaxConnections
public int getMaxConnections() -
setMaxConnections
public void setMaxConnections(int maxConnections) Set's the maximum number of connections this server instance will accept.- Parameters:
maxConnections-
-
getConnectionTimeout
public int getConnectionTimeout() -
setConnectionTimeout
public void setConnectionTimeout(int connectionTimeout) Set the number of milliseconds that the server will wait for client input. Sometime after this period expires, an client will be rejected and the connection closed. -
getMaxRecipients
public int getMaxRecipients() -
setMaxRecipients
public void setMaxRecipients(int maxRecipients) Set the maximum number of recipients allowed for each message. A value of -1 means "unlimited". -
setEnableTLS
public void setEnableTLS(boolean enableTLS) If set to true, TLS will be supported.The minimal JSSE configuration necessary for a working TLS support on Oracle JRE 6:
- javax.net.ssl.keyStore system property must refer to a file containing a JKS keystore with the private key.
- javax.net.ssl.keyStorePassword system property must specify the keystore password.
Up to SubEthaSMTP 3.1.5 the default was true, i.e. TLS was enabled.
- See Also:
-
getEnableTLS
public boolean getEnableTLS() -
getDisableTLS
Deprecated.useenableTLS -
setDisableTLS
Deprecated. -
getHideTLS
public boolean getHideTLS() -
setHideTLS
public void setHideTLS(boolean value) If set to true, TLS will not be advertised in the EHLO string. Default is false; true implied when disableTLS=true. -
getRequireTLS
public boolean getRequireTLS() -
setRequireTLS
public void setRequireTLS(boolean requireTLS) - Parameters:
requireTLS- true to require a TLS handshake, false to allow operation with or without TLS. Default is false; ignored when disableTLS=true.
-
getRequireAuth
public boolean getRequireAuth() -
setRequireAuth
public void setRequireAuth(boolean requireAuth) - Parameters:
requireAuth- true for mandatory smtp authentication, i.e. no mail mail be accepted until authentication succeeds. Don't forget to set AuthenticationHandlerFactory to allow client authentication. Defaults to false.
-
getMaxMessageSize
public int getMaxMessageSize()- Returns:
- the maxMessageSize
-
setMaxMessageSize
public void setMaxMessageSize(int maxMessageSize) - Parameters:
maxMessageSize- the maxMessageSize to set
-
getDisableReceivedHeaders
public boolean getDisableReceivedHeaders() -
setDisableReceivedHeaders
public void setDisableReceivedHeaders(boolean disableReceivedHeaders) - Parameters:
disableReceivedHeaders- false to include Received headers. Default is false.
-
getSessionIdFactory
-
setSessionIdFactory
Sets theSessionIdFactorywhich will allocate a unique identifier for each mail sessions. If not set, a reasonable default will be used.
-
enableTLS