alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code


/**
 *  Message.java
 *
 *  Bill Lynch
 *  CoolServlets.com
 *  April 21 1999
 *
 *  Email Package Version 1.1
 *
 *    Copyright (C) 1999  Bill Lynch
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Library General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Library General Public License for more details.
 *
 *    You should have received a copy of the GNU Library General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.coolservlets.email;
        
/**
 *  Message class
 *
 *  This class is designed to build an email message. You should use this class
 *  to create a message object which is passed to a Transport object to send.
 *
 *  Here's what I would do to set up a simple Message object:
 *
 *  Message msg = new Message();
 *  msg.setFrom( new Address( "Bill", "bill@coolservlets.com" ) );
 *  msg.setRecipient( RecipientType.TO, new Address( "Dad", "BillsDad@coolservlets.com" ) );
 *  msg.setSubject( "Please send more $$" );
 *  msg.setText( "Hi Dad. Guess what -- bills are due." );
 *
 *  To add a CC, do this:
 *  msg.setRecipient( RecipientType.CC, new Address( "Mom", "BillsMom@coolservlets.com" ) );
 *
 *  Public Methods:
 *    Message()
 *    void addRecipient( int type, Address address)
 *    void addRecipients( int type, Address[] addresses)
 *    Address[] getRecipients( int type )
 *    Address[] getFrom( )
 *    String getSubject( )
 *    String getText( )
 *    void setFrom( Address address )
 *    void setRecipient( int type, Address address )
 *    void setRecipients( int type, Address[] addresses )
 *    void setSubject( String subject )
 *    void setText( String msgText )
 *
 *  Private Methods/Fields:
 *    Address[] addArray( int type, Address[] array1, Address[] array2 )
 *    Address   from;
 *    private Address[] recipientTO;
 *    private Address[] recipientCC;
 *    private Address[] recipientBCC;
 *    private String subject;
 *    private String msgText;
 *
 */
 
import java.util.*;

public class Message
{
  /**
   *  Initialize all arrays to size zero.
   */
  public Message()
  { recipientTO = new Address[0];
    recipientCC = new Address[0];
    recipientBCC = new Address[0];
  }

  /**
   *  Adds one email address to the existing list of addresses.
   */
  public void addRecipient( int type, Address address)
  {
    Address[] tempAddr = new Address[1];
    tempAddr[0] = address;
    addRecipients( type, tempAddr );
  }

  /**
   *  Adds multiple email addresses to the existing list of addresses.
   */
  public void addRecipients( int type, Address[] addresses)
  {
    //int newSize;
    if( type == RecipientType.TO )
      recipientTO  = addArray( RecipientType.TO, recipientTO, addresses );
    else if( type  == RecipientType.CC )
      recipientCC  = addArray( RecipientType.CC, recipientCC, addresses );
    else if( type  == RecipientType.BCC )
      recipientBCC = addArray( RecipientType.BCC, recipientBCC, addresses );
  }
  /**
   *  Combines two address arrays. (Only used by addRecipients(...) method.)
   */
  private Address[] addArray( int type, Address[] array1, Address[] array2 )
  {
    int i,j;

    Address[] tempAddr = new Address[array1.length + array2.length];
    for( i=0; i<array1.length; i++ )
      tempAddr[i] = array1[i];
    for( j=i; j<(array2.length+i); j++ )
      tempAddr[j] = array2[j-i];

    Address[] a = tempAddr;
    return a;
  }
  
  /**
   *  Returns the specified recipient array.
   */
  public Address[] getRecipients( int type )
  { if( type == RecipientType.TO )
      return this.recipientTO;
    else if( type == RecipientType.CC )
      return this.recipientCC;
    else //if( type == RecipientType.BCC )
      return this.recipientBCC;
  } 

  /**
   *  Returns the FROM address.
   *  This will always return an array of size 1.
   */
  public Address[] getFrom( )
  { Address[] tempAddr = new Address[1];
    tempAddr[0] = from;
    return tempAddr;
  } 

  /**
   *  Returns the message subject.
   */
  public String getSubject( )
  { return subject;
  }

  /**
   *  Returns the message text.
   */
  public String getText( )
  { return msgText;
  }

  /**
   *  Sets the FROM email address. (This will replace the current FROM address.)
   */
  public void setFrom( Address address )
  { from = address;
  }

  /**
   *  This method will set one recipient and will override anything
   *  that has been previously stored.
   */
  public void setRecipient( int type, Address address )
  { Address[] tempAddr = new Address[1];
    tempAddr[0] = address;
    setRecipients( type, tempAddr );
  }

  /**
   *  This method will sets multiple recipients and will override anything
   *  that has been previously stored.
   */
  public void setRecipients( int type, Address[] addresses )
  {
    if( type == RecipientType.TO )
      this.recipientTO = addresses;
    else if( type == RecipientType.CC )
      this.recipientCC = addresses;
    else if( type == RecipientType.BCC )
      this.recipientBCC = addresses;
  } 

  /**
   *  Sets the SUBJECT of the email.
   */
  public void setSubject( String subject )
  { this.subject = subject;
    if( this.subject == null )
      this.subject = " ";
  } 

  /**
   *  Sets the TEXT of the email.
   */
  public void setText( String msgText )
  { this.msgText = msgText;
  }

  // Data
  private Address   from;
  private Address[] recipientTO;
  private Address[] recipientCC;
  private Address[] recipientBCC;

  private String subject;
  private String msgText;

  //private Address[] replyTo; (not supported right now)
  //private Date sentDate; (not supported right now)
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.