org.subethamail.smtp.client
Class SMTPClient

java.lang.Object
  extended by 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

Nested Class Summary
static class SMTPClient.Response
          Result of an SMTP exchange.
 
Constructor Summary
SMTPClient()
          Creates an unconnected client.
SMTPClient(String host, int port)
          Establishes a connection to host and port.
SMTPClient(String host, int port, SocketAddress bindpoint)
          Establishes a connection to host and port from the specified local socket address.
 
Method Summary
 void close()
          Logs but otherwise ignores errors
 void connect(String host, int port)
          Establishes a connection to host and port.
protected  Socket createSocket()
          Returns a new unconnected socket.
 SocketAddress getBindpoint()
          Returns the local socket address.
 String getHostPort()
           
protected  SMTPClient.Response receive()
          Note that the response text comes back without trailing newlines.
 void receiveAndCheck()
          If response is not success, throw an exception
protected  void send(String msg)
          Sends a message to the server, ie "HELO foo.example.com".
 void sendAndCheck(String msg)
          If response is not success, throw an exception
 SMTPClient.Response sendReceive(String msg)
          Sends a message to the server, ie "HELO foo.example.com".
 void setBindpoint(SocketAddress bindpoint)
          Sets the local socket address.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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 Detail

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.


getHostPort

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

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 void receiveAndCheck()
                     throws IOException,
                            SMTPException
If response is not success, throw an exception

Throws:
IOException
SMTPException

sendAndCheck

public void 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.



Copyright © 2006-2012. All Rights Reserved.