You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by qu...@apache.org on 2003/03/07 17:08:18 UTC

cvs commit: jakarta-commons-sandbox/email/src/java/org/apache/commons/mail Email.java

quintonm    2003/03/07 08:08:17

  Modified:    email/src/java/org/apache/commons/mail Email.java
  Log:
  - removed unused imports
  - added missing javadocs
  - The debug property is now set during session creation instead of after
  - setFrom, setTo, setCc, and setBcc are now overloaded to accept just
    an email address.
  - setSubject no longer declares MessagingException to be thrown
  
  Revision  Changes    Path
  1.6       +127 -51   jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/Email.java
  
  Index: Email.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/email/src/java/org/apache/commons/mail/Email.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Email.java	19 Jan 2003 20:06:17 -0000	1.5
  +++ Email.java	7 Mar 2003 16:08:17 -0000	1.6
  @@ -60,8 +60,6 @@
   import java.util.Enumeration;
   import java.util.Collection;
   
  -import javax.mail.Address;
  -import javax.mail.Authenticator;
   import javax.mail.Message;
   import javax.mail.MessagingException;
   import javax.mail.Session;
  @@ -76,6 +74,7 @@
    * sent date.  Subclasses are responsible for setting the message
    * body.
    *
  + * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
    * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a>
    * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a>
  @@ -103,6 +102,7 @@
       public static final String TEXT_PLAIN = "text/plain";
       public static final String ATTACHMENTS = "attachments";
       public static final String FILE_SERVER = "file.server";
  +    public static final String MAIL_DEBUG = "mail.debug";
   
       public static final String KOI8_R = "koi8-r";
       public static final String ISO_8859_1 = "iso-8859-1";
  @@ -128,20 +128,26 @@
   
       /** The content type  */
       private String contentType = null;
  -    
  -    /** Set session debugging on or off*/
  +
  +    /** Set session debugging on or off */
       private boolean debug = false;
   
      /**
  -    * The hostname of the mail server with which to connect. If null will try to get
  -    * property from system.properties. If still null, quit
  +    * The hostname of the mail server with which to connect. If null will try
  +    * to get property from system.properties. If still null, quit
       */
       private String hostName = null;
   
  -    /** Lists of related email adresses */
  +    /** List of "to" email adresses */
       private ArrayList toList = null;
  +
  +    /** List of "cc" email adresses */
       private ArrayList ccList = null;
  +
  +    /** List of "bcc" email adresses */
       private ArrayList bccList = null;
  +
  +    /** List of "replyTo" email adresses */
       private ArrayList replyList = null;
   
       /**
  @@ -192,19 +198,19 @@
       public void setContent(Object aObject, String aContentType)
       {
           this.content = aObject;
  -        if (aContentType == null) 
  +        if (aContentType == null)
           {
               this.contentType = aContentType;
           }
  -        else 
  -        {        
  +        else
  +        {
               int charsetPos = aContentType.toLowerCase().indexOf("; charset=");
  -            if (charsetPos > 0) 
  +            if (charsetPos > 0)
               {
                   aContentType.substring(0, charsetPos);
                   charset = aContentType.substring(charsetPos + 10);
               }
  -            else 
  +            else
               {
                   this.contentType = aContentType;
               }
  @@ -212,7 +218,7 @@
       }
   
       /**
  -     * Set the hostname
  +     * Set the hostname of the outgoing mail server
        *
        * @param   aHostName
        */
  @@ -226,7 +232,7 @@
        *
        * @return A Session.
        */
  -    private Session getMailSession() 
  +    private Session getMailSession()
           throws MessagingException
       {
           Properties properties = System.getProperties();
  @@ -236,19 +242,18 @@
           {
               hostName = properties.getProperty(MAIL_HOST);
           }
  -        
  +
           if (hostName == null)
           {
               throw new MessagingException(
                   "Cannot find valid hostname to mail session");
           }
  -        
  +
           properties.setProperty(MAIL_HOST, hostName);
  -        
  +        properties.setProperty(MAIL_DEBUG, Boolean.toString(this.debug));
  +
           Session session = Session.getDefaultInstance(properties, null);
  -        
  -        session.setDebug(this.debug);
  -        
  +
           return session;
       }
   
  @@ -256,11 +261,24 @@
        * Set the FROM field of the email.
        *
        * @param email A String.
  +     * @return An Email.
  +     * @exception MessagingException Indicates an invalid email address
  +     */
  +    public Email setFrom(String email)
  +        throws MessagingException
  +    {
  +        return setFrom(email, null);
  +    }
  +
  +    /**
  +     * Set the FROM field of the email.
  +     *
  +     * @param email A String.
        * @param name A String.
        * @return An Email.
  -     * @exception MessagingException.
  +     * @exception MessagingException Indicates an invalid email address
        */
  -    public Email setFrom(String email, String name) 
  +    public Email setFrom(String email, String name)
           throws MessagingException
       {
           try
  @@ -290,11 +308,24 @@
        * Add a recipient TO to the email.
        *
        * @param email A String.
  +     * @return An Email.
  +     * @exception MessagingException Indicates an invalid email address
  +     */
  +    public Email addTo(String email)
  +        throws MessagingException
  +    {
  +        return addTo(email, null);
  +    }
  +
  +    /**
  +     * Add a recipient TO to the email.
  +     *
  +     * @param email A String.
        * @param name A String.
        * @return An Email.
  -     * @exception MessagingException.
  +     * @exception MessagingException Indicates an invalid email address
        */
  -    public Email addTo(String email, String name) 
  +    public Email addTo(String email, String name)
           throws MessagingException
       {
           try
  @@ -321,8 +352,8 @@
       /**
        * Set a list of "TO" addresses
        *
  -     * @param   aCollection
  -     * @return
  +     * @param   aCollection collection of InternetAddress objects
  +     * @return An Email.
        */
       public Email setTo(Collection aCollection)
       {
  @@ -334,11 +365,24 @@
        * Add a recipient CC to the email.
        *
        * @param email A String.
  +     * @return An Email.
  +     * @exception MessagingException Indicates an invalid email address
  +     */
  +    public Email addCc(String email)
  +        throws MessagingException
  +    {
  +        return addCc(email, null);
  +    }
  +
  +    /**
  +     * Add a recipient CC to the email.
  +     *
  +     * @param email A String.
        * @param name A String.
        * @return An Email.
  -     * @exception MessagingException.
  +     * @exception MessagingException Indicates an invalid email address
        */
  -    public Email addCc(String email, String name) 
  +    public Email addCc(String email, String name)
           throws MessagingException
       {
           try
  @@ -366,7 +410,7 @@
       /**
        * Set a list of "CC" addresses
        *
  -     * @param   aCollection
  +     * @param   aCollection collection of InternetAddress objects
        * @return An Email.
        */
       public Email setCc(Collection aCollection)
  @@ -379,11 +423,24 @@
        * Add a blind BCC recipient to the email.
        *
        * @param email A String.
  +     * @return An Email.
  +     * @exception MessagingException Indicates an invalid email address
  +     */
  +    public Email addBcc(String email)
  +        throws MessagingException
  +    {
  +        return addBcc(email, null);
  +    }
  +
  +    /**
  +     * Add a blind BCC recipient to the email.
  +     *
  +     * @param email A String.
        * @param name A String.
        * @return An Email.
  -     * @exception MessagingException.
  +     * @exception MessagingException Indicates an invalid email address
        */
  -    public Email addBcc(String email, String name) 
  +    public Email addBcc(String email, String name)
           throws MessagingException
       {
           try
  @@ -411,8 +468,8 @@
       /**
        * Set a list of "BCC" addresses
        *
  -     * @param   aCollection
  -     * @return
  +     * @param   aCollection collection of InternetAddress objects
  +     * @return  An Email.
        */
       public Email setBcc(Collection aCollection)
       {
  @@ -424,12 +481,24 @@
        * Add a reply to address to the email.
        *
        * @param email A String.
  +     * @return An Email.
  +     * @exception MessagingException Indicates an invalid email address
  +     */
  +    public Email addReplyTo(String email) throws MessagingException
  +    {
  +        return addReplyTo(email, null);
  +    }
  +
  +    /**
  +     * Add a reply to address to the email.
  +     *
  +     * @param email A String.
        * @param name A String.
        * @return An Email.
  -     * @exception MessagingException.
  +     * @exception MessagingException Indicates an invalid email address
        */
  -    public Email addReplyTo(String email,
  -            String name) throws MessagingException
  +    public Email addReplyTo(String email, String name)
  +            throws MessagingException
       {
           try
           {
  @@ -474,6 +543,14 @@
        */
       public void addHeader(String name, String value)
       {
  +        if( name == null )
  +        {
  +            throw new IllegalArgumentException("name can not be null");
  +        }
  +        if( value == null )
  +        {
  +            throw new IllegalArgumentException("value can not be null");
  +        }
           if (headers == null)
           {
               headers = new Hashtable();
  @@ -484,11 +561,10 @@
       /**
        * Set the email subject.
        *
  -     * @param subject A String.
  +     * @param aSubject A String.
        * @return An Email.
  -     * @exception MessagingException.
        */
  -    public Email setSubject(String aSubject) throws MessagingException
  +    public Email setSubject(String aSubject)
       {
           subject = aSubject;
           return this;
  @@ -500,27 +576,27 @@
        *
        * @param msg A String.
        * @return An Email.
  -     * @exception MessagingException.
  +     * @exception MessagingException generic exception
        */
       public abstract Email setMsg(String msg) throws MessagingException;
   
       /**
        * Does the work of actually sending the email.
        *
  -     * @exception MessagingException, if there was an error.
  +     * @exception MessagingException if there was an error.
        */
  -    public void send() 
  +    public void send()
           throws MessagingException
       {
           MimeMessage message = new MimeMessage(getMailSession());
   
           if (subject != null)
           {
  -            if (charset == null) 
  +            if (charset == null)
               {
                   message.setSubject(subject);
               }
  -            else 
  +            else
               {
                   message.setSubject(subject, charset);
               }
  @@ -529,10 +605,10 @@
           if (content != null)
           {
               String type = contentType;
  -            if (type != null && charset != null) 
  +            if (type != null && charset != null)
               {
                   type += "; charset=" + charset;
  -            } 
  +            }
               message.setContent(content, type);
           }
           else if (emailBody != null)
  @@ -588,11 +664,11 @@
               }
           }
   
  -        if (message.getSentDate() == null) 
  +        if (message.getSentDate() == null)
           {
               message.setSentDate(new Date());
           }
  -        
  +
           Transport.send(message);
       }
   
  @@ -600,14 +676,14 @@
        * Utility to copy ArrayList of known InternetAddress objects into an
        * array.
        *
  -     * @param v A ArrayList.
  +     * @param aList A ArrayList.
        * @return An InternetAddress[].
        */
       private InternetAddress[] toInternetAddressArray(ArrayList aList)
       {
           InternetAddress[] ia =
               (InternetAddress[]) aList.toArray(new InternetAddress[0]);
  -        
  +
           return ia;
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org