Secure iNet Factory

com.jscape.inet.smtp
Class Smtp

java.lang.Object
  extended by com.jscape.inet.smtp.Smtp
All Implemented Interfaces:
java.io.Serializable
Direct Known Subclasses:
SmtpSsh, SmtpSsl

public class Smtp
extends java.lang.Object
implements java.io.Serializable

Implements the basic functionality of a SMTP client.

Example Usage:

 // create new instance
 Smtp smtp = new Smtp("smtp.myserver.com");
  // address the message
 EmailMessage message = new EmailMessage();
 message.setTo("mjones@myserver.com");
 message.setFrom("jsmith@myserver.com");
 message.setSubject("Meeting today at 8");
 message setBody("see you then");
  // connect, send the message, disconnect
 try
 {
   smtp.connect();
   smtp.send(message);
   smtp.disconnect();
 }
 catch(Exception e)
 {
   System.out.println(e);
 }
 

See Also:
Serialized Form

Field Summary
static int AUTH_CRAM_MD5
          CRAM_MD5 authentication.
static int AUTH_LOGIN
          Standard username/password authentication.
 
Constructor Summary
Smtp()
          Constructs a new Smtp instance.
Smtp(java.lang.String hostname)
          Constructs a new Smtp instance.
 
Method Summary
 void addSmtpListener(SmtpListener listener)
          Adds Smtp event listener.
 void clearProxySettings()
          Clears proxy server values.
 void connect()
          Connects to SMTP server.
 void disconnect()
          Disconnects from SMTP server.
 int getConnectTimeout()
          Gets the timeout used when opening a connection to SMTP server.
 java.io.PrintStream getDebugStream()
          Gets PrintStream used in reporting debug statements.
 java.lang.String getHostname()
          Gets the hostname or IP address of SMTP server.
 int getPort()
          Gets port of SMTP server.
 int getReadTimeout()
          Gets the timeout used when reading responses from SMTP server.
 int getTimeout()
          Deprecated. Replaced by getConnectTimeout and getReadTimeout
 boolean isConnected()
          Checks if client is connected to SMTP server.
 boolean isDebug()
          Gets debug level of SMTP session.
 java.lang.String issueCommand(java.lang.String command)
          Sends command to SMTP server Command must be a valid SMTP command.
 void login(java.lang.String username, java.lang.String password)
          Login to SMTP server using standard username / password authentication.
 void login(java.lang.String username, java.lang.String password, int authentication)
          Login to SMTP server using provided username, password and authentication mode.
static void main(java.lang.String[] args)
           
 void removeSmtpListener(SmtpListener listener)
          Removes specified Smtp event listener.
 void send(EmailMessage message)
          Sends email message.
 void send(EmailMessage message, java.lang.String from, java.lang.String to)
          Sends email message.
 void send(java.lang.String from, java.lang.String to, java.lang.String subject, java.lang.String body)
          Sends basic email message.
 void setConnectTimeout(int timeout)
          Sets the timeout for opening the connection.
 void setDebug(boolean debug)
          Sets debugging state.
 void setDebugStream(java.io.PrintStream debugStream)
          Sets PrintStream used in reporting debug statements.
 void setHeloAddress(java.lang.String heloAddress)
          Sets the source address to use when issuing an EHLO or HELO command to SMTP server.
 void setHostname(java.lang.String hostname)
          Sets the hostname or IP address of SMTP server.
 void setPort(int port)
          Sets port of SMTP server.
 void setProxyAuthentication(java.lang.String proxyUsername, java.lang.String proxyPassword)
          Sets the username and password to use when for authentication with proxy server.
 void setProxyHost(java.lang.String proxyHostname, int proxyPort)
          Sets the proxy hostname and port for this connection.
 void setProxyType(java.lang.String proxyType)
          Sets the proxy type will be used for this connection.
 void setReadTimeout(int timeout)
          Sets the timeout for reading responses from SMTP server.
 void setTimeout(int timeout)
          Deprecated. Replaced by setConnectTimeout and setReadTimeout
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

AUTH_LOGIN

public static final int AUTH_LOGIN
Standard username/password authentication.

See Also:
login(java.lang.String, java.lang.String, int), Constant Field Values

AUTH_CRAM_MD5

public static final int AUTH_CRAM_MD5
CRAM_MD5 authentication.

See Also:
login(java.lang.String, java.lang.String, int), Constant Field Values
Constructor Detail

Smtp

public Smtp()
Constructs a new Smtp instance.


Smtp

public Smtp(java.lang.String hostname)
Constructs a new Smtp instance.

Parameters:
hostname - the hostname of SMTP server
Method Detail

setConnectTimeout

public void setConnectTimeout(int timeout)
Sets the timeout for opening the connection. Default is 30 seconds.

Parameters:
timeout - the timeout in milliseconds

getConnectTimeout

public int getConnectTimeout()
Gets the timeout used when opening a connection to SMTP server. Default is 30 seconds.

Returns:
timeout server connection.

isConnected

public boolean isConnected()
Checks if client is connected to SMTP server.

Returns:
connection status

setDebug

public void setDebug(boolean debug)
Sets debugging state. All debug statements are reported to debug stream. Default debug stream is System.out

Parameters:
debug - true to turn debugging on
See Also:
setDebugStream(java.io.PrintStream)

isDebug

public boolean isDebug()
Gets debug level of SMTP session. Debug messages are reported to debug stream. Default debug stream is System.out

Returns:
true if debugging is on
See Also:
setDebugStream(java.io.PrintStream)

setDebugStream

public void setDebugStream(java.io.PrintStream debugStream)
Sets PrintStream used in reporting debug statements. Default PrintStream is System.out

Parameters:
debugStream - the PrintStream to send debug statements to
See Also:
PrintStream

getDebugStream

public java.io.PrintStream getDebugStream()
Gets PrintStream used in reporting debug statements. Default PrintStream is System.out

Returns:
the PrintStream used in debugging
See Also:
PrintStream

setHeloAddress

public void setHeloAddress(java.lang.String heloAddress)
Sets the source address to use when issuing an EHLO or HELO command to SMTP server.

Parameters:
heloAddress - the IP address or hostname to use

setHostname

public void setHostname(java.lang.String hostname)
Sets the hostname or IP address of SMTP server.

Parameters:
hostname - the hostname of SMTP server

getHostname

public java.lang.String getHostname()
Gets the hostname or IP address of SMTP server.

Returns:
hostname

setPort

public void setPort(int port)
Sets port of SMTP server. Default port value is 25.

Parameters:
port - the port of SMTP server

getPort

public int getPort()
Gets port of SMTP server. Default port value is 25.

Returns:
port

setReadTimeout

public void setReadTimeout(int timeout)
Sets the timeout for reading responses from SMTP server. Default is 30 seconds.

Parameters:
timeout - the timeout in milliseconds

getReadTimeout

public int getReadTimeout()
Gets the timeout used when reading responses from SMTP server. Default is 30 seconds.

Returns:
reading from server timeout.

setTimeout

public void setTimeout(int timeout)
Deprecated. Replaced by setConnectTimeout and setReadTimeout

Sets the timeout for opening connection to SMTP server. Default is 30 seconds.

Parameters:
timeout - the timeout in milliseconds

getTimeout

public int getTimeout()
Deprecated. Replaced by getConnectTimeout and getReadTimeout

Gets the timeout for opening connection to SMTP server. Default is 30 seconds.

Returns:
timeout in milliseconds

setProxyAuthentication

public void setProxyAuthentication(java.lang.String proxyUsername,
                                   java.lang.String proxyPassword)
Sets the username and password to use when for authentication with proxy server. To clear these settings invoke the #clearProxySettings method.

Parameters:
proxyUsername - the proxy username
proxyPassword - the proxy password
See Also:
clearProxySettings()

setProxyHost

public void setProxyHost(java.lang.String proxyHostname,
                         int proxyPort)
Sets the proxy hostname and port for this connection. To clear these settings invoke the #clearProxySettings method.

Parameters:
proxyHostname - the hostname or ip address of the proxy server
proxyPort - the port of the proxy server
See Also:
clearProxySettings()

setProxyType

public void setProxyType(java.lang.String proxyType)
Sets the proxy type will be used for this connection.

Parameters:
proxyType - The proxy type. Valid values: HTTP, SOCKS5

clearProxySettings

public void clearProxySettings()
Clears proxy server values.


addSmtpListener

public void addSmtpListener(SmtpListener listener)
Adds Smtp event listener.

Parameters:
listener - the SmtpListener to be added
See Also:
SmtpListener

connect

public void connect()
             throws SmtpException
Connects to SMTP server.

Throws:
SmtpException - thrown for I/O or SMTP related error

disconnect

public void disconnect()
                throws SmtpException
Disconnects from SMTP server.

Throws:
SmtpException - thrown to handle SMTP and I/O related errors
See Also:
SmtpException

issueCommand

public java.lang.String issueCommand(java.lang.String command)
                              throws SmtpException
Sends command to SMTP server Command must be a valid SMTP command.

Parameters:
command - an SMTP command
Returns:
the SMTP response
Throws:
SmtpException - if an I/O or SMTP related error occurs

login

public void login(java.lang.String username,
                  java.lang.String password,
                  int authentication)
           throws SmtpException
Login to SMTP server using provided username, password and authentication mode. Valid authentication modes are Smtp.AUTH_LOGIN and Smtp.AUTH_CRAM_MD5. Default authentication mode is Smtp.AUTH_LOGIN

Parameters:
username - the SMTP username
password - the SMTP password
authentication - the authentication mode to use
Throws:
SmtpException - thrown for I/O or SMTP related error
See Also:
AUTH_LOGIN, AUTH_CRAM_MD5

login

public void login(java.lang.String username,
                  java.lang.String password)
           throws SmtpException
Login to SMTP server using standard username / password authentication.

Parameters:
username - the SMTP username
password - the SMTP password
Throws:
SmtpException - thrown for I/O or SMTP related error
See Also:
AUTH_LOGIN

removeSmtpListener

public void removeSmtpListener(SmtpListener listener)
Removes specified Smtp event listener.

Parameters:
listener - the SmtpListener to be removed
See Also:
SmtpListener

send

public void send(java.lang.String from,
                 java.lang.String to,
                 java.lang.String subject,
                 java.lang.String body)
          throws SmtpException,
                 MimeException
Sends basic email message. For more control over the content of the email message see the overloaded version of send method.

Parameters:
from - the from email address
to - the to email address
subject - the subject of the email message
body - the body of the email message
Throws:
SmtpException - if an I/O or SMTP related error occurs
MimeException - if a MIME related error occurs

send

public void send(EmailMessage message,
                 java.lang.String from,
                 java.lang.String to)
          throws SmtpException
Sends email message.

Parameters:
message - an EmailMessage
from - the email address to send this message from when communicating with SMTP server, overriding the From header in the message
to - the email address to send this message to when communicating with SMTP server, overriding the To header in the message
Throws:
SmtpException - if an I/O or SMTP related error occurs
See Also:
EmailMessage

send

public void send(EmailMessage message)
          throws SmtpException
Sends email message.

Parameters:
message - an EmailMessage
Throws:
SmtpException - if an I/O or SMTP related error occurs
See Also:
EmailMessage

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Throws:
java.lang.Exception

Secure iNet Factory

Copyright © JSCAPE LLC. 1999-2011. All Rights Reserved