Class SmtpServer

java.lang.Object
ch.astorm.smtp4j.SmtpServer
All Implemented Interfaces:
AutoCloseable

public class SmtpServer extends Object implements AutoCloseable
Simple SMTP server.
See Also:
  • Field Details

    • DEFAULT_PORT

      public static int DEFAULT_PORT
      Default SMTP port. The port 25 is generally used for a simple SMTP relay. Ports 587 and 2525 uses explicit SSL/TLS connections whereas port 465 is for implicit SSL/TLS connections. See here for more information.
  • Constructor Details

    • SmtpServer

      public SmtpServer(int port)
      Creates a new SmtpServer.
      Parameters:
      port - The port to listen to. A value less or equal to zero indicates that a free port as to be discovered when the start method is called.
  • Method Details

    • getSessionProperties

      public Properties getSessionProperties()
      Returns the basic Properties that can be used for Session. If the port is dynamic, then the server must have been started before this method can be called.
      Returns:
      The properties for this server.
    • createSession

      public jakarta.mail.Session createSession()
      Creates a new Session instance that will send messages to this server.
      Returns:
      A new Session instance.
    • setMessageHandler

      public void setMessageHandler(SmtpMessageHandler handler)
      Defines the SmtpMessageHandler that will receive all the incoming messages. By default, all the messages are stored in a local SmtpMessageStorage.

      The handler will replace the current message handler that will not be notified of received messages anymore. It is possible to set it to null to restore default behavior.

      Parameters:
      handler - The handler or null.
    • getMessageHandler

      public SmtpMessageHandler getMessageHandler()
      Returns the current SmtpMessageHandler.
      Returns:
      The current message handler.
    • receivedMessageIterator

      public SmtpMessageHandler.SmtpMessageIterator receivedMessageIterator()
      Returns a SmtpMessageHandler.SmtpMessageIterator that will block until a new SmtpMessage is available.
      Returns:
      A new {code SmtpMessageIterator} instance.
      See Also:
    • readReceivedMessages

      public List<SmtpMessage> readReceivedMessages()
      Returns all the (newly) received messages. If no message has been received since the last invocation, an empty list will be returned.
      Returns:
      A list with the newly received messages or an empty list.
      See Also:
    • getReceivedMessages

      @Deprecated public List<SmtpMessage> getReceivedMessages()
      Deprecated.
      Returns all the received messages.
      Returns:
      The received messages.
      See Also:
    • clearReceivedMessages

      @Deprecated public void clearReceivedMessages()
      Deprecated.
      Clears all the received messages.
      See Also:
    • getPort

      public int getPort()
      Returns the port on which the SmtpServer listen to. If the value is zero or less, then the port will be discovered when the server is started.
      Returns:
      The port.
    • isRunning

      public boolean isRunning()
      Returns true if the SmtpServer is started and is actually listening for new messages.
      Returns:
      True if the server has been started and not yet closed.
    • isClosed

      public boolean isClosed()
      Returns true if the server has been closed or is not started yet.
      Returns:
      True if the server has been closed or is not started yet.
    • start

      public void start() throws IOException
      Starts the server. If the server is already started, this method will raise and IllegalStateException.
      Throws:
      IOException
    • addListener

      public void addListener(SmtpServerListener listener)
      Registers the specified listener to the server's events.
      Parameters:
      listener - The listener to add.
    • removeListener

      public boolean removeListener(SmtpServerListener listener)
      Removes the specified listener of the server's event notifications.
      Parameters:
      listener - The listener to remove.
      Returns:
      True if the listener has been removed.
    • getListeners

      public List<SmtpServerListener> getListeners()
      Returns the listeners of this server. The returned list is live.
      Returns:
      The listeners.
    • close

      public void close() throws IOException
      Closes this SmtpServer instance and releases all the resources associated to it. Once closed, it possible to restart it again. If the server is already closed, this method does nothing.
      Specified by:
      close in interface AutoCloseable
      Throws:
      IOException