Package ch.astorm.smtp4j
Class SmtpServer
- java.lang.Object
-
- ch.astorm.smtp4j.SmtpServer
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class SmtpServer extends java.lang.Object implements java.lang.AutoCloseableSimple SMTP server.- See Also:
SmtpServerBuilder
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_PORTDefault SMTP port.
-
Constructor Summary
Constructors Constructor Description SmtpServer(int port)Creates a newSmtpServer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearReceivedMessages()Clears all the received messages.voidclose()Closes thisSmtpServerinstance and releases all the resources associated to it.intgetBufferSize()Returns the socket buffer size (in bytes).SmtpMessageHandlergetMessageHandler()Returns the currentSmtpMessageHandler.intgetPort()Returns the port on which theSmtpServerlisten to.java.util.List<SmtpMessage>getReceivedMessages()Returns all the received messages.booleanisClosed()Returns true if the server has been closed or is not started yet.voidsetBufferSize(int size)Defines the socket buffer size (in bytes).voidsetMessageHandler(SmtpMessageHandler handler)Defines theSmtpMessageHandlerthat will receive all the incoming messages.voidstart()Starts the server.
-
-
-
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 newSmtpServer.- 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.
-
-
Method Detail
-
setMessageHandler
public void setMessageHandler(SmtpMessageHandler handler)
Defines theSmtpMessageHandlerthat will receive all the incoming messages. By default, all the messages are stored in a localSmtpMessageStorage.The
handlerwill replace the current message handler that will not be notified of received messages anymore. It is possible to set it tonullto restore default behavior.- Parameters:
handler- The handler or null.
-
getMessageHandler
public SmtpMessageHandler getMessageHandler()
Returns the currentSmtpMessageHandler.- Returns:
- The current message handler.
-
setBufferSize
public void setBufferSize(int size)
Defines the socket buffer size (in bytes). The buffer size impacts the amount of data the underlying Socket used for the communication can store. A buffer too small regarding the message size can lead to data loss and hence disregarding incoming messages.By default, this value is set to 1 Mo and cannot be change while the server is running.
- Parameters:
size- The socket buffer size (must greater than zero).
-
getBufferSize
public int getBufferSize()
Returns the socket buffer size (in bytes). By default, this value is set to 1 Mo.- Returns:
- The buffer size.
-
getReceivedMessages
public java.util.List<SmtpMessage> getReceivedMessages()
Returns all the received messages.- Returns:
- The received messages.
- See Also:
SmtpMessageStorage.getMessages()
-
clearReceivedMessages
public void clearReceivedMessages()
Clears all the received messages.- See Also:
SmtpMessageStorage.clear()
-
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.
-
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.IOExceptionStarts the server. If the server is already started, this method will raise andIllegalStateException.- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionCloses 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 interfacejava.lang.AutoCloseable- Throws:
java.io.IOException
-
-