Class SMTPClient

java.lang.Object
org.subethamail.smtp.client.SMTPClient
Direct Known Subclasses:
SmartClient

public class SMTPClient extends Object
A very low level abstraction of the STMP stream which knows how to handle the raw protocol for lines, whitespace, etc.
Author:
Jeff Schnitzer
  • Constructor Details

    • SMTPClient

      public SMTPClient()
      Creates an unconnected client.
    • SMTPClient

      public SMTPClient(String host, int port) throws UnknownHostException, IOException
      Establishes a connection to host and port.
      Throws:
      UnknownHostException - if the hostname cannot be resolved
      IOException - if there is a problem connecting to the port
    • SMTPClient

      public SMTPClient(String host, int port, SocketAddress bindpoint) throws UnknownHostException, IOException
      Establishes a connection to host and port from the specified local socket address.
      Parameters:
      bindpoint - the local socket address. If null, the system will pick up an ephemeral port and a valid local address.
      Throws:
      UnknownHostException - if the hostname cannot be resolved
      IOException - if there is a problem connecting to the port
  • Method Details

    • connect

      public void connect(String host, int port) throws IOException
      Establishes a connection to host and port.
      Throws:
      IOException - if there is a problem connecting to the port
    • createSocket

      protected Socket createSocket()
      Returns a new unconnected socket.

      Implementation notice for subclasses: This function is called by the constructors which open the connection immediately. In these cases the subclass is not yet initialized, therefore subclasses overriding this function shouldn't use those constructors.

    • isConnected

      public boolean isConnected()
      Returns true if the client is connected to the server.
    • send

      protected void send(String msg) throws IOException
      Sends a message to the server, ie "HELO foo.example.com". A newline will be appended to the message.
      Parameters:
      msg - should not have any newlines
      Throws:
      IOException
    • receive

      protected SMTPClient.Response receive() throws IOException
      Note that the response text comes back without trailing newlines.
      Throws:
      IOException
    • sendReceive

      public SMTPClient.Response sendReceive(String msg) throws IOException
      Sends a message to the server, ie "HELO foo.example.com". A newline will be appended to the message.
      Parameters:
      msg - should not have any newlines
      Returns:
      the response from the server
      Throws:
      IOException
    • receiveAndCheck

      public SMTPClient.Response receiveAndCheck() throws IOException, SMTPException
      If response is not success, throw an exception
      Throws:
      IOException
      SMTPException
    • sendAndCheck

      public SMTPClient.Response sendAndCheck(String msg) throws IOException, SMTPException
      If response is not success, throw an exception
      Throws:
      IOException
      SMTPException
    • close

      public void close()
      Logs but otherwise ignores errors
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setBindpoint

      public void setBindpoint(SocketAddress bindpoint)
      Sets the local socket address. If null, the system will pick up an ephemeral port and a valid local address. Default is null.
    • getBindpoint

      public SocketAddress getBindpoint()
      Returns the local socket address.
    • setHostPort

      public void setHostPort(String hostPort)
      Sets the name of the remote MTA for informative purposes. Default is host:port, where host and port are the values which were used to open the TCP connection to the server, as they were passed to the connect method.
    • getHostPort

      public String getHostPort()
      Returns:
      a nice pretty description of who we are connected to