com.jscape.inet.smtp

## Class Smtp

- java.lang.Object
- - com.jscape.inet.smtp.Smtp

- All Implemented Interfaces:
    java.io.Serializable, java.lang.AutoCloseable
  Direct Known Subclasses:
    SmtpSsh, SmtpSsl

      public class Smtp
      extends java.lang.Object
      implements java.lang.AutoCloseable, 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

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `Smtp.Authentication`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static int` | `AUTH_CRAM_MD5` CRAM_MD5 authentication.
    `static int` | `AUTH_LOGIN` Standard username/password authentication.
    `static int` | `AUTH_XOAUTH2`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `Smtp()` Constructs a new Smtp instance.
    `Smtp(java.lang.String hostname)` Constructs a new Smtp instance.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods Deprecated Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `addSmtpListener(SmtpListener listener)` Adds Smtp event listener.
    `void` | `clearProxySettings()` Clears proxy server values.
    `void` | `close()`
    `Smtp` | `connect()` Connects to SMTP server.
    `void` | `disconnect()` Disconnects from SMTP server.
    `Smtp.Authentication` | `getAuthentication()`
    `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 authenticationCode)` Login to SMTP server using provided username, password and authentication mode.
    `void` | `login(java.lang.String username, java.lang.String password, Smtp.Authentication authentication)`
    `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.
    `Smtp` | `setAuthentication(Smtp.Authentication value)`
    `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

    - #### AUTH_XOAUTH2

          public static final int AUTH_XOAUTH2

      See Also:
        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.

    - #### getAuthentication

          public Smtp.Authentication getAuthentication()

    - #### setAuthentication

          public Smtp setAuthentication(Smtp.Authentication value)

    - #### 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 Smtp 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`

    - #### close

          public void close()
                     throws java.lang.Exception

      Specified by:
        `close` in interface `java.lang.AutoCloseable`
      Throws:
        `java.lang.Exception`

    - #### 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 authenticationCode)
                     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
        `authenticationCode` - 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,
                            Smtp.Authentication authentication)
                     throws SmtpException

      Throws:
        `SmtpException`

    - #### 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`

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