org.subethamail.smtp.server
Class SMTPServer

java.lang.Object
  extended by org.subethamail.smtp.server.SMTPServer

public class SMTPServer
extends Object

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. This class also manages a watchdog thread which will timeout stale connections. 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
SMTPServer(MessageHandlerFactory handlerFactory)
          Simple constructor.
SMTPServer(MessageHandlerFactory handlerFactory, AuthenticationHandlerFactory authHandlerFact)
          Constructor with AuthenticationHandlerFactory.
SMTPServer(MessageHandlerFactory msgHandlerFact, AuthenticationHandlerFactory authHandlerFact, ExecutorService executorService)
          Complex constructor.
 
Method Summary
protected  ServerSocket createServerSocket()
          Override this method if you want to create your own server sockets.
 SSLSocket createSSLSocket(Socket socket)
          Create a SSL socket that wraps the existing socket.
 AuthenticationHandlerFactory getAuthenticationHandlerFactory()
           
 int getBacklog()
          The backlog is the Socket backlog.
 InetAddress getBindAddress()
          null means all interfaces
 CommandHandler getCommandHandler()
          The CommandHandler manages handling the SMTP commands such as QUIT, MAIL, RCPT, DATA, etc.
 int getConnectionTimeout()
           
 boolean getDisableReceivedHeaders()
           
 boolean getDisableTLS()
          Deprecated. use enableTLS
 String getDisplayableLocalSocketAddress()
           
 boolean getEnableTLS()
           
 ExecutorService getExecutorService()
           
 boolean getHideTLS()
           
 String getHostName()
           
 int getMaxConnections()
           
 int getMaxMessageSize()
           
 int getMaxRecipients()
           
 MessageHandlerFactory getMessageHandlerFactory()
           
 int getPort()
           
 boolean getRequireTLS()
           
 SessionIdFactory getSessionIdFactory()
           
 String getSoftwareName()
          The string reported to the public as the software running here.
 boolean isRunning()
          Is the server running after start() has been called?
 void setAuthenticationHandlerFactory(AuthenticationHandlerFactory fact)
           
 void setBacklog(int backlog)
          The backlog is the Socket backlog.
 void setBindAddress(InetAddress bindAddress)
          null means all interfaces
 void setConnectionTimeout(int connectionTimeout)
          Set the number of milliseconds that the server will wait for client input.
 void setDisableReceivedHeaders(boolean disableReceivedHeaders)
           
 void setDisableTLS(boolean value)
          Deprecated. use setEnableTLS(boolean)
 void setEnableTLS(boolean enableTLS)
          If set to true, TLS will be supported.
 void setHideTLS(boolean value)
          If set to true, TLS will not be advertised in the EHLO string.
 void setHostName(String hostName)
          The host name that will be reported to SMTP clients
 void setMaxConnections(int maxConnections)
          Set's the maximum number of connections this server instance will accept.
 void setMaxMessageSize(int maxMessageSize)
           
 void setMaxRecipients(int maxRecipients)
          Set the maximum number of recipients allowed for each message.
 void setMessageHandlerFactory(MessageHandlerFactory fact)
           
 void setPort(int port)
           
 void setRequireTLS(boolean requireTLS)
           
 void setSessionIdFactory(SessionIdFactory sessionIdFactory)
          Sets the SessionIdFactory which will allocate a unique identifier for each mail sessions.
 void setSoftwareName(String value)
          Changes the publicly reported software information.
 void start()
          Call this method to get things rolling after instantiating the SMTPServer.
 void stop()
          Shut things down gracefully.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SMTPServer

public SMTPServer(MessageHandlerFactory handlerFactory)
Simple constructor.


SMTPServer

public SMTPServer(MessageHandlerFactory handlerFactory,
                  AuthenticationHandlerFactory authHandlerFact)
Constructor with AuthenticationHandlerFactory.


SMTPServer

public SMTPServer(MessageHandlerFactory msgHandlerFact,
                  AuthenticationHandlerFactory authHandlerFact,
                  ExecutorService executorService)
Complex constructor.

Parameters:
authHandlerFact - the AuthenticationHandlerFactory which 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. Use Session.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 by Executors.newCachedThreadPool().
Method Detail

getHostName

public String getHostName()
Returns:
the host name that will be reported to SMTP clients

setHostName

public void setHostName(String hostName)
The host name that will be reported to SMTP clients


getBindAddress

public InetAddress getBindAddress()
null means all interfaces


setBindAddress

public void setBindAddress(InetAddress bindAddress)
null means all interfaces


getPort

public int getPort()

setPort

public void setPort(int port)

getSoftwareName

public String getSoftwareName()
The string reported to the public as the software running here. Defaults to SubEthaSTP and the version number.


setSoftwareName

public void setSoftwareName(String value)
Changes the publicly reported software information.


getExecutorService

public ExecutorService 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.


stop

public void stop()
Shut things down gracefully.


createServerSocket

protected ServerSocket createServerSocket()
                                   throws IOException
Override this method if you want to create your own server sockets. You must return a bound ServerSocket instance

Throws:
IOException

createSSLSocket

public SSLSocket createSSLSocket(Socket socket)
                          throws IOException
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 by createServerSocket() (not null)
Returns:
a SSLSocket
Throws:
IOException - when creating the socket failed

getDisplayableLocalSocketAddress

public String getDisplayableLocalSocketAddress()

getMessageHandlerFactory

public MessageHandlerFactory getMessageHandlerFactory()
Returns:
the factory for message handlers, cannot be null

setMessageHandlerFactory

public void setMessageHandlerFactory(MessageHandlerFactory fact)

getAuthenticationHandlerFactory

public AuthenticationHandlerFactory getAuthenticationHandlerFactory()
Returns:
the factory for auth handlers, or null if no such factory has been set.

setAuthenticationHandlerFactory

public void setAuthenticationHandlerFactory(AuthenticationHandlerFactory fact)

getCommandHandler

public CommandHandler 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:

Up to SubEthaSMTP 3.1.5 the default was true, i.e. TLS was enabled.

See Also:
Securing Connections with TLS

getEnableTLS

public boolean getEnableTLS()

getDisableTLS

@Deprecated
public boolean getDisableTLS()
Deprecated. use enableTLS


setDisableTLS

@Deprecated
public void setDisableTLS(boolean value)
Deprecated. use setEnableTLS(boolean)


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.

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

public SessionIdFactory getSessionIdFactory()

setSessionIdFactory

public void setSessionIdFactory(SessionIdFactory sessionIdFactory)
Sets the SessionIdFactory which will allocate a unique identifier for each mail sessions. If not set, a reasonable default will be used.



Copyright © 2006-2012. All Rights Reserved.