SMTPClient: Email from Java Applications

SMTPClient is a lightweight SMTP (Simple Mail Transport Protocol) wrapper. Using this class (with the companion SMTPAddress class), Java developers can easily send email from any Java application (assuming, of course, there is visibility to an SMTP server).

SMTPClient supports basic email features, including:

  • Multiple recipient (to) addresses
  • Multiple carbon-copy (cc) addresses
  • Multiple blind-cc (bcc) addresses
  • Anonymous recipient lists
  • Configurable message priority and sensitivity

Because this is intended to be a lightweight SMTP wrapper, a few common email features are notably missing from this implementation, including:

  • Encrypted message support (via PGP or other plugin)
  • Attachments

The following code snippet illustrates how easy it is to send email with SMTPClient: (Note: Not all features of the class are demonstrated here.)

import com.precisonline.smtp.SMTPAddress;
import com.precisonline.smtp.SMTPClient;

public class SMTPTest
{
    public static void main (String args[]) 
    {
        /*
         * Setup connection to an SMTP server
         */
        
        SMTPClient client = new SMTPClient("mail.sample.com",25);
        
        /*
         * Identify yourself to the SMTP server.
         */
        
        client.setHelloName("YourName");
        client.setFromAddress("YourName@YourDomain.com","Your Name");
        
        /*
         * Add one of these for each person who should receive the message.
         * Change To (as in AddToAddress) to Cc or Bcc to affect the different
         * envelope addresses.
         */
        
        client.addToAddress("Info@PrecisOnline.com","Precision Solutions, Inc");
        
        /*
         * Set the subject of the message.
         */
        
        client.setSubject("More Information, Please");
        
        /*
         * And finally, set the text of the message to send.  Embed carriage
         * returns as appropriate.
         */
        
        client.setText("Please tell me how PSI can solve my technology problems!");
        
        /*
         * Send the message.  This will establish the connection to the 
         * server and do the required communication.  Up to this point, any
         * part of the message can be changed, including the server, port, etc.
         */
        
        client.send();
    }
}

(Note: This code is written for use with the JDK 1.3 socket API. It has not been tested with other JDK versions.)

To download the JAR file containing the source and class files for SMTPClient, plus the relevant Javadocs, click here. To view the Javadoc for the SMTPClient class, click here.

For a great, no-cost SMTP server for Windows, check out PostCast Server (www.PostCast.com, look for the "PostCast Server" free product). This server provides basic mail sending services and integrates quickly and easily with SMTPClient.