public class SMTP extends SocketClient
SMTPClient
class, 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_AVAILABLE
response to
a command. When that occurs, the SMTP class method encountering that reply will throw an SMTPConnectionClosedException
.
SMTPConectionClosedException
is a subclass of IOException
and therefore need not be caught separately, but if you are going to
catch it separately, its catch block must appear before the more general IOException
catch block. When you encounter an
SMTPConnectionClosedException
, you must disconnect the connection with
disconnect()
to properly clean up the system resources used by SMTP. Before disconnecting, you may
check the last reply code and text with getReplyCode
, getReplyString
, and getReplyStrings
.
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.
Modifier and Type | Field and Description |
---|---|
protected ProtocolCommandSupport |
_commandSupport_
A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.
|
static int |
DEFAULT_PORT
The default SMTP port (25).
|
protected String |
encoding
The encoding to use (user-settable).
|
_defaultPort_, _hostname_, _input_, _output_, _serverSocketFactory_, _socket_, _socketFactory_, _timeout_, connectTimeout, NETASCII_EOL
Constructor and Description |
---|
SMTP()
The default SMTP constructor.
|
SMTP(String encoding)
Overloaded constructor where the user may specify a default encoding.
|
Modifier and Type | Method and Description |
---|---|
protected void |
_connectAction_()
Initiates control connections and gets initial reply.
|
int |
data()
A convenience method to send the SMTP DATA command to the server, receive the reply, and return the reply code.
|
void |
disconnect()
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.
|
int |
expn(String name)
A convenience method to send the SMTP VRFY command to the server, receive the reply, and return the reply code.
|
protected ProtocolCommandSupport |
getCommandSupport()
Provide command support to super-class
|
int |
getReply()
Fetches a reply from the SMTP server and returns the integer reply code.
|
int |
getReplyCode()
Returns the integer value of the reply code of the last SMTP reply.
|
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.
|
String[] |
getReplyStrings()
Returns the lines of text from the last SMTP server response as an array of strings, one entry per line.
|
int |
helo(String hostname)
A convenience method to send the SMTP HELO command to the server, receive the reply, and return the reply code.
|
int |
help()
A convenience method to send the SMTP HELP command to the server, receive the reply, and return the reply code.
|
int |
help(String command)
A convenience method to send the SMTP HELP command to the server, receive the reply, and return the reply code.
|
int |
mail(String reversePath)
A convenience method to send the SMTP MAIL command to the server, receive the reply, and return the reply code.
|
int |
noop()
A convenience method to send the SMTP NOOP command to the server, receive the reply, and return the reply code.
|
int |
quit()
A convenience method to send the SMTP QUIT command to the server, receive the reply, and return the reply code.
|
int |
rcpt(String forwardPath)
A convenience method to send the SMTP RCPT command to the server, receive the reply, and return the reply code.
|
void |
removeProtocolCommandistener(ProtocolCommandListener listener)
Removes a ProtocolCommandListener.
|
int |
rset()
A convenience method to send the SMTP RSET command to the server, receive the reply, and return the reply code.
|
int |
saml(String reversePath)
A convenience method to send the SMTP SAML command to the server, receive the reply, and return the reply code.
|
int |
send(String reversePath)
A convenience method to send the SMTP SEND command to the server, receive the reply, and return the reply code.
|
int |
sendCommand(int command)
Sends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code.
|
int |
sendCommand(int command,
String args)
Sends an SMTP command to the server, waits for a reply and returns the numerical response code.
|
int |
sendCommand(String command)
Sends an SMTP command with no arguments to the server, waits for a reply and returns the numerical response code.
|
int |
sendCommand(String command,
String args)
Sends an SMTP command to the server, waits for a reply and returns the numerical response code.
|
int |
soml(String reversePath)
A convenience method to send the SMTP SOML command to the server, receive the reply, and return the reply code.
|
int |
turn()
A convenience method to send the SMTP TURN command to the server, receive the reply, and return the reply code.
|
int |
vrfy(String user)
A convenience method to send the SMTP VRFY command to the server, receive the reply, and return the reply code.
|
addProtocolCommandListener, applySocketAttributes, 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
public static final int DEFAULT_PORT
protected final String encoding
protected ProtocolCommandSupport _commandSupport_
public SMTP()
DEFAULT_PORT
and initializes internal data structures for saving SMTP reply
information.protected void _connectAction_() throws IOException
_connectAction_
in class SocketClient
IOException
- (SocketException) if a problem occurs with the socketpublic int data() throws IOException
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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public void disconnect() throws IOException
disconnect
in class SocketClient
IOException
- If an error occurs while disconnecting.public int expn(String name) throws IOException
name
- The name to expand.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.protected ProtocolCommandSupport getCommandSupport()
getCommandSupport
in class SocketClient
null
public int getReply() throws IOException
getReplyString
or getReplyStrings
. 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.
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.IOException
- If an I/O error occurs while receiving the server reply.public int getReplyCode()
connect
is of type void.
public String getReplyString()
public String[] getReplyStrings()
public int helo(String hostname) throws IOException
hostname
- The hostname of the sender.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int help() throws IOException
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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int help(String command) throws IOException
command
- The command name on which to request help.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int mail(String reversePath) throws IOException
reversePath
- The reverese path.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int noop() throws IOException
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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int quit() throws IOException
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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int rcpt(String forwardPath) throws IOException
forwardPath
- The forward path.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public void removeProtocolCommandistener(ProtocolCommandListener listener)
SocketClient.removeProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener)
listener
- The ProtocolCommandListener to removepublic int rset() throws IOException
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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int saml(String reversePath) throws IOException
reversePath
- The reverese path.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int send(String reversePath) throws IOException
reversePath
- The reverese path.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int sendCommand(int command) throws IOException
getReplyString
or getReplyStrings
.
command
- The SMTPCommand constant corresponding to the SMTP command to send.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int sendCommand(int command, String args) throws IOException
getReplyString
or getReplyStrings
.
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.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int sendCommand(String command) throws IOException
getReplyString
or getReplyStrings
.
command
- The text representation of the SMTP command to send.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int sendCommand(String command, String args) throws IOException
getReplyString
or getReplyStrings
.
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.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int soml(String reversePath) throws IOException
reversePath
- The reverese path.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int turn() throws IOException
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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.public int vrfy(String user) throws IOException
user
- The user address to verify.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.IOException
- If an I/O error occurs while either sending the command or receiving the server reply.Copyright © 2001–2022 The Apache Software Foundation. All rights reserved.