Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.apache.commons.net.smtp;
 
 
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. The 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 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.

 
 
 public class SMTP extends SocketClient
 {
    
The default SMTP port (25). *
 
     public static final int DEFAULT_PORT = 25;
 
     // We have to ensure that the protocol communication is in ASCII
     // but we use ISO-8859-1 just in case 8-bit characters cross
     // the wire.
     private static final String __DEFAULT_ENCODING = "ISO-8859-1";

    
The encoding to use (user-settable).

Since:
3.1 (changed from private to protected)
 
    protected final String encoding;

    
A ProtocolCommandSupport object used to manage the registering of ProtocolCommandListeners and te firing of ProtocolCommandEvents.
    private int _replyCode;
    private final ArrayList<String_replyLines;
    private boolean _newReplyString;
    private String _replyString;

    
The default SMTP constructor. Sets the default port to DEFAULT_PORT and initializes internal data structures for saving SMTP reply information.
    public SMTP()
    {
        this();
    }

    
Overloaded constructor where the user may specify a default encoding.

Parameters:
encoding
Since:
2.0
    public SMTP(String encoding) {
         = new ArrayList<String>();
         = false;
         = null;
         = new ProtocolCommandSupport(this);
        this. = encoding;
    }

    
Send a command to the server. May also be used to send text data.

Parameters:
command the command to send (as a plain String)
args the command arguments, may be null
includeSpace if true, add a space between the command and its arguments
Returns:
the reply code
Throws:
java.io.IOException
    private int __sendCommand(String commandString argsboolean includeSpace)
    throws IOException
    {
        StringBuilder __commandBuffer = new StringBuilder();
        __commandBuffer.append(command);
        if (args != null)
        {
            if (includeSpace) {
                __commandBuffer.append(' ');
            }
            __commandBuffer.append(args);
        }
        __commandBuffer.append(.);
        String message;
        .write(message = __commandBuffer.toString());
        .flush();
        fireCommandSent(commandmessage);
        __getReply();
        return ;
    }

    

Parameters:
command the command to send (as an int defined in SMPTCommand)
args the command arguments, may be null
includeSpace if true, add a space between the command and its arguments
Returns:
the reply code
Throws:
java.io.IOException
    private int __sendCommand(int commandString argsboolean includeSpace)
    throws IOException
    {
        return __sendCommand(SMTPCommand.getCommand(command), argsincludeSpace);
    }
    private void __getReply() throws IOException
    {
        int length;
         = true;
        .clear();
        String line = .readLine();
        if (line == null) {
            throw new SMTPConnectionClosedException(
                "Connection closed without indication.");
        }
        // In case we run into an anomaly we don't want fatal index exceptions
        // to be thrown.
        length = line.length();
        if (length < 3) {
            throw new MalformedServerReplyException(
                "Truncated server reply: " + line);
        }
        try
        {
            String code = line.substring(0, 3);
             = Integer.parseInt(code);
        }
        catch (NumberFormatException e)
        {
            throw new MalformedServerReplyException(
                "Could not parse response code.\nServer Reply: " + line);
        }
        .add(line);
        // Get extra lines if message continues.
        if (length > 3 && line.charAt(3) == '-')
        {
            do
            {
                line = .readLine();
                if (line == null) {
                    throw new SMTPConnectionClosedException(
                        "Connection closed without indication.");
                }
                .add(line);
                // The length() check handles problems that could arise from readLine()
                // returning too soon after encountering a naked CR or some other
                // anomaly.
            }
            while (!(line.length() >= 4 && line.charAt(3) != '-' &&
                     Character.isDigit(line.charAt(0))));
            // This is too strong a condition because a non-conforming server
            // could screw things up like ftp.funet.fi does for FTP
            // line.startsWith(code)));
        }
        if ( == .) {
            throw new SMTPConnectionClosedException(
                "SMTP response 421 received.  Server closed connection.");
        }
    }

    
Initiates control connections and gets initial reply. *
    @Override
    protected void _connectAction_() throws IOException
    {
        super._connectAction_();
         =
            new CRLFLineReader(new InputStreamReader(,
                                                    ));
         =
            new BufferedWriter(new OutputStreamWriter(,
                                                      ));
        __getReply();
    }


    
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. The reply text and code information from the last command is voided so that the memory it used may be reclaimed.

Throws:
java.io.IOException If an error occurs while disconnecting.
    @Override
    public void disconnect() throws IOException
    {
        super.disconnect();
         = null;
         = null;
         = null;
        .clear();
         = false;
    }


    
Sends 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 calling getReplyString or getReplyStrings .

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.
    public int sendCommand(String commandString argsthrows IOException
    {
        return __sendCommand(commandargstrue);
    }


    
Sends 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 calling getReplyString or getReplyStrings .

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.
    public int sendCommand(int commandString argsthrows IOException
    {
        return sendCommand(SMTPCommand.getCommand(command), args);
    }


    
Sends 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 calling getReplyString or getReplyStrings .

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.
    public int sendCommand(String commandthrows IOException
    {
        return sendCommand(commandnull);
    }


    
Sends 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 calling getReplyString or getReplyStrings .

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.
    public int sendCommand(int commandthrows IOException
    {
        return sendCommand(commandnull);
    }


    
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 since connect is of type void.

Returns:
The integer value of the reply code of the last SMTP reply.
    public int getReplyCode()
    {
        return ;
    }

    
Fetches 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 calling 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.

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.
    public int getReply() throws IOException
    {
        __getReply();
        return ;
    }


    
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.
    public String[] getReplyStrings()
    {
        return .toArray(new String[.size()]);
    }

    
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.
    public String getReplyString()
    {
        StringBuilder buffer;
        if (!) {
            return ;
        }
        buffer = new StringBuilder();
        for (String line : )
        {
            buffer.append(line);
            buffer.append(.);
        }
         = false;
        return ( = buffer.toString());
    }


    
A 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.
    public int helo(String hostnamethrows IOException
    {
        return sendCommand(.hostname);
    }


    
A 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.
    public int mail(String reversePaththrows IOException
    {
        return __sendCommand(.reversePathfalse);
    }


    
A 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.
    public int rcpt(String forwardPaththrows IOException
    {
        return __sendCommand(.forwardPathfalse);
    }


    
A 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.
    public int data() throws IOException
    {
        return sendCommand(.);
    }


    
A 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.
    public int send(String reversePaththrows IOException
    {
        return sendCommand(.reversePath);
    }


    
A 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.
    public int soml(String reversePaththrows IOException
    {
        return sendCommand(.reversePath);
    }


    
A 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.
    public int saml(String reversePaththrows IOException
    {
        return sendCommand(.reversePath);
    }


    
A 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.
    public int rset() throws IOException
    {
        return sendCommand(.);
    }


    
A 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.
    public int vrfy(String userthrows IOException
    {
        return sendCommand(.user);
    }


    
A 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.
    public int expn(String namethrows IOException
    {
        return sendCommand(.name);
    }

    
A 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.
    public int help() throws IOException
    {
        return sendCommand(.);
    }

    
A 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.
    public int help(String commandthrows IOException
    {
        return sendCommand(.command);
    }

    
A 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.
    public int noop() throws IOException
    {
        return sendCommand(.);
    }


    
A 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.
    public int turn() throws IOException
    {
        return sendCommand(.);
    }


    
A 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.
    public int quit() throws IOException
    {
        return sendCommand(.);
    }

    
Removes a ProtocolCommandListener. Delegates this incorrectly named method - removeProtocolCommandistener (note the missing "L")- to the correct method org.apache.commons.net.SocketClient.removeProtocolCommandListener(org.apache.commons.net.ProtocolCommandListener)

Parameters:
listener The ProtocolCommandListener to remove
        removeProtocolCommandListener(listener);
    }

    
Provide command support to super-class
    @Override
        return ;
    }
/* Emacs configuration
 * Local variables:        **
 * mode:             java  **
 * c-basic-offset:   4     **
 * indent-tabs-mode: nil   **
 * End:                    **
 */
New to GrepCode? Check out our FAQ X