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 with a DefaultSmtpMessageHandler instance to handle received messages.
      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.
    • SmtpServer

      public SmtpServer(int port, SmtpMessageHandler messageHandler, ThreadFactory threadFactory)
      Creates a new SmtpServer. The messageHandler will always be notified first for the SmtpServerListener events and is NOT part of the listeners list.
      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.
      messageHandler - The SmtpMessageHandler used to receive messages or null to use a new DefaultSmtpMessageHandler instance.
      threadFactory - The ThreadFactory to use.
  • 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.
    • getMessageHandler

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

      public SmtpMessageHandler.SmtpMessageReader receivedMessageReader()
      Returns a new SmtpMessageHandler.SmtpMessageReader to read incoming messages.
      Returns:
      A new {code SmtpMessageReader} 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.

      Note that if a reader has been created, this method will compete over the same list, hence the messages returned won't be received through the reader.

      In case there is no message, this method will wait 200 milliseconds before returning to let a chance for any new message to arrive.

      Returns:
      A list with the newly received messages or an empty list.
      See Also:
    • readReceivedMessages

      public List<SmtpMessage> readReceivedMessages(long delayIfNoMessage, TimeUnit unit)
      Returns all the (newly) received messages. If no message has been received since the last invocation, an empty list will be returned.

      Note that if a reader has been created, this method will compete over the same list, hence the messages returned won't be received through the reader.

      Parameters:
      delayIfNoMessage - Delay to wait if there is no message or a negative value to return immediately.
      unit - The time unit of delayIfNoMessage.
      Returns:
      A list with the newly received messages or an empty list.
      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