org.subethamail.smtp.server
Class SMTPServer

java.lang.Object
  extended by org.subethamail.smtp.server.SMTPServer
All Implemented Interfaces:
java.lang.Runnable

public class SMTPServer
extends java.lang.Object
implements java.lang.Runnable

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)
          The primary constructor.
SMTPServer(MessageHandlerFactory msgHandlerFact, AuthenticationHandlerFactory authHandlerFact)
          The primary constructor.
 
Method Summary
protected  void closeServerSocket()
          Closes the serverSocket in an orderly way
protected  java.net.ServerSocket createServerSocket()
          Override this method if you want to create your own server sockets.
 AuthenticationHandlerFactory getAuthenticationHandlerFactory()
           
 int getBacklog()
          The backlog is the Socket backlog.
 java.net.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 getHideTLS()
           
 java.lang.String getHostName()
           
 int getMaxConnections()
           
 int getMaxRecipients()
           
 MessageHandlerFactory getMessageHandlerFactory()
           
 java.lang.String getName()
           
 java.lang.String getNameVersion()
           
 int getNumberOfConnections()
           
 int getPort()
           
protected  java.lang.ThreadGroup getSessionGroup()
           
 boolean hasTooManyConnections()
           
 boolean isRunning()
          Is the server running after start() has been called?
 void run()
          This method is called by this thread when it starts up.
 void setAuthenticationHandlerFactory(AuthenticationHandlerFactory fact)
           
 void setBacklog(int backlog)
          The backlog is the Socket backlog.
 void setBindAddress(java.net.InetAddress bindAddress)
          null means all interfaces
 void setConnectionTimeout(int connectionTimeout)
          Set the number of milliseconds that the server will wait for client input.
 void setHideTLS(boolean value)
          If set to true, TLS will not be advertised in the EHLO string.
 void setHostName(java.lang.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 setMaxRecipients(int maxRecipients)
          Set the maximum number of recipients allowed for each message.
 void setMessageHandlerFactory(MessageHandlerFactory fact)
           
 void setPort(int port)
           
 void start()
          Call this method to get things rolling after instantiating the SMTPServer.
 void stop()
          Shut things down gracefully.
protected  void stopAllSessions()
          Grabs all instances of Sessions and attempts to close the socket if it is still open.
protected  void stopServerThread()
          Shuts down the server thread and the associated server socket in an orderly fashion.
 
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)
The primary constructor.


SMTPServer

public SMTPServer(MessageHandlerFactory msgHandlerFact,
                  AuthenticationHandlerFactory authHandlerFact)
The primary constructor.

Method Detail

getHostName

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

setHostName

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


getBindAddress

public java.net.InetAddress getBindAddress()
null means all interfaces


setBindAddress

public void setBindAddress(java.net.InetAddress bindAddress)
null means all interfaces


getPort

public int getPort()

setPort

public void setPort(int port)

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.


stopAllSessions

protected void stopAllSessions()
Grabs all instances of Sessions and attempts to close the socket if it is still open. Note this must be called after the main server socket is shut down, otherwise new sessions could be created while we are shutting them down.


createServerSocket

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

Throws:
java.io.IOException

closeServerSocket

protected void closeServerSocket()
Closes the serverSocket in an orderly way


stopServerThread

protected void stopServerThread()
Shuts down the server thread and the associated server socket in an orderly fashion.


run

public void run()
This method is called by this thread when it starts up. To safely cause this to exit, call stopServerThread().

Specified by:
run in interface java.lang.Runnable

getName

public java.lang.String getName()

getNameVersion

public java.lang.String getNameVersion()

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

getSessionGroup

protected java.lang.ThreadGroup getSessionGroup()

getNumberOfConnections

public int getNumberOfConnections()

hasTooManyConnections

public boolean hasTooManyConnections()

getMaxConnections

public int getMaxConnections()

setMaxConnections

public void setMaxConnections(int maxConnections)
Set's the maximum number of connections this server instance will accept. A value of -1 means "unlimited".

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".


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.



Copyright © 2009 SubEthaSMTP. All Rights Reserved. Build version: UNVERSIONED