Package ch.astorm.smtp4j
Class SmtpServer
java.lang.Object
ch.astorm.smtp4j.SmtpServer
- All Implemented Interfaces:
AutoCloseable
Simple SMTP server.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionSmtpServer(int port) Creates a newSmtpServerwith aDefaultSmtpMessageHandlerinstance to handle received messages.SmtpServer(int port, SmtpMessageHandler messageHandler, ThreadFactory threadFactory) Creates a newSmtpServer. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(SmtpServerListener listener) Registers the specifiedlistenerto the server's events.voidclose()Closes thisSmtpServerinstance and releases all the resources associated to it.jakarta.mail.SessionCreates a newSessioninstance that will send messages to this server.Returns the listeners of this server.Returns the currentSmtpMessageHandler.intgetPort()Returns the port on which theSmtpServerlisten to.Returns the basicPropertiesthat can be used forSession.booleanisClosed()Returns true if the server has been closed or is not started yet.booleanReturns true if theSmtpServeris started and is actually listening for new messages.Returns all the (newly) received messages.readReceivedMessages(long delayIfNoMessage, TimeUnit unit) Returns all the (newly) received messages.Returns a newSmtpMessageHandler.SmtpMessageReaderto read incoming messages.booleanremoveListener(SmtpServerListener listener) Removes the specifiedlistenerof the server's event notifications.voidstart()Starts the server.
-
Field Details
-
DEFAULT_PORT
public static int DEFAULT_PORTDefault 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 newSmtpServerwith aDefaultSmtpMessageHandlerinstance 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 thestartmethod is called.
-
SmtpServer
Creates a newSmtpServer. ThemessageHandlerwill always be notified first for theSmtpServerListenerevents and is NOT part of thelistenerslist.- Parameters:
port- The port to listen to. A value less or equal to zero indicates that a free port as to be discovered when thestartmethod is called.messageHandler- TheSmtpMessageHandlerused to receive messages or null to use a newDefaultSmtpMessageHandlerinstance.threadFactory- TheThreadFactoryto use.
-
-
Method Details
-
getSessionProperties
Returns the basicPropertiesthat can be used forSession. 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 newSessioninstance that will send messages to this server.- Returns:
- A new
Sessioninstance.
-
getMessageHandler
Returns the currentSmtpMessageHandler.- Returns:
- The current message handler.
-
receivedMessageReader
Returns a newSmtpMessageHandler.SmtpMessageReaderto read incoming messages.- Returns:
- A new {code SmtpMessageReader} instance.
- See Also:
-
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
readerhas 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
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
readerhas 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 ofdelayIfNoMessage.- Returns:
- A list with the newly received messages or an empty list.
- See Also:
-
getPort
public int getPort()Returns the port on which theSmtpServerlisten to. If the value is zero or less, then the port will be discovered when the server isstarted.- Returns:
- The port.
-
isRunning
public boolean isRunning()Returns true if theSmtpServeris started and is actually listening for new messages.- Returns:
- True if the server has been
startedand 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
Starts the server. If the server is already started, this method will raise andIllegalStateException.- Throws:
IOException
-
addListener
Registers the specifiedlistenerto the server's events.- Parameters:
listener- The listener to add.
-
removeListener
Removes the specifiedlistenerof the server's event notifications.- Parameters:
listener- The listener to remove.- Returns:
- True if the listener has been removed.
-
getListeners
Returns the listeners of this server. The returned list is live.- Returns:
- The listeners.
-
close
Closes thisSmtpServerinstance 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:
closein interfaceAutoCloseable- Throws:
IOException
-