Mailer: Sends an email message : Email « Network Protocol « Java






Mailer: Sends an email message

 
/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*; 

/** Mailer. No relation to Norman. Sends an email message.
 * My old Sender class, recast as a Bean for use in JSP's & elsewhere.
 * Example usage:
 * <pre>
 * Mailer mb = new Mailer();
 * mb.setFrom("orders@YourDomain.com");
 * mb.addTo("orders@YourDomain.com");
 * mb.setSubject("LHBOOKS ORDER!!");
 * mb.setBody(order.toString());
 * mb.setServer(application.getInitParameter("mail.server.smtp")); 
 * try {
 *     mb.doSend();
 * } catch (MessagingException ex) {
 *     ...
 * }
 * </pre>
 * @author Ian F. Darwin
 * @version $Id: Mailer.java,v 1.16 2004/01/31 01:26:05 ian Exp $
 */
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;
  }

  // SETTERS/GETTERS FOR TO: LIST 

  /** Get tolist, as an array of Strings */
  public ArrayList getToList() {
    return toList;
  }

  /** Set to list to an ArrayList of Strings */
  public void setToList(ArrayList to) {
    toList = to;
  }

  /** Set to as a string like "tom, mary, robin@host". Loses any
   * previously-set values. */
  public void setToList(String s) {
    toList = tokenize(s);
  }

  /** Add one "to" recipient */
  public void addTo(String to) {
    toList.add(to);
  }

  // SETTERS/GETTERS FOR CC: LIST 

  /** Get cclist, as an array of Strings */
  public ArrayList getCcList() {
    return ccList;
  }

  /** Set cc list to an ArrayList of Strings */
  public void setCcList(ArrayList cc) {
    ccList = cc;
  }

  /** Set cc as a string like "tom, mary, robin@host". Loses any
   * previously-set values. */
  public void setCcList(String s) {
    ccList = tokenize(s);
  }

  /** Add one "cc" recipient */
  public void addCc(String cc) {
    ccList.add(cc);
  }

  // SETTERS/GETTERS FOR BCC: LIST 

  /** Get bcclist, as an array of Strings */
  public ArrayList getBccList() {
    return bccList;
  }

  /** Set bcc list to an ArrayList of Strings */
  public void setBccList(ArrayList bcc) {
    bccList = bcc;
  }

  /** Set bcc as a string like "tom, mary, robin@host". Loses any
   * previously-set values. */
  public void setBccList(String s) {
    bccList = tokenize(s);
  }

  /** Add one "bcc" recipient */
  public void addBcc(String bcc) {
    bccList.add(bcc);
  }

  // SETTER/GETTER FOR MESSAGE BODY

  /** Get message */
  public String getBody() {
    return body;
  }

  /** Set message */
  public void setBody(String text) {
    body = text;
  }

  // SETTER/GETTER FOR VERBOSITY

  /** Get verbose */
  public boolean isVerbose() {
    return verbose;
  }

  /** Set verbose */
  public void setVerbose(boolean v) {
    verbose = v;
  }

  /** 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("mail.smtp.host", 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.
   * @param mailhost - SMTP server host
   * @param recipient - domain address of email (user@host.domain)
   * @param sender - your email address
   * @param subject - the subject line
   * @param message - the entire message body as a String with embedded \n's
   */
  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, mary@foo.com, 123.45@c$.com"
   * but will fail on certain complex (but RFC-valid) names like
   * "(Darwin, Ian) <http://www.darwinsys.com/>".
   * Or even "Ian Darwin <http://www.darwinsys.com/>".
   */
  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;
  }
}

           
         
  








Related examples in the same category

1.Pure Java Email client
2.Sending Mail Using Sockets
3.Sending Mail
4.Get Email Message Example
5.A Client to Send SMTP MailA Client to Send SMTP Mail
6.TestOpenMailRelay -- send self-returning SPAM to check for relay sitesTestOpenMailRelay -- send self-returning SPAM to check for relay sites
7.Sender -- send an email message
8.Sender -- send an email message with attachment
9.SendMime -- send a multi-part MIME email message
10.Read a file return mail headers one at a time
11.Send mail using GMAIL
12.Send Mail Implementation using simple SMTP
13.Validator for Zip code, Email, Phone number
14.Send email out