Class SMTP
- java.lang.Object
-
- org.apache.commons.net.SocketClient
-
- org.apache.commons.net.smtp.SMTP
-
- Direct Known Subclasses:
SMTPClient
public class SMTP extends SocketClient
SMTP provides the basic the functionality necessary to implement your own SMTP client. To derive the full benefits of the SMTP class requires some knowledge of the FTP protocol defined in RFC 821. However, there is no reason why you should have to use the SMTP class. TheSMTPClientclass, derived from SMTP, implements all the functionality required of an SMTP client. The SMTP class is made public to provide access to various SMTP constants and to make it easier for adventurous programmers (or those with special needs) to interact with the SMTP protocol and implement their own clients. A set of methods with names corresponding to the SMTP command names are provided to facilitate this interaction.You should keep in mind that the SMTP server may choose to prematurely close a connection for various reasons. The SMTP class will detect a premature SMTP server connection closing when it receives a
SMTPReply.SERVICE_NOT_AVAILABLEresponse to a command. When that occurs, the SMTP class method encountering that reply will throw anSMTPConnectionClosedException.SMTPConectionClosedExceptionis a subclass ofIOExceptionand therefore need not be caught separately, but if you are going to catch it separately, its catch block must appear before the more generalIOExceptioncatch block. When you encounter anSMTPConnectionClosedException, you must disconnect the connection withdisconnect()to properly clean up the system resources used by SMTP. Before disconnecting, you may check the last reply code and text withgetReplyCode,getReplyString, andgetReplyStrings.Rather than list it separately for each method, we mention here that every method communicating with the server and throwing an IOException can also throw a
MalformedServerReplyException, which is a subclass of IOException. A MalformedServerReplyException will be thrown when the reply received from the server deviates enough from the protocol specification that it cannot be interpreted in a useful manner despite attempts to be as lenient as possible.
-
-
Field Summary
Fields Modifier and Type Field Description protected ProtocolCommandSupport_commandSupport_A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.static intDEFAULT_PORTThe default SMTP port (25).protected java.lang.StringencodingThe encoding to use (user-settable).-
Fields inherited from class org.apache.commons.net.SocketClient
_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void_connectAction_()Initiates control connections and gets initial reply.intdata()A convenience method to send the SMTP DATA command to the server, receive the reply, and return the reply code.voiddisconnect()Closes the connection to the SMTP server and sets to null some internal data so that the memory may be reclaimed by the garbage collector.intexpn(java.lang.String name)A convenience method to send the SMTP VRFY command to the server, receive the reply, and return the reply code.protected ProtocolCommandSupportgetCommandSupport()Provide command support to super-classintgetReply()Fetches a reply from the SMTP server and returns the integer reply code.intgetReplyCode()Returns the integer value of the reply code of the last SMTP reply.java.lang.StringgetReplyString()Returns the entire text of the last SMTP server response exactly as it was received, including all end of line markers in NETASCII format.java.lang.String[]getReplyStrings()Returns the lines of text from the last SMTP server response as an array of strings, one entry per line.inthelo(java.lang.String hostname)A convenience method to send the SMTP HELO command to the server, receive the reply, and return the reply code.inthelp()A convenience method to send the SMTP HELP command to the server, receive the reply, and return the reply code.inthelp(java.lang.String command)A convenience method to send the SMTP HELP command to the server, receive the reply, and return the reply code.intmail(java.lang.String reversePath)A convenience method to send the SMTP MAIL command to the server, receive the reply, and return the reply code.intnoop()A convenience method to send the SMTP NOOP command to the server, receive the reply, and return the reply code.intquit()A convenience method to send the SMTP QUIT command to the server, receive the reply, and return the reply code.intrcpt(java.lang.String forwardPath)A convenience method to send the SMTP RCPT command to the server, receive the reply, and return the reply code.voidremoveProtocolCommandistener(ProtocolCommandListener listener)Removes a ProtocolCommandListener.intrset()A convenience method to send the SMTP RSET command to the server, receive the reply, and return the reply code.intsaml(java.lang.String reversePath)A convenience method to send the SMTP SAML command to the server, receive the reply, and return the reply code.intsend(java.lang.String reversePath)A convenience method to send the SMTP SEND command to the server, receive the reply, and return the reply code.intsendCommand(int command)Sends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code.intsendCommand(int command, java.lang.String args)Sends an SMTP command to the server, waits for a reply and returns the numerical response code.intsendCommand(java.lang.String command)Sends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code.intsendCommand(java.lang.String command, java.lang.String args)Sends an SMTP command to the server, waits for a reply and returns the numerical response code.intsoml(java.lang.String reversePath)A convenience method to send the SMTP SOML command to the server, receive the reply, and return the reply code.intturn()A convenience method to send the SMTP TURN command to the server, receive the reply, and return the reply code.intvrfy(java.lang.String user)A convenience method to send the SMTP VRFY command to the server, receive the reply, and return the reply code.-
Methods inherited from class org.apache.commons.net.SocketClient
addProtocolCommandListener, connect, connect, connect, connect, connect, connect, createCommandSupport, fireCommandSent, fireReplyReceived, getCharset, getCharsetName, getConnectTimeout, getDefaultPort, getDefaultTimeout, getKeepAlive, getLocalAddress, getLocalPort, getProxy, getReceiveBufferSize, getRemoteAddress, getRemotePort, getSendBufferSize, getServerSocketFactory, getSoLinger, getSoTimeout, getTcpNoDelay, isAvailable, isConnected, removeProtocolCommandListener, setCharset, setConnectTimeout, setDefaultPort, setDefaultTimeout, setKeepAlive, setProxy, setReceiveBufferSize, setSendBufferSize, setServerSocketFactory, setSocketFactory, setSoLinger, setSoTimeout, setTcpNoDelay, verifyRemote
-
-
-
-
Field Detail
-
DEFAULT_PORT
public static final int DEFAULT_PORT
The default SMTP port (25).- See Also:
- Constant Field Values
-
encoding
protected final java.lang.String encoding
The encoding to use (user-settable).- Since:
- 3.1 (changed from private to protected)
-
_commandSupport_
protected ProtocolCommandSupport _commandSupport_
A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.
-
-
Constructor Detail
-
SMTP
public SMTP()
The default SMTP constructor. Sets the default port toDEFAULT_PORTand initializes internal data structures for saving SMTP reply information.
-
SMTP
public SMTP(java.lang.String encoding)
Overloaded constructor where the user may specify a default encoding.- Parameters:
encoding- the encoing to use- Since:
- 2.0
-
-
Method Detail
-
_connectAction_
protected void _connectAction_() throws java.io.IOExceptionInitiates control connections and gets initial reply.- Overrides:
_connectAction_in classSocketClient- Throws:
java.io.IOException- (SocketException) if a problem occurs with the socket
-
disconnect
public void disconnect() throws java.io.IOExceptionCloses the connection to the SMTP server and sets to null some internal data so that the memory may be reclaimed by the garbage collector. The reply text and code information from the last command is voided so that the memory it used may be reclaimed.- Overrides:
disconnectin classSocketClient- Throws:
java.io.IOException- If an error occurs while disconnecting.
-
sendCommand
public int sendCommand(java.lang.String command, java.lang.String args) throws java.io.IOExceptionSends an SMTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyStringorgetReplyStrings.- Parameters:
command- The text representation of the SMTP command to send.args- The arguments to the SMTP command. If this parameter is set to null, then the command is sent with no argument.- Returns:
- The integer value of the SMTP reply code returned by the server in response to the command.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
sendCommand
public int sendCommand(int command, java.lang.String args) throws java.io.IOExceptionSends an SMTP command to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyStringorgetReplyStrings.- Parameters:
command- The SMTPCommand constant corresponding to the SMTP command to send.args- The arguments to the SMTP command. If this parameter is set to null, then the command is sent with no argument.- Returns:
- The integer value of the SMTP reply code returned by the server in response to the command.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
sendCommand
public int sendCommand(java.lang.String command) throws java.io.IOExceptionSends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyStringorgetReplyStrings.- Parameters:
command- The text representation of the SMTP command to send.- Returns:
- The integer value of the SMTP reply code returned by the server in response to the command.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
sendCommand
public int sendCommand(int command) throws java.io.IOExceptionSends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code. After invocation, for more detailed information, the actual reply text can be accessed by callinggetReplyStringorgetReplyStrings.- Parameters:
command- The SMTPCommand constant corresponding to the SMTP command to send.- Returns:
- The integer value of the SMTP reply code returned by the server in response to the command.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
getReplyCode
public int getReplyCode()
Returns the integer value of the reply code of the last SMTP reply. You will usually only use this method after you connect to the SMTP server to check that the connection was successful sinceconnectis of type void.- Returns:
- The integer value of the reply code of the last SMTP reply.
-
getReply
public int getReply() throws java.io.IOExceptionFetches a reply from the SMTP server and returns the integer reply code. After calling this method, the actual reply text can be accessed from either callinggetReplyStringorgetReplyStrings. Only use this method if you are implementing your own SMTP client or if you need to fetch a secondary response from the SMTP server.- Returns:
- The integer value of the reply code of the fetched SMTP reply.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while receiving the server reply.
-
getReplyStrings
public java.lang.String[] getReplyStrings()
Returns the lines of text from the last SMTP server response as an array of strings, one entry per line. The end of line markers of each are stripped from each line.- Returns:
- The lines of text from the last SMTP response as an array.
-
getReplyString
public java.lang.String getReplyString()
Returns the entire text of the last SMTP server response exactly as it was received, including all end of line markers in NETASCII format.- Returns:
- The entire text from the last SMTP response as a String.
-
helo
public int helo(java.lang.String hostname) throws java.io.IOExceptionA convenience method to send the SMTP HELO command to the server, receive the reply, and return the reply code.- Parameters:
hostname- The hostname of the sender.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
mail
public int mail(java.lang.String reversePath) throws java.io.IOExceptionA convenience method to send the SMTP MAIL command to the server, receive the reply, and return the reply code.- Parameters:
reversePath- The reverese path.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
rcpt
public int rcpt(java.lang.String forwardPath) throws java.io.IOExceptionA convenience method to send the SMTP RCPT command to the server, receive the reply, and return the reply code.- Parameters:
forwardPath- The forward path.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
data
public int data() throws java.io.IOExceptionA convenience method to send the SMTP DATA command to the server, receive the reply, and return the reply code.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
send
public int send(java.lang.String reversePath) throws java.io.IOExceptionA convenience method to send the SMTP SEND command to the server, receive the reply, and return the reply code.- Parameters:
reversePath- The reverese path.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
soml
public int soml(java.lang.String reversePath) throws java.io.IOExceptionA convenience method to send the SMTP SOML command to the server, receive the reply, and return the reply code.- Parameters:
reversePath- The reverese path.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
saml
public int saml(java.lang.String reversePath) throws java.io.IOExceptionA convenience method to send the SMTP SAML command to the server, receive the reply, and return the reply code.- Parameters:
reversePath- The reverese path.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
rset
public int rset() throws java.io.IOExceptionA convenience method to send the SMTP RSET command to the server, receive the reply, and return the reply code.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
vrfy
public int vrfy(java.lang.String user) throws java.io.IOExceptionA convenience method to send the SMTP VRFY command to the server, receive the reply, and return the reply code.- Parameters:
user- The user address to verify.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
expn
public int expn(java.lang.String name) throws java.io.IOExceptionA convenience method to send the SMTP VRFY command to the server, receive the reply, and return the reply code.- Parameters:
name- The name to expand.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
help
public int help() throws java.io.IOExceptionA convenience method to send the SMTP HELP command to the server, receive the reply, and return the reply code.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
help
public int help(java.lang.String command) throws java.io.IOExceptionA convenience method to send the SMTP HELP command to the server, receive the reply, and return the reply code.- Parameters:
command- The command name on which to request help.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
noop
public int noop() throws java.io.IOExceptionA convenience method to send the SMTP NOOP command to the server, receive the reply, and return the reply code.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
turn
public int turn() throws java.io.IOExceptionA convenience method to send the SMTP TURN command to the server, receive the reply, and return the reply code.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
quit
public int quit() throws java.io.IOExceptionA convenience method to send the SMTP QUIT command to the server, receive the reply, and return the reply code.- Returns:
- The reply code received from the server.
- Throws:
SMTPConnectionClosedException- If the SMTP server prematurely closes the connection as a result of the client being idle or some other reason causing the server to send SMTP reply code 421. This exception may be caught either as an IOException or independently as itself.java.io.IOException- If an I/O error occurs while either sending the command or receiving the server reply.
-
removeProtocolCommandistener
public void removeProtocolCommandistener(ProtocolCommandListener listener)
Removes a ProtocolCommandListener. Delegates this incorrectly named method - removeProtocolCommandistener (note the missing "L")- to the correct methodSocketClient.removeProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener)- Parameters:
listener- The ProtocolCommandListener to remove
-
getCommandSupport
protected ProtocolCommandSupport getCommandSupport()
Provide command support to super-class- Overrides:
getCommandSupportin classSocketClient- Returns:
- the CommandSupport instance, may be
null
-
-