Immediately after connecting is the only real time you need to check the reply code (because connect is of type void). The convention for all the SMTP command methods in SMTPClient is such that they either return a boolean value or some other value. The boolean methods return true on a successful completion reply from the SMTP server and false on a reply resulting in an error condition or failure. The methods returning a value other than boolean return a value containing the higher level data produced by the SMTP command, or null if a reply resulted in an error condition or failure. If you want to access the exact SMTP reply code causing a success or failure, you must call {@link org.apache.commons.net.smtp.SMTP#getReplyCode getReplyCode } aftera success or failure.
You should keep in mind that the SMTP server may choose to prematurely close a connection for various reasons. The SMTPClient class will detect a premature SMTP server connection closing when it receives a {@link org.apache.commons.net.smtp.SMTPReply#SERVICE_NOT_AVAILABLE SMTPReply.SERVICE_NOT_AVAILABLE }response to a command. When that occurs, the method encountering that reply will throw an {@link org.apache.commons.net.smtp.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 {@link org.apache.commons.net.smtp.SMTPConnectionClosedException}, you must disconnect the connection with {@link #disconnect disconnect() } to properly clean up thesystem resources used by SMTPClient. Before disconnecting, you may check the last reply code and text with {@link org.apache.commons.net.smtp.SMTP#getReplyCode getReplyCode }, {@link org.apache.commons.net.smtp.SMTP#getReplyString getReplyString }, and {@link org.apache.commons.net.smtp.SMTP#getReplyStrings 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 {@link org.apache.commons.net.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.
@author Daniel F. Savarese @see SMTP @see SimpleSMTPHeader @see RelayPath @see SMTPConnectionClosedException @see org.apache.commons.net.MalformedServerReplyException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|