You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ca...@apache.org on 2001/05/21 20:20:55 UTC

cvs commit: jakarta-taglibs/mailer/src/org/apache/taglibs/mailer SendTag.java

catlett     01/05/21 11:20:55

  Modified:    mailer/src/org/apache/taglibs/mailer SendTag.java
  Log:
  support for mail attachments added
  
  Revision  Changes    Path
  1.3       +233 -151  jakarta-taglibs/mailer/src/org/apache/taglibs/mailer/SendTag.java
  
  Index: SendTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/mailer/src/org/apache/taglibs/mailer/SendTag.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SendTag.java	2001/04/11 20:56:41	1.2
  +++ SendTag.java	2001/05/21 18:20:50	1.3
  @@ -1,7 +1,7 @@
   /*
  - * $Header: /home/cvs/jakarta-taglibs/mailer/src/org/apache/taglibs/mailer/SendTag.java,v 1.2 2001/04/11 20:56:41 catlett Exp $
  - * $Revision: 1.2 $
  - * $Date: 2001/04/11 20:56:41 $
  + * $Header: /home/cvs/jakarta-taglibs/mailer/src/org/apache/taglibs/mailer/SendTag.java,v 1.3 2001/05/21 18:20:50 catlett Exp $
  + * $Revision: 1.3 $
  + * $Date: 2001/05/21 18:20:50 $
    *
    * ====================================================================
    *
  @@ -67,6 +67,7 @@
   import javax.servlet.jsp.tagext.*;
   import javax.mail.*;
   import javax.mail.internet.*;
  +import javax.activation.DataHandler;
   
   /**
    * SendTag - JSP tag <b>Send</b> is used to send the created email.
  @@ -103,6 +104,11 @@
   
   	int i = 0;  // counter for list of extra header name/value pairs
   	MimeMessage message;  // message object that contains this message
  +// Added by Jayson Falkner - 5/8/2001 --------------------------
  +	MimeMultipart multipart; // multipart for this message
  +// End of added.
  +	ArrayList bodyparts;  // the list of attachments
  +	ListIterator iterate;  // iterator for the list of attachments
   	String to = null;  // the to address for this e-mail message
   	String cc = null;  // the cc address for this e-mail message
   	String bcc = null;  // the bcc address for this e-mail message
  @@ -116,205 +122,281 @@
   	// get message from parent tag
   	message = myparent.getMessage();
   
  -	try {
  -	    // get the list of name and values for the headers to be added
  -	    ArrayList name = myparent.getHeaderName();
  -	    ArrayList value = myparent.getHeaderValue();
  +	// get the list of name and values for the headers to be added
  +	ArrayList name = myparent.getHeaderName();
  +	ArrayList value = myparent.getHeaderValue();
   
  -	    try {
  -		// set extra headers if any
  -	        if (name.size() > 0)
  -		    for (i = 0; i < name.size(); i++)
  -		        message.setHeader((String)name.get(i),
  -					  (String)value.get(i));
  -	    } catch (MessagingException me) {
  -		throw new JspException("Header " + name.get(i).toString()
  +	try {
  +	    // set extra headers if any
  +	    if (name.size() > 0)
  +		for (i = 0; i < name.size(); i++)
  +		    message.setHeader((String)name.get(i),
  +					       (String)value.get(i));
  +	} catch (MessagingException me) {
  +	    throw new JspException("Header " + name.get(i).toString()
   			  + " was not able to be set");
  -	    }
  +	}
   
  -	    // get the to address(es)
  -	    if((to = myparent.getTo()) != null) {
  +	// get the to address(es)
  +	if ((to = myparent.getTo()) != null) {
   
  -		// due to the way address are added if addTo is used a ','
  -		// could be in the first position strip it
  -		if (to.indexOf(',') == 0)
  -		    to = to.substring(1);
  -
  -		try {
  -		    // set the to address for this message
  -		    // catch any errors in the format of the addresses
  -	            message.setRecipients(Message.RecipientType.TO, 
  +	    // due to the way address are added if addTo is used a ','
  +	    // could be in the first position strip it
  +	    if (to.indexOf(',') == 0)
  +		to = to.substring(1);
  +
  +	    try {
  +		// set the to address for this message
  +		// catch any errors in the format of the addresses
  +	        message.setRecipients(Message.RecipientType.TO, 
   				      InternetAddress.parse(to));
  -		} catch (AddressException ae) {
  -		    // get the address that the error occured with
  -		    String ref = ae.getRef();
  -
  -		    // check for more than one address
  -		    if (ref.indexOf(',') != -1) {
  -			// position of the start of the error inducing address
  -			int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1;
  -			// extract the error inducing address
  -			ref = ref.substring(pos, ref.indexOf(','));
  -		    }
  -
  -		    // check for existence of error if it does not exist create it
  -		    if (error == null)
  -			error = new ArrayList();
  +	    } catch (AddressException ae) {
  +		// get the address that the error occured with
  +		String ref = ae.getRef();
  +
  +		// check for more than one address
  +		if (ref.indexOf(',') != -1) {
  +		    // position of the start of the error inducing address
  +		    int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1;
  +		    // extract the error inducing address
  +		    ref = ref.substring(pos, ref.indexOf(','));
  +		}
   
  -	            String errorinput = "The to address " + ref + " is not in"
  -			      + " the proper format.";
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
   
  -		    error.add(errorinput);
  -		}
  -	    } else {
  -		// if no to address has been given through an error
  -		String errorinput = "A to address must be supplied.";
  +	        String errorinput = "The to address " + ref + " is not in"
  +			      + " the proper format.";
   
   		error.add(errorinput);
  -	    }
  +	    } catch (MessagingException me) {
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
   
  -	    // set the Reply-to address if it hax been supplied
  -	    if (myparent.getReplyTo() != null) {
  +		// exception occurs when any of the addresses cannot be
  +		// properly set in the message
  +                String errorinput = "Messaging Exception: To address/es could"
  +			      + " not be set in the message." + me.getMessage();
   
  -		try {
  -		    message.setReplyTo(
  -                        InternetAddress.parse(myparent.getReplyTo()));
  -		} catch (AddressException ae) {
  -		    // check for existence of error if it does not exist create it
  -		    if (error == null)
  -			error = new ArrayList();
  +		error.add(errorinput);
  +	    }
  +	} else {
  +	    // if no to address has been given through an error
  +	    String errorinput = "A to address must be supplied.";
   
  -		    // exception occurs when the cc address cannot be parsed
  -		    String errorinput = "The Reply-To address was incorrectly set";
  +	    error.add(errorinput);
  +	}
   
  -		    error.add(errorinput);
  -		}
  -	    }
  +	// set the Reply-to address if it hax been supplied
  +	if (myparent.getReplyTo() != null) {
   
   	    try {
  +		message.setReplyTo(InternetAddress.parse(myparent.getReplyTo()));
  +	    } catch (AddressException ae) {
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
   
  -		// get from address from the parent tag
  -		String from = myparent.getFrom();
  +		// exception occurs when the cc address cannot be parsed
  +		String errorinput = "The Reply-To address was incorrectly set";
   
  -	        // set from address for this message
  -	        // check for user entered from address
  -	        if ((from == null) || (from.length() < 2)) {
  -
  -		    if (myparent.getSessionObj().getProperty("from") != null)
  -		    // check to see if from is set at the level of the Session obj
  -	                message.setFrom(new InternetAddress(
  -	    		   myparent.getSessionObj().getProperty("from")));
  -		}
  -		else
  -	            message.setFrom(new InternetAddress(from)); 
  -
  +		error.add(errorinput);
   	    } catch (MessagingException me) {
   		// check for existence of error if it does not exist create it
   		if (error == null)
   		    error = new ArrayList();
   
  -		// add exception to the list of errors in the e-mail
  -		String errorinput = "The from address was not set or is not in"
  -		    + " the proper format for an email address.";
  +		// exception occurs when any of the addresses cannot be
  +		// properly set in the message
  +                String errorinput = "Messaging Exception: Reply-To address/es"
  +		        + " could not be set in the message." + me.getMessage();
   
   		error.add(errorinput);
   	    }
  +	}
  +
  +	try {
  +
  +	    // get from address from the parent tag
  +	    String from = myparent.getFrom();
  +
  +	    // set from address for this message
  +	    // check for user entered from address
  +	    if ((from == null) || (from.length() < 2)) {
  +
  +		// check to see if from is set at the level of the Session
  +		if (myparent.getSessionObj().getProperty("mail.from") != null)
  +
  +	            message.setFrom(new InternetAddress(
  +	    		   myparent.getSessionObj().getProperty("mail.from")));
  +	    }
  +	    else
  +		message.setFrom(new InternetAddress(from));
   
  -	    // check for and set cc addresses
  -	    if ((cc = myparent.getCc()) != null) {
  +	} catch (MessagingException me) {
  +	    // check for existence of error if it does not exist create it
  +	    if (error == null)
  +		error = new ArrayList();
   
  -		// due to the way address are added if addCc is used a
  -		// ',' could be in the first position strip it
  -		if (cc.indexOf(',') == 0)
  -		    cc = cc.substring(1);
  +	    // add exception to the list of errors in the e-mail
  +	    String errorinput = "The from address was not set or is not in"
  +		    + " the proper format for an email address.";
   
  -		try {
  -                    message.setRecipients(Message.RecipientType.CC, 
  +	    error.add(errorinput);
  +	}
  +
  +	// check for and set cc addresses
  +	if ((cc = myparent.getCc()) != null) {
  +
  +	    // due to the way address are added if addCc is used a
  +	    // ',' could be in the first position strip it
  +	    if (cc.indexOf(',') == 0)
  +		cc = cc.substring(1);
  +
  +	    try {
  +                message.setRecipients(Message.RecipientType.CC, 
   					   InternetAddress.parse(cc));
  -		} catch (AddressException ae) {
  -		    // get the address that the error occured with
  -		    String ref = ae.getRef();
  -
  -		    // check for more than one address
  -		    if (ref.indexOf(',') != -1) {
  -			// position of the start of the error inducing address
  -			int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1;
  -			// extract the error inducing address
  -			ref = ref.substring(pos, ref.indexOf(','));
  -		    }
  -
  -		    // check for existence of error if it does not exist create it
  -		    if (error == null)
  -			error = new ArrayList();
  +	    } catch (AddressException ae) {
  +		// get the address that the error occured with
  +		String ref = ae.getRef();
  +
  +		// check for more than one address
  +		if (ref.indexOf(',') != -1) {
  +		    // position of the start of the error inducing address
  +		    int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1;
  +		    // extract the error inducing address
  +		    ref = ref.substring(pos, ref.indexOf(','));
  +		}
   
  -		    // exception occurs when the to address cannot be parsed
  -		    String errorinput = "The cc address " + ref + " is not in"
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
  +
  +		// exception occurs when the to address cannot be parsed
  +		String errorinput = "The cc address " + ref + " is not in"
   			      + " the proper format.";
   
  -		    error.add(errorinput);
  -		}
  -            }
  +		error.add(errorinput);
  +	    } catch (MessagingException me) {
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
   
  -	    // check for and set bcc addresses
  -            if ((bcc = myparent.getBcc()) != null) {
  +		// exception occurs when any of the addresses cannot be
  +		// properly set in the message
  +                String errorinput = "Messaging Exception: Some cc address/es"
  +			                + " could not be set in the message." 
  +			                + me.getMessage();
   
  -		// due to the way address are added if addBcc is used a
  -		// ',' could be in the first position strip it
  -		if (bcc.indexOf(',') == 0)
  -		    bcc = bcc.substring(1);
  +		error.add(errorinput);
  +	    }
  +	}
   
  -		try {
  -                    message.setRecipients(Message.RecipientType.BCC, 
  +	// check for and set bcc addresses
  +	if ((bcc = myparent.getBcc()) != null) {
  +
  +	    // due to the way address are added if addBcc is used a
  +	    // ',' could be in the first position strip it
  +	    if (bcc.indexOf(',') == 0)
  +		bcc = bcc.substring(1);
  +
  +	    try {
  +                message.setRecipients(Message.RecipientType.BCC, 
   					  InternetAddress.parse(bcc));
  -		} catch (AddressException ae) {
  -		    // get the address that the error occured with
  -		    String ref = ae.getRef();
  -
  -		    // check for more than one address
  -		    if (ref.indexOf(',') != -1) {
  -			// position of the start of the error inducing address
  -			int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1;
  -			// extract the error inducing address
  -			ref = ref.substring(pos, ref.indexOf(','));
  -		    }
  -
  -		    // check for existence of error if it does not exist create it
  -		    if (error == null)
  -			error = new ArrayList();
  +	    } catch (AddressException ae) {
  +		// get the address that the error occured with
  +		String ref = ae.getRef();
  +
  +		// check for more than one address
  +		if (ref.indexOf(',') != -1) {
  +		    // position of the start of the error inducing address
  +		    int pos = ref.substring(0, ae.getPos()).indexOf(',') + 1;
  +		    // extract the error inducing address
  +		    ref = ref.substring(pos, ref.indexOf(','));
  +		}
   
  -		    // exception occurs when the to address cannot be parsed
  -		    String errorinput = "The bcc address " + ref + " is not in"
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
  +
  +		// exception occurs when the to address cannot be parsed
  +		String errorinput = "The bcc address " + ref + " is not in"
   			      + " the proper format.";
   
  -		    error.add(errorinput);
  -		}
  -            }
  +		error.add(errorinput);
  +	    } catch (MessagingException me) {
  +		// check for existence of error if it does not exist create it
  +		if (error == null)
  +		    error = new ArrayList();
  +
  +		// exception occurs when any of the addresses cannot be
  +		// properly set in the message
  +                String errorinput = "Messaging Exception: Some bcc address/es"
  +			                + " could not be set in the message." 
  +			                + me.getMessage();
   
  +		error.add(errorinput);
  +	    }
  +	}
  +
  +	try {
   	    // set the subject in the message
   	    message.setSubject(myparent.getSubject());
   
   	    // add the sent date time to the message
   	    message.setSentDate(new Date());
  +	} catch (MessagingException me) {
  +	    // error occured while adding one of the above to the message
  +	}
   
  -	    // set the message with a mimetype according to type set by user
  -            if (myparent.getType().equalsIgnoreCase("html"))
  -                message.setContent(myparent.getBody(), "text/html");
  -            else 
  -                message.setContent(myparent.getBody(), "text/plain");
  +	// check if there are attachments
  +	if (myparent.getAttachments()) {
  +	    // create a multipart object and set the message as the first
  +	    // part then add the attachments
  +	    multipart = new MimeMultipart();
   
  -	} catch (MessagingException me) {
  -	    // check for existence of error if it does not exist create it
  -	    if (error == null)
  -		error = new ArrayList();
  +	    try {
   
  -	    // exception occurs when any of the addresses cannot be
  -	    // properly set in the message
  -            String errorinput = "Messaging Exception: Some address could"
  -	    		       + " not be set in the message." + me.getMessage();
  +		// create a mimebodypart for the body of the e-mail message
  +		MimeBodyPart mbp = new MimeBodyPart();
   
  -	    error.add(errorinput);
  -	}
  +		// set the content in the bodypart 
  +		mbp.setContent(myparent.getBody(), myparent.getType());
  +
  +		// add the message as the first bodypart in the multipart object
  +		multipart.addBodyPart(mbp);
  +
  +		// get the list of attachments
  +		iterate = myparent.getBodyParts().listIterator();
  +
  +		// loop through the list of attachments and add them to the 
  +		// multipart object
  +		while (iterate.hasNext()) {
  +		    multipart.addBodyPart((MimeBodyPart) iterate.next());
  +		}
  +
  +		// add the multipart object with the attachments to the message
  +		message.setContent(multipart);
   
  +	    } catch (MessagingException me) {
  +		// error occured while adding the message to the multipart
  +		// content
  +		throw new JspException("An error occured while trying to add" +
  +				       "the attachments to the e-mail, please"
  +				       +" to send the e-mail again.");
  +	    }
  +	} else {
  +	    try {
  +		// set the message with a mimetype according to type set by user
  +		message.setContent(myparent.getBody(), myparent.getType());
  +	    } catch (MessagingException me) {
  +		// this error is not very likely to occur
  +		throw new JspException("The message could not be set in " +
  +				   "the e-mail, please back up and try again.");
  +	    }
  +	}
   	// check if errors have occured in creating the message
   	if (error != null)
   	    return EVAL_BODY_TAG;
  @@ -335,8 +417,8 @@
        *  @throws JSPException  thrown when an error occurs while processing the
        *                        body of this method
        *
  -     *  @return - int telling the tag handler whether or not to evaluate the rest
  -     *             of the JSP
  +     *  @return - int telling the tag handler whether or not to evaluate the
  +     *            rest of the JSP
        *
        */
       public final int doEndTag() throws JspException {