10.16. Sending Mail with SMTP

Problem

You need to send an email.

Solution

Use SMTPClient from Commons Net. The following example sends a simple email to [email protected] from [email protected] via the SMTP server on host www.discursive.com using port 25:

import org.apache.commons.net.smtp.SMTPClient;

SMTPClient client = new SMTPClient( );
client.connect("www.discursive.com");
client.sendSimpleMessage("[email protected]", 
                         "[email protected]", 
                         "Hey! Call me when you get a chance." );
client.disconnect( );

This example sends a very simple email message to one recipient by passing three arguments to client.sendSimpleMessage() : the sender’s email address, the recipient’s email address, and a message. If you are trying to send a simple email message to multiple recipients, pass a String[] containing each address as the second parameter to sendSimpleMessage( ). The following example sends a simple email message to multiple recipients by passing a String[] to sendSimpleMessage( ):

import org.apache.commons.net.smtp.SMTPClient;

SMTPClient client = new SMTPClient( );
client.connect("www.discursive.com");

String[] recipients = new String[2];
recipients[0] = "[email protected]";
recipients[1] = "[email protected]";

client.sendSimpleMessage("[email protected]", 
                         recipients, 
                         "The eagle has landed." );
client.disconnect( );

Discussion

Telnet to port 25 of a SMTP server, and you will see the server respond with a numeric code (220). SMTPReply.isPositiveCompletion( ) returns true if the response code of the previously executed command is between 200 and 299; the value of the initial response code, 220, is equal to the public static variable SMTPReply.SERVICE_READY. The following example uses getReplyCode( ) and SMTPReply.isPositiveCompletion() to test the connection to the SMTP server:

import org.apache.commons.net.smtp.SMTP;
import org.apache.commons.net.smtp.SMTPClient;
import org.apache.commons.net.smtp.SMTPReply;

SMTPClient client = new SMTPClient( );
client.connect("www.discursive.com");

int response = client.getReplyCode( );
if( SMTPReply.isPositiveCompletion( response ) ) {

    // Set the sender and the recipients
    client.setSender( "[email protected]" );
    client.addRecipient( "[email protected]" );
    client.addRecipient( "[email protected]" );

    // Supply the message via a Writer
    Writer message = client.sendMessageData( );
    message.write( "Spend more money on energy research.  Thanks." );
    message.close( );

    // Send the message and print a confirmation
    boolean success = client.completePendingCommand( );
    if( success ) {
        System.out.println( "Message sent" );                
    }
} else {
    System.out.println( "Error communicating with SMTP server" );
}
client.disconnect( );

Instead of sendSimpleMessage( ), the previous example sets a sender address and two recipient addresses using setSender( ) and addRecipient(). The message body is then written to a Writer returned by sendMessageData(). When the Writer is closed, the message is sent by calling completePendingCommand(). completePendingCommand( ) returns true if the message has been queued for delivery.

See Also

JavaMail is the set of utilities contained in the javax.mail package, and JavaMail is usually used in conjunction with the Java Activation Framework (JAF) to send email messages using SMTP. Commons Net does not aim to replace the JavaMail API, but it does provide a very straightforward alternative for sending email messages. For more information about JavaMail, see the JavaMail product page at http://java.sun.com/products/javamail. For more information about the JavaMail API, see Chapter 10, “JavaMail Best Practices,” in Java Enterprise Best Practices (O’Reilly).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.73.175