Class SMTPClient

java.lang.Object
org.bluezoo.gumdrop.smtp.client.SMTPClient

public class SMTPClient extends Object
High-level SMTP client facade.

This class provides a simple, concrete API for connecting to SMTP servers. It internally creates a TCPTransportFactory, ClientEndpoint, and SMTPClientProtocolHandler, wiring them together and forwarding lifecycle events to the caller's ServerGreeting handler.

Plaintext with STARTTLS (submission)


 SMTPClient client = new SMTPClient(selectorLoop, "smtp.example.com", 587);
 client.setSSLContext(sslContext);
 client.connect(new ServerGreeting() {
     public void handleGreeting(ClientHelloState hello,
                                String message, boolean esmtp) {
         hello.ehlo("myhostname", ehloHandler);
     }
     // ...
 });
 

Implicit TLS (SMTPS)


 SMTPClient client = new SMTPClient("smtp.example.com", 465);
 client.setSecure(true);
 client.setSSLContext(sslContext);
 client.connect(greetingHandler);
 
Author:
Chris Burdess
See Also:
  • Constructor Details

    • SMTPClient

      public SMTPClient(String host, int port)
      Creates an SMTP client for the given hostname and port.

      Uses the next available worker loop from the global Gumdrop instance. DNS resolution is deferred until connect(org.bluezoo.gumdrop.smtp.client.handler.ServerGreeting) is called.

      Parameters:
      host - the remote hostname or IP address
      port - the remote port
    • SMTPClient

      public SMTPClient(SelectorLoop selectorLoop, String host, int port)
      Creates an SMTP client with an explicit selector loop.

      DNS resolution is deferred until connect(org.bluezoo.gumdrop.smtp.client.handler.ServerGreeting) is called.

      Parameters:
      selectorLoop - the selector loop, or null to use a Gumdrop worker
      host - the remote hostname or IP address
      port - the remote port
    • SMTPClient

      public SMTPClient(InetAddress host, int port)
      Creates an SMTP client for the given address and port.
      Parameters:
      host - the remote host address
      port - the remote port
    • SMTPClient

      public SMTPClient(SelectorLoop selectorLoop, InetAddress host, int port)
      Creates an SMTP client with an explicit selector loop and address.
      Parameters:
      selectorLoop - the selector loop, or null to use a Gumdrop worker
      host - the remote host address
      port - the remote port
  • Method Details

    • setSecure

      public void setSecure(boolean secure)
      Sets whether this client uses implicit TLS (SMTPS).

      When true, the connection starts with TLS immediately (port 465). When false, the connection starts plaintext and STARTTLS can be used to upgrade if an SSLContext is configured.

      Parameters:
      secure - true for implicit TLS
      See Also:
    • setSSLContext

      public void setSSLContext(SSLContext context)
      Sets the SSL context for TLS connections.

      Required for both implicit TLS (setSecure(true)) and explicit TLS via STARTTLS. When set without setSecure(true), an SSLEngine is created but not activated until the protocol handler calls endpoint.startTLS().

      Parameters:
      context - the SSL context
    • setTrustManager

      public void setTrustManager(X509TrustManager trustManager)
      Sets a custom trust manager for TLS certificate verification.
      Parameters:
      trustManager - the trust manager, or null to use defaults
      See Also:
    • setKeystoreFile

      public void setKeystoreFile(Path path)
      Sets the keystore file for client certificate authentication.
      Parameters:
      path - the keystore file path
    • setKeystoreFile

      public void setKeystoreFile(String path)
    • setKeystorePass

      public void setKeystorePass(String password)
      Sets the keystore password.
      Parameters:
      password - the keystore password
    • setKeystoreFormat

      public void setKeystoreFormat(String format)
      Sets the keystore format (e.g. JKS, PKCS12).
      Parameters:
      format - the keystore format
    • connect

      public void connect(ServerGreeting handler)
      Connects to the remote SMTP server.

      Creates the transport factory, endpoint handler, and client endpoint, then initiates the connection. Lifecycle events are forwarded to the given handler.

      Parameters:
      handler - the handler to receive the server greeting and lifecycle events
    • isOpen

      public boolean isOpen()
      Returns whether the connection is open.
      Returns:
      true if connected and open
    • close

      public void close()
      Closes the connection.