Recipe 19.2 Sending Email: For Real


You need to send email, and the browser trick in Recipe 19.1 won't cut it.


Provide a real email client.


A real email client allows the user considerably more control. Of course, it also requires more work. In this recipe, I'll build a simple version of a mail sender, relying upon the JavaMail standard extension in package javax.mail and javax.mail.internet (the latter contains classes that are specific to Internet email protocols). This first example shows the steps of sending mail over SMTP, the standard Internet mail protocol. The steps are listed in the sidebar.

Ian's Basic Steps: Sending Email over SMTP

Here are the steps for sending email over SMTP:

  1. Create a java.util.Properties object (see Recipe 7.3) to pass information about the mail server, as the JavaMail API allows room for many settings.

  2. Load the Properties with at least the hostname of the SMTP mail server.

  3. Create a Session object.

  4. Create a Message from the Session object.

  5. Set the From, To, CC addresses, and Subject in the Message.

  6. Set the message text into the message body.

  7. Finally, use the static method Transport.send( ) to send the message!

As usual in Java, you must catch certain exceptions. This API requires that you catch the MessagingException , which indicates some failure of the transmission. Class Sender is shown in Example 19-3.

Example 19-3.
import*; import java.util.*; import javax.mail.*; import javax.mail.internet.*;  /** sender -- send an email message.  */ public class Sender {     /** The message recipient. */     protected String message_recip = "";     /* What's it all about, Alfie? */     protected String message_subject = "Re: your mail";     /** The message CC recipient. */     protected String message_cc = "";     /** The message body */     protected String message_body =         "I am unable to attend to your message, as I am busy sunning " +         "myself on the beach in Maui, where it is warm and peaceful. " +         "Perhaps when I return I'll get around to reading your mail. " +         "Or perhaps not.";     /** The JavaMail session object */     protected Session session;     /** The JavaMail message object */     protected Message mesg;     /** Do the work: send the mail to the SMTP server.  */     public void doSend( ) {         // We need to pass info to the mail server as a Properties, since         // JavaMail (wisely) allows room for LOTS of properties...         Properties props = new Properties( );         // Your LAN must define the local SMTP server as "mailhost"         // for this simple-minded version to be able to send mail...         props.put("", "mailhost");         // Create the Session object         session = Session.getDefaultInstance(props, null);         session.setDebug(true);        // Verbose!                  try {             // create a message             mesg = new MimeMessage(session);             // From Address - this should come from a Properties...             mesg.setFrom(new InternetAddress("nobody@host.domain"));             // TO Address              InternetAddress toAddress = new InternetAddress(message_recip);             mesg.addRecipient(Message.RecipientType.TO, toAddress);             // CC Address             InternetAddress ccAddress = new InternetAddress(message_cc);             mesg.addRecipient(Message.RecipientType.CC, ccAddress);             // The Subject             mesg.setSubject(message_subject);             // Now the message body.             mesg.setText(message_body);             // TODO I18N: use setText(msgText.getText( ), charset)                          // Finally, send the message!             Transport.send(mesg);         } catch (MessagingException ex) {             while ((ex = (MessagingException)ex.getNextException( )) != null) {                 ex.printStackTrace( );             }         }     }     /** Simple test case driver */     public static void main(String[] av) {         Sender sm = new Sender( );         sm.doSend( );     } }

Of course, a program that can only send one message to one address is not useful in the long run. The second version (not shown here, but in the source tree accompanying this book) allows the To, From, Mailhost, and Subject to come from the command line and reads the mail text either from a file or from the standard input.

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: