Class SmtpServer

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class SmtpServer
    extends java.lang.Object
    implements java.lang.AutoCloseable
    Simple SMTP server.
    See Also:
    SmtpServerBuilder
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int DEFAULT_PORT
      Default SMTP port.
    • Constructor Summary

      Constructors 
      Constructor Description
      SmtpServer​(int port)
      Creates a new SmtpServer.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearReceivedMessages()
      Clears all the received messages.
      void close()
      Closes this SmtpServer instance and releases all the resources associated to it.
      javax.mail.Session createSession()
      Creates a new Session instance that will send messages to this server.
      SmtpMessageHandler getMessageHandler()
      Returns the current SmtpMessageHandler.
      int getPort()
      Returns the port on which the SmtpServer listen to.
      java.util.List<SmtpMessage> getReceivedMessages()
      Returns all the received messages.
      java.util.Properties getSessionProperties()
      Returns the basic Properties that can be used for Session.
      boolean isClosed()
      Returns true if the server has been closed or is not started yet.
      void setMessageHandler​(SmtpMessageHandler handler)
      Defines the SmtpMessageHandler that will receive all the incoming messages.
      void start()
      Starts the server.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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 Detail

      • 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 Detail

      • getSessionProperties

        public java.util.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 javax.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.
      • clearReceivedMessages

        public void clearReceivedMessages()
        Clears all the received messages.
        See Also:
        SmtpMessageStorage.clear()
      • 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.
      • 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 java.io.IOException
        Starts the server. If the server is already started, this method will raise and IllegalStateException.
        Throws:
        java.io.IOException
      • close

        public void close()
                   throws java.io.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 java.lang.AutoCloseable
        Throws:
        java.io.IOException