Recipe 19.3 Mail-Enabling a Server Program


You want to send mail notification from within a program.


Use the javax.mail API directly, or use this Mailer wrapper.


It is not uncommon to want to send email from deep within a non-GUI program such as a server. Here, I package all the standard code into a class called Mailer, which has a series of "set" methods to set the sender, recipient, mail server, etc. You simply call the Mailer method doSend( ) after setting the recipient, sender, subject, and the message text, and Mailer does the rest. Very convenient! So convenient, in fact, that Mailer is part of the com.darwinsys.util package.

For extra generality, the lists of To, CC, and BCC recipients can be set in one of three ways:

  • By passing a string containing one or more recipients, such as "ian, robin"

  • By passing an ArrayList containing all the recipients as strings

  • By adding each recipient as a string

A "full" version allows the user to type the recipients, the subject, the text, and so on into a GUI and have some control over the header fields. The MailComposeBean (in Recipe 19.9) does all of these, using a Swing-based GUI. MailComposeBean uses this Mailer class to interface with the JavaMail API. Example 19-4 contains the code for the Mailer class.

Example 19-4.
package com.darwinsys.mail; import*; import java.util.*; import javax.mail.*; import javax.mail.internet.*;  /** Mailer. No relation to Norman. Sends an email message.  */ public class Mailer {     /** The javamail session object. */     protected Session session;     /** The sender's email address */     protected String from;     /** The subject of the message. */     protected String subject;     /** The recipient ("To:"), as Strings. */     protected ArrayList toList = new ArrayList( );     /** The CC list, as Strings. */     protected ArrayList ccList = new ArrayList( );     /** The BCC list, as Strings. */     protected ArrayList bccList = new ArrayList( );     /** The text of the message. */     protected String body;     /** The SMTP relay host */     protected String mailHost;     /** The verbosity setting */     protected boolean verbose;     /** Get from */     public String getFrom( ) {         return from;     }     /** Set from */     public void setFrom(String fm) {         from = fm;     }     /** Get subject */     public String getSubject( ) {         return subject;     }     /** Set subject */     public void setSubject(String subj) {         subject = subj;     }     // LOTS OF OBVIOUS SETTERS/GETTERS NOT SHOWN HERE     // They are in the online source version.     /** Check if all required fields have been set before sending.      * Normally called e.g., by a JSP before calling doSend.      * Is also called by doSend for verification.      */     public boolean isComplete( ) {         if (from == null    || from.length( )==0) {             System.err.println("doSend: no FROM");             return false;         }         if (subject == null || subject.length( )==0) {             System.err.println("doSend: no SUBJECT");             return false;         }         if (toList.size( )==0) {             System.err.println("doSend: no recipients");             return false;         }         if (body == null || body.length( )==0) {             System.err.println("doSend: no body");             return false;         }         if (mailHost == null || mailHost.length( )==0) {             System.err.println("doSend: no server host");             return false;         }         return true;     }     public void setServer(String s) {         mailHost = s;     }     /** Send the message.      */     public synchronized void doSend( ) throws MessagingException {         if (!isComplete( ))             throw new IllegalArgumentException(                 "doSend called before message was complete");         /** Properties object used to pass props into the MAIL API */         Properties props = new Properties( );         props.put("", mailHost);         // Create the Session object         if (session == null) {             session = Session.getDefaultInstance(props, null);             if (verbose)                 session.setDebug(true);        // Verbose!         }                  // create a message         final Message mesg = new MimeMessage(session);         InternetAddress[] addresses;         // TO Address list         addresses = new InternetAddress[toList.size( )];         for (int i=0; i<addresses.length; i++)             addresses[i] = new InternetAddress((String)toList.get(i));         mesg.setRecipients(Message.RecipientType.TO, addresses);         // From Address         mesg.setFrom(new InternetAddress(from));         // CC Address list         addresses = new InternetAddress[ccList.size( )];         for (int i=0; i<addresses.length; i++)             addresses[i] = new InternetAddress((String)ccList.get(i));         mesg.setRecipients(Message.RecipientType.CC, addresses);         // BCC Address list         addresses = new InternetAddress[bccList.size( )];         for (int i=0; i<addresses.length; i++)             addresses[i] = new InternetAddress((String)bccList.get(i));         mesg.setRecipients(Message.RecipientType.BCC, addresses);         // The Subject         mesg.setSubject(subject);         // Now the message body.         mesg.setText(body);         // Finally, send the message! (use static Transport method)         // Do this in a Thread as it sometimes is too slow for JServ         new Thread( ) {             public void run( ) {                 try {                     Transport.send(mesg);                 } catch (MessagingException e) {                     throw new IllegalArgumentException(                     "Transport.send( ) threw: " + e.toString( ));                 }             }         }.start( );     }     /** Convenience method that does it all with one call. */     public static void send(String mailhost,          String recipient, String sender, String subject, String message)      throws MessagingException {         Mailer m = new Mailer( );         m.setServer(mailhost);         m.addTo(recipient);         m.setFrom(sender);         m.setSubject(subject);         m.setBody(message);         m.doSend( );     }     /** Convert a list of addresses to an ArrayList. This will work      * for simple names like "tom,, 123.45@c$.com"      * but will fail on certain complex (but RFC-valid) names like      * "(Darwin, Ian) <>".      * Or even "Ian Darwin <>".      */     protected ArrayList tokenize(String s) {         ArrayList al = new ArrayList( );         StringTokenizer tf = new StringTokenizer(s, ",");         // For each word found in the line         while (tf.hasMoreTokens( )) {             // trim blanks, and add to list.             al.add(tf.nextToken( ).trim( ));         }         return al;     } }

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: