Class SMTP

java.lang.Object
javaforce.SMTP

public class SMTP extends Object
SMTP client class.
  • Field Details

  • Constructor Details

    • SMTP

      public SMTP()
  • Method Details

    • connect

      public boolean connect(String host, int port) throws Exception
      Connects to an insecure SMTP server.
      Parameters:
      host - = SMTP server
      port - = port to connect (default = 25 or 587)
      Throws:
      Exception
    • connectSSL

      public boolean connectSSL(String host, int port) throws Exception
      Connects to a secure SMTP server.
      Parameters:
      host - = SMTP server
      port - = port to connect (default = 465)
      Throws:
      Exception
    • disconnect

      public void disconnect() throws Exception
      Disconnect from SMTP Server.
      Throws:
      Exception
    • setDebug

      public static void setDebug(boolean state)
      Set debug logging state.
    • setLog

      public static void setLog(int id)
    • login

      public boolean login() throws Exception
      Sends HELO / EHLO command.
      Throws:
      Exception
    • auth

      public boolean auth(String user, String pass, String type) throws Exception
      Authenticates with SMTP Server.
      Parameters:
      user - = username
      pass - = password
      type - = auth type (AUTH_LOGIN or AUTH_NTML)
      Throws:
      Exception
    • auth

      public boolean auth(String user, String pass) throws Exception
      Authenticates with SMTP Server using AUTH_LOGIN type.
      Parameters:
      user - = username
      pass - = password
      Throws:
      Exception
    • logout

      public void logout() throws Exception
      Send QUIT command.
      Throws:
      Exception
    • cmd

      public void cmd(String cmd) throws Exception
      Send any command to server.
      Throws:
      Exception
    • starttls

      public void starttls() throws Exception
      Secures connection using STARTTLS command.
      Throws:
      Exception
    • from

      public void from(String email) throws Exception
      Sets from email for message.
      Parameters:
      email - = from email address
      Throws:
      Exception
    • to

      public void to(String email) throws Exception
      Sets to email for message.
      Parameters:
      email - = to email address
      Throws:
      Exception
    • cc

      public void cc(String email) throws Exception
      Sets cc email for message.
      Parameters:
      email - = cc email address
      Throws:
      Exception
    • bcc

      public void bcc(String email) throws Exception
      Sets bcc email for message.
      Parameters:
      email - = bcc email address
      Throws:
      Exception
    • data

      public boolean data(String msg) throws Exception
      Sends a simple text message with headers included. The terminating \r\n.\r\n will be added.
      Parameters:
      msg - = email headers + blank line + message body
      Throws:
      Exception
    • subject

      public void subject(String subject)
      Sets subject header for email.
      Parameters:
      subject - = subject of email Can only be used with enhanced data() method.
    • setContentType

      public void setContentType(String type)
      Sets content type header of body.
      Parameters:
      type - = "text/plain" or "text/html" (default is unspecified) Can only be used with enhanced data() method.
    • addDate

      public void addDate(Calendar date)
      Add date header to email. Can only be used with enhanced data() method.
    • addHeader

      public void addHeader(String name, String value)
      Add custom header to email. Can only be used with enhanced data() method.
    • data

      public boolean data(String body_text, String body_html, SMTP.Attachment[] attachments) throws Exception
      Sends an enhanced message in text and/or html formats with headers and optional attachments. Headers are automatically generated and MUST NOT be included in body. One or both of body_text, body_html MUST be supplied.
      Parameters:
      body_text - = body of message in TEXT format
      body_html - = body of message in HTML format
      attachments - = files to attach
      Throws:
      Exception
    • getLastResponse

      public String getLastResponse()
      Returns last line of response from last command sent.
    • main

      public static void main(String[] args)
      CLI